재우니의 블로그



https://docs.aws.amazon.com/ko_kr/ses/latest/DeveloperGuide/send-using-sdk-net.html


.NET용 AWS SDK를 사용하여 이메일 전송

다음 절차에서는 Visual Studio 및 .NET용 AWS SDK를 사용하여 Amazon SES를 통해 이메일을 보내는 방법을 보여 줍니다.

이 솔루션은 다음 구성 요소를 사용하여 테스트되었습니다.

  • Microsoft Visual Studio Community 2017, 버전 15.4.0

  • Microsoft .NET Framework 버전 4.6.1

  • NuGet을 사용하여 설치된 AWSSDK.Core 패키지(버전 3.3.18.1)

  • NuGet을 사용하여 설치된 AWSSDK.SimpleEmail 패키지(버전 3.3.4)

참고

이 시작 자습서에서는 수신 여부를 확인할 수 있도록 자신에게 이메일을 발송합니다. 추가적인 실험 또는 로드 테스트는 Amazon SES 메일박스 시뮬레이터를 사용하십시오. 메일박스 시뮬레이터로 전송되는 이메일은 발신 할당량이나 반송 메일 및 불만 제기 발생률에 포함되지 않습니다. 자세한 내용은 Amazon SES 이메일 전송 테스트 단원을 참조하십시오.

사전 조건

시작하기 전에 다음 작업을 수행하십시오.

  • Amazon SES에서 이메일 주소 확인–Amazon SES에서 이메일을 보내기 전에 발신자 이메일 주소의 소유자인지 확인해야 합니다. 계정이 아직 Amazon SES 샌드박스에 있는 경우, 수신자 이메일 주소도 확인해야 합니다. 이메일 주소를 확인하는 가장 쉬운 방법은 Amazon SES 콘솔을 사용하는 것입니다. 자세한 내용은 확인 절차 단원을 참조하십시오.

  • AWS 자격 증명 획득 – SDK를 사용해 Amazon SES에 액세스하려면 AWS 액세스 키 ID와 AWS 보안 액세스 키가 필요합니다. AWS Management Console의 Security Credentials 페이지에서 자격 증명을 확인할 수 있습니다. 자격 증명에 대한 자세한 내용은 Amazon SES에서 자격 증명 사용을 참조하십시오.

  • Visual Studio 설치 - Visual Studio는 https://www.visualstudio.com/에서 다운로드할 수 있습니다.

  • 공유 자격 증명 파일 만들기 - 이 섹션의 샘플 코드가 올바로 실행되기 위해서는 공유 자격 증명 파일을 만들어야 합니다. 자세한 내용은 공유 자격 증명 파일 생성 단원을 참조하십시오.

프로시저

다음 절차에서는 .NET용 AWS SDK를 사용하여 Amazon SES를 통해 이메일을 보내는 방법을 보여 줍니다.

.NET용 AWS SDK을 사용하여 이메일을 전송하려면

  1. 다음 단계에 따라 새 프로젝트를 만듭니다.

    1. Visual Studio를 시작합니다.

    2. [File] 메뉴에서 [New]와 [Project]를 차례대로 선택합니다.

    3. [New Project] 창의 왼쪽 패널에서 [Installed]를 확장한 후 [Visual C#]을 확장합니다.

    4. 오른쪽 패널에서 [Console App (.NET Framework)]을 선택합니다.

    5. [Name] 필드에 AmazonSESSample을 입력한 후 [OK]를 선택합니다.

  2. NuGet을 사용해 다음 단계를 완료하여 솔루션에 Amazon SES 패키지를 포함시킵니다.

    1. 솔루션 탐색기에서 프로젝트를 마우스 오른쪽 버튼으로 클릭한 후 [Manage NuGet Packages for Solution]을 선택합니다.

    2. [NuGet - Solution] 탭에서 [Browse]를 선택합니다.

    3. 검색 상자에 AWSSDK.SimpleEmail을 입력합니다. [AWSSDK.SimpleEmail] 패키지를 선택합니다.

    4. [Versions] 아래에서 프로젝트 옆의 상자를 선택한 후 [Install]을 선택합니다.

    5. [Preview Changes] 창에서 [OK]를 선택합니다.

  3. [Program.cs] 탭에서 다음 코드를 붙여넣습니다.

    using Amazon; using System; using System.Collections.Generic; using Amazon.SimpleEmail; using Amazon.SimpleEmail.Model; namespace AmazonSESSample { class Program { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. static readonly string senderAddress = "sender@example.com"; // Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified. static readonly string receiverAddress = "recipient@example.com"; // The configuration set to use for this email. If you do not want to use a // configuration set, comment out the following property and the // ConfigurationSetName = configSet argument below. static readonly string configSet = "ConfigSet"; // The subject line for the email. static readonly string subject = "Amazon SES test (AWS SDK for .NET)"; // The email body for recipients with non-HTML email clients. static readonly string textBody = "Amazon SES Test (.NET)\r\n" + "This email was sent through Amazon SES " + "using the AWS SDK for .NET."; // The HTML body of the email. static readonly string htmlBody = @"<html> <head></head> <body> <h1>Amazon SES Test (AWS SDK for .NET)</h1> <p>This email was sent with <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-net/'> AWS SDK for .NET</a>.</p> </body> </html>"; static void Main(string[] args) { // Replace USWest2 with the AWS Region you're using for Amazon SES. // Acceptable values are EUWest1, USEast1, and USWest2. using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2)) { var sendRequest = new SendEmailRequest { Source = senderAddress, Destination = new Destination { ToAddresses = new List<string> { receiverAddress } }, Message = new Message { Subject = new Content(subject), Body = new Body { Html = new Content { Charset = "UTF-8", Data = htmlBody }, Text = new Content { Charset = "UTF-8", Data = textBody } } }, // If you are not using a configuration set, comment // or remove the following line ConfigurationSetName = configSet }; try { Console.WriteLine("Sending email using Amazon SES..."); var response = client.SendEmail(sendRequest); Console.WriteLine("The email was sent successfully."); } catch (Exception ex) { Console.WriteLine("The email was not sent."); Console.WriteLine("Error message: " + ex.Message); } } Console.Write("Press any key to continue..."); Console.ReadKey(); } } }
  4. Save Program.cs.

  5. 다음 단계를 완료하여 애플리케이션을 빌드 및 실행합니다.

    1. [Build] 메뉴에서 [Build Solution]을 선택합니다.

    2. [Debug] 메뉴에서 [Start Debugging]을 선택합니다. 콘솔 창이 나타납니다.

  6. 콘솔 출력을 확인합니다. 이메일이 성공적으로 전송되었으면 콘솔에 "The email was sent successfully."가 표시되고, 그렇지 않으면 오류 메시지가 표시됩니다.

  7. 이메일이 성공적으로 전송되었으면 수신자 주소의 이메일 클라이언트에 로그인합니다. 보낸 메시지가 도착해 있을 것입니다.