AWS SDK를 사용하여 Amazon SES를 통해 이메일 보내기 - Amazon Simple Email Service

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

AWS SDK를 사용하여 Amazon SES를 통해 이메일 보내기

AWS SDK를 사용하여 Amazon SES를 통해 이메일을 보낼 수 있습니다. AWS SDK는 여러 프로그래밍 언어로 사용할 수 있습니다. 자세한 내용은 Amazon Web Services용 도구를 참조하십시오.

필수 조건

다음 섹션의 코드 샘플을 완료하려면 다음 필수 구성 요소를 완료해야 합니다.

  • 아직 하지 않았다면, Amazon Simple Email Service 설정의 작업을 완료합니다.

  • Amazon SES에서 이메일 주소 확인—Amazon SES에서 이메일을 보내기 전에 발신자 이메일 주소의 소유자인지 확인해야 합니다. 사용자 계정이 아직 Amazon SES 샌드박스 환경에 있는 경우 수신자 이메일 주소도 확인해야 합니다. Amazon SES 콘솔을 사용하여 이메일 주소를 확인하는 것이 좋습니다. 자세한 정보는 이메일 주소 자격 증명 생성을 참조하세요.

  • AWS 자격 증명 받기 —SDK를 사용하여 Amazon SES에 액세스하려면 AWS 액세스 키 ID와 AWS 보안 액세스 키가 필요합니다. AWS Management Console의 보안 자격 증명 페이지를 사용하여 자격 증명을 찾을 수 있습니다. 자격 증명에 대한 자세한 내용은 Amazon SES 자격 증명 유형 단원을 참조하세요.

  • 공유 자격 증명 파일 생성—이 단원의 샘플 코드가 올바로 실행되기 위해서는 공유 자격 증명 파일을 생성해야 합니다. 자세한 정보는 AWS SDK를 사용하여 Amazon SES를 통해 이메일을 보낼 때 사용할 공유 자격 증명 파일 생성을 참조하세요.

코드 예시

중요

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

프로그래밍 언어를 선택하여 해당 언어에 대한 예제를 봅니다.
    .NET

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

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

    • Microsoft Visual Studio Community 2017, 버전 15.4.0

    • Microsoft .NET Framework 버전 4.6.1

    • AWSSDK.Core 패키지 (버전 3.3.19) 를 사용하여 설치했습니다. NuGet

    • 그. AWSSDK SimpleEmail 패키지 (버전 3.3.6.1), 를 사용하여 설치됨. NuGet

    시작하기 전에 다음 작업을 수행하세요.
    를 사용하여 이메일을 보내려면 AWS SDK for .NET
    1. 다음 단계에 따라 새 프로젝트를 만듭니다.

      1. Visual Studio를 시작합니다.

      2. File 메뉴에서 NewProject를 차례대로 선택합니다.

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

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

      5. 이름AmazonSESSample을 입력한 다음 확인을 선택합니다.

    2. 다음 단계를 완료하여 Amazon SES 패키지를 솔루션에 포함시키는 데 사용하십시오 NuGet .

      1. 솔루션 탐색기 창에서 프로젝트를 마우스 오른쪽 버튼으로 클릭한 다음 NuGet 패키지 관리를 선택합니다.

      2. NuGet: AmazonsSample 탭에서 찾아보기를 선택합니다.

      3. 검색 상자에 AWSSDK.SimpleEmail를 입력합니다.

      4. AWSSDK선택합니다. SimpleEmail패키지를 선택한 다음 설치를 선택합니다.

      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. 코드 편집기에서 다음을 수행합니다.

      • sender@example.com을 "From:"의 이메일 주소로 바꿉니다. 이 주소가 확인되어야 합니다. 자세한 내용은 Amazon SES에서 확인된 자격 증명 단원을 참조하세요.

      • recipient@example.com을 "To:"의 주소로 바꿉니다. 계정이 아직 샌드박스 환경에 있을 경우 이 주소도 확인해야 합니다.

      • 이 이메일을 보낼 때 사용할 구성 세트의 ConfigSet이름으로 바꾸십시오.

      • USWest2를 Amazon SES를 사용하여 이메일을 보내는 데 사용하는 AWS 리전 엔드포인트 이름으로 바꾸십시오. Amazon SES를 사용할 수 있는 리전 목록은 AWS 일반 참조Amazon Simple Email Service(Amazon SES)를 참조하세요.

      완료되면 Program.cs를 저장합니다.

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

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

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

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

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

    Java

    다음 절차는 Java EE 개발자용 Eclipse IDE를 사용하는 방법과 AWS SDK 프로젝트를 생성하고 Amazon SES를 통해 이메일을 보내도록 Java 코드를 수정하는 방법을 보여줍니다. AWS Toolkit for Eclipse

    시작하기 전에 다음 작업을 수행하세요.
    를 사용하여 이메일을 보내려면 AWS SDK for Java
    1. 다음 단계를 수행하여 Eclipse에서 AWS 자바 프로젝트를 생성합니다.

      1. Eclipse를 시작합니다.

      2. [File] 메뉴에서 [New]와 [Other]를 차례대로 선택합니다. 창에서 AWS 폴더를 확장하여 AWS Java Project(Java 프로젝트)를 선택합니다.

      3. 새 AWS Java 프로젝트 대화 상자에서 다음과 같이 하십시오.

        1. [Project name]에 프로젝트 이름을 입력합니다.

        2. AWS SDK for Java 샘플에서 Amazon 단순 이메일 서비스 JavaMail 샘플을 선택합니다.

        3. 마침을 클릭합니다.

    2. Eclipse에서 [Package Explorer] 창에서 프로젝트를 확장합니다.

    3. 프로젝트 아래에서 src/main/java 폴더와 com.amazon.aws.samples 폴더를 차례대로 확장한 후 AmazonSESSample.java를 두 번 클릭합니다.

    4. AmazonSESSample.java의 전체 내용을 다음 코드로 바꿉니다.

      package com.amazonaws.samples; import java.io.IOException; import com.amazonaws.regions.Regions; import com.amazonaws.services.simpleemail.AmazonSimpleEmailService; import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder; import com.amazonaws.services.simpleemail.model.Body; import com.amazonaws.services.simpleemail.model.Content; import com.amazonaws.services.simpleemail.model.Destination; import com.amazonaws.services.simpleemail.model.Message; import com.amazonaws.services.simpleemail.model.SendEmailRequest; public class AmazonSESSample { // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. static final String FROM = "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 final String TO = "recipient@example.com"; // The configuration set to use for this email. If you do not want to use a // configuration set, comment the following variable and the // .withConfigurationSetName(CONFIGSET); argument below. static final String CONFIGSET = "ConfigSet"; // The subject line for the email. static final String SUBJECT = "Amazon SES test (AWS SDK for Java)"; // The HTML body for the email. static final String HTMLBODY = "<h1>Amazon SES test (AWS SDK for Java)</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-java/'>" + "AWS SDK for Java</a>"; // The email body for recipients with non-HTML email clients. static final String TEXTBODY = "This email was sent through Amazon SES " + "using the AWS SDK for Java."; public static void main(String[] args) throws IOException { try { AmazonSimpleEmailService client = AmazonSimpleEmailServiceClientBuilder.standard() // Replace US_WEST_2 with the AWS Region you're using for // Amazon SES. .withRegion(Regions.US_WEST_2).build(); SendEmailRequest request = new SendEmailRequest() .withDestination( new Destination().withToAddresses(TO)) .withMessage(new Message() .withBody(new Body() .withHtml(new Content() .withCharset("UTF-8").withData(HTMLBODY)) .withText(new Content() .withCharset("UTF-8").withData(TEXTBODY))) .withSubject(new Content() .withCharset("UTF-8").withData(SUBJECT))) .withSource(FROM) // Comment or remove the next line if you are not using a // configuration set .withConfigurationSetName(CONFIGSET); client.sendEmail(request); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent. Error message: " + ex.getMessage()); } } }
    5. AmazonSESSample.java에서 다음 값을 사용자의 값으로 대체합니다.

      중요

      이메일 주소는 대/소문자를 구분합니다. 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.

      • SENDER@EXAMPLE.COM—"From" 이메일 주소로 대체합니다. 이 프로그램을 실행하기 전에 이 주소를 확인해야 합니다. 자세한 내용은 Amazon SES에서 확인된 자격 증명 단원을 참조하세요.

      • RECIPIENT@EXAMPLE.COM—"To" 이메일 주소로 대체합니다. 계정이 아직 샌드박스에 있는 경우, 이 주소를 확인해야 계정을 사용할 수 있습니다. 자세한 내용은 프로덕션 액세스 요청 (Amazon SES 샌드박스 밖으로 이동) 단원을 참조하세요.

      • (선택 사항) us-west-2—미국 서부(오레곤) 이외의 리전에서 Amazon SES를 사용하려면 이것을 사용할 리전으로 바꿉니다. Amazon SES를 사용할 수 있는 리전 목록은 AWS 일반 참조Amazon Simple Email Service(Amazon SES)를 참조하세요.

    6. AmazonSESSample.java을(를) 저장합니다.

    7. 프로젝트를 빌드하려면 [Project]를 선택한 후 [Build Project]를 선택합니다.

      참고

      이 옵션이 비활성화되어 있는 경우 자동 빌드가 활성화될 수도 있습니다. 그렇다면 이 단계를 건너뜁니다.

    8. 프로그램을 시작하고 이메일을 전송하려면 [Run]을 선택하고 다시 [Run]을 선택합니다.

    9. Eclipse의 콘솔 창에서 출력을 확인합니다. 이메일이 성공적으로 전송되었으면 콘솔에 "Email sent!"가 표시됩니다. 그렇지 않으면 오류 메시지가 표시됩니다.

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

    PHP

    이 주제에서는 AWS SDK for PHP을(를) 사용하여 Amazon SES를 통해 이메일을 전송하는 방법을 보여줍니다.

    시작하기 전에 다음 작업을 수행하세요.
    • PHP 설치—PHP는 http://php.net/downloads.php에서 다운로드할 수 있습니다. 본 자습서에서는 PHP 버전 5.5 이상 사용이 필수적입니다. PHP를 설치한 후, 원하는 모든 명령 프롬프트에서 PHP를 실행할 수 있도록 PHP에 대한 경로를 환경 변수에 추가합니다. 이 자습서의 코드는 PHP 7.2.7을 사용하여 테스트하였습니다.

    • AWS SDK for PHP 버전 3 설치 - 다운로드 및 설치 지침은 AWS SDK for PHP 설명서를 참조하십시오. 이 자습서의 코드는 SDK 버전 3.64.13을 사용하여 테스트하였습니다.

    다음을 사용하여 Amazon SES를 통해 이메일을 보내려면 AWS SDK for PHP
    1. 텍스트 편집기에서 amazon-ses-sample.php이라는 파일을 만듭니다. 다음 코드를 붙여넣습니다.

      <?php // If necessary, modify the path in the require statement below to refer to the // location of your Composer autoload.php file. require 'vendor/autoload.php'; use Aws\Ses\SesClient; use Aws\Exception\AwsException; // Create an SesClient. Change the value of the region parameter if you're // using an AWS Region other than US West (Oregon). Change the value of the // profile parameter if you want to use a profile in your credentials file // other than the default. $SesClient = new SesClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-west-2' ]); // Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES. $sender_email = 'sender@example.com'; // Replace these sample addresses with the addresses of your recipients. If // your account is still in the sandbox, these addresses must be verified. $recipient_emails = ['recipient1@example.com','recipient2@example.com']; // Specify a configuration set. If you do not want to use a configuration // set, comment the following variable, and the // 'ConfigurationSetName' => $configuration_set argument below. $configuration_set = 'ConfigSet'; $subject = 'Amazon SES test (AWS SDK for PHP)'; $plaintext_body = 'This email was sent with Amazon SES using the AWS SDK for PHP.' ; $html_body = '<h1>AWS Amazon Simple Email Service Test Email</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-php/">'. 'AWS SDK for PHP</a>.</p>'; $char_set = 'UTF-8'; try { $result = $SesClient->sendEmail([ 'Destination' => [ 'ToAddresses' => $recipient_emails, ], 'ReplyToAddresses' => [$sender_email], 'Source' => $sender_email, 'Message' => [ 'Body' => [ 'Html' => [ 'Charset' => $char_set, 'Data' => $html_body, ], 'Text' => [ 'Charset' => $char_set, 'Data' => $plaintext_body, ], ], 'Subject' => [ 'Charset' => $char_set, 'Data' => $subject, ], ], // If you aren't using a configuration set, comment or delete the // following line 'ConfigurationSetName' => $configuration_set, ]); $messageId = $result['MessageId']; echo("Email sent! Message ID: $messageId"."\n"); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo("The email was not sent. Error message: ".$e->getAwsErrorMessage()."\n"); echo "\n"; }
    2. amazon-ses-sample.php에서 다음 값을 사용자의 값으로 대체합니다.

      • path_to_sdk_inclusion—프로그램에 포함시키는 데 필요한 경로로 바꾸십시오. AWS SDK for PHP 자세한 내용은 AWS SDK for PHP 설명서를 참조하세요.

      • sender@example.com—Amazon SES에서 확인한 이메일 주소로 바꿉니다. 자세한 내용은 확인된 자격 증명 단원을 참조하세요. Amazon SES에서 이메일 주소는 대/소문자를 구분합니다. 입력하는 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.

      • recipient1@example.com, recipient2@example.com - 수신자의 주소로 바꿉니다. 계정이 아직 샌드박스 환경에 있을 경우 수신자의 주소도 확인해야 합니다. 자세한 내용은 프로덕션 액세스 요청 (Amazon SES 샌드박스 밖으로 이동) 단원을 참조하세요. 입력하는 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.

      • (선택 사항) ConfigSet—이 이메일을 전송할 때 구성 세트를 사용하려면 이 값을 구성 세트의 이름으로 바꿉니다. 구성 세트에 대한 자세한 내용은 Amazon SES에서 구성 세트 사용 단원을 참조하세요.

      • (선택 사항) us-west-2—미국 서부(오레곤) 이외의 리전에서 Amazon SES를 사용하려면 이것을 사용할 리전으로 바꿉니다. Amazon SES를 사용할 수 있는 리전 목록은 AWS 일반 참조Amazon Simple Email Service(Amazon SES)를 참조하세요.

    3. amazon-ses-sample.php을(를) 저장합니다.

    4. 프로그램을 실행하려면 amazon-ses-sample.php와 동일한 디렉터리에서 명령 프롬프트를 열고 다음 명령을 입력합니다.

      $ php amazon-ses-sample.php
    5. 출력 결과를 검토합니다. 이메일이 성공적으로 전송되었으면 콘솔에 "Email sent!"가 표시됩니다. 그렇지 않으면 오류 메시지가 표시됩니다.

      참고

      프로그램을 실행할 때 "cURL error 60: SSL certificate problem" 오류가 발생하면 AWS SDK for PHP 설명서의 설명에 따라 최신 CA 번들을 다운로드합니다. 그런 다음 amazon-ses-sample.php에서 SesClient::factory 어레이에 다음 줄을 추가하고, path_of_certs를 CA 번들 다운로드 경로로 바꾼 후 프로그램을 다시 실행합니다.

      'http' => [ 'verify' => 'path_of_certs\ca-bundle.crt' ]
    6. 수신자 주소의 이메일 클라이언트에 로그인합니다. 보낸 메시지가 도착해 있을 것입니다.

    Ruby

    이 주제에서는 AWS SDK for Ruby을(를) 사용하여 Amazon SES를 통해 이메일을 전송하는 방법을 보여줍니다.

    시작하기 전에 다음 작업을 수행하세요.
    다음을 사용하여 Amazon SES를 통해 이메일을 보내려면 AWS SDK for Ruby
    1. 텍스트 편집기에서 amazon-ses-sample.rb이라는 파일을 만듭니다. 다음 코드를 파일에 붙여넣습니다.

      require 'aws-sdk' # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. sender = "sender@example.com" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. recipient = "recipient@example.com" # Specify a configuration set. If you do not want to use a configuration # set, comment the following variable and the # configuration_set_name: configsetname argument below. configsetname = "ConfigSet" # Replace us-west-2 with the AWS Region you're using for Amazon SES. awsregion = "us-west-2" # The subject line for the email. subject = "Amazon SES test (AWS SDK for Ruby)" # The HTML body of the email. htmlbody = '<h1>Amazon SES test (AWS SDK for Ruby)</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-ruby/">'\ 'AWS SDK for Ruby</a>.' # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the AWS SDK for Ruby." # Specify the text encoding scheme. encoding = "UTF-8" # Create a new SES resource and specify a region ses = Aws::SES::Client.new(region: awsregion) # Try to send the email. begin # Provide the contents of the email. resp = ses.send_email({ destination: { to_addresses: [ recipient, ], }, message: { body: { html: { charset: encoding, data: htmlbody, }, text: { charset: encoding, data: textbody, }, }, subject: { charset: encoding, data: subject, }, }, source: sender, # Comment or remove the following line if you are not using # a configuration set configuration_set_name: configsetname, }) puts "Email sent!" # If something goes wrong, display an error message. rescue Aws::SES::Errors::ServiceError => error puts "Email not sent. Error message: #{error}" end
    2. amazon-ses-sample.rb에서 다음 값을 사용자의 값으로 대체합니다.

      • sender@example.com—Amazon SES에서 확인한 이메일 주소로 바꿉니다. 자세한 내용은 확인된 자격 증명 단원을 참조하세요. Amazon SES에서 이메일 주소는 대/소문자를 구분합니다. 입력하는 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.

      • recipient@example.com—수신자의 주소로 바꿉니다. 계정이 아직 샌드박스에 있는 경우, 이 주소를 확인해야 계정을 사용할 수 있습니다. 자세한 내용은 프로덕션 액세스 요청 (Amazon SES 샌드박스 밖으로 이동) 단원을 참조하세요. 입력하는 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.

      • (선택 사항) us-west-2—미국 서부(오레곤) 이외의 리전에서 Amazon SES를 사용하려면 이것을 사용할 리전으로 바꿉니다. Amazon SES를 사용할 수 있는 리전 목록은 AWS 일반 참조Amazon Simple Email Service(Amazon SES)를 참조하세요.

    3. amazon-ses-sample.rb을(를) 저장합니다.

    4. 프로그램을 실행하려면 amazon-ses-sample.rb와 동일한 디렉터리에서 명령 프롬프트를 열고 ruby amazon-ses-sample.rb를 입력합니다.

    5. 출력 결과를 검토합니다. 이메일이 성공적으로 전송되었으면 콘솔에 "Email sent!"가 표시됩니다. 그렇지 않으면 오류 메시지가 표시됩니다.

    6. 수신자 주소의 이메일 클라이언트에 로그인합니다. 보낸 메시지가 도착해 있을 것입니다.

    Python

    이 주제에서는 AWS SDK for Python (Boto)을(를) 사용하여 Amazon SES를 통해 이메일을 전송하는 방법을 보여줍니다.

    시작하기 전에 다음 작업을 수행하세요.
    • Amazon SES에서 이메일 주소 확인—Amazon SES에서 이메일을 보내기 전에 발신자 이메일 주소의 소유자인지 확인해야 합니다. 사용자 계정이 아직 Amazon SES 샌드박스 환경에 있는 경우 수신자 이메일 주소도 확인해야 합니다. Amazon SES 콘솔을 사용하여 이메일 주소를 확인하는 것이 좋습니다. 자세한 정보는 이메일 주소 자격 증명 생성을 참조하세요.

    • AWS 자격 증명 받기 —SDK를 사용하여 Amazon SES에 액세스하려면 AWS 액세스 키 ID와 AWS 보안 액세스 키가 필요합니다. AWS Management Console의 보안 자격 증명 페이지를 사용하여 자격 증명을 찾을 수 있습니다. 자격 증명에 대한 자세한 내용은 Amazon SES 자격 증명 유형 단원을 참조하세요.

    • Python 설치—Python은 https://www.python.org/downloads/에서 다운로드할 수 있습니다. 이 자습서의 코드는 Python 2.7.6 및 Python 3.6.1을 사용하여 테스트하였습니다. Python을 설치한 후 원하는 모든 명령 프롬프트에서 Python을 실행할 수 있도록 Python에 대한 경로를 환경 변수에 추가합니다.

    • 설치 AWS SDK for Python (Boto) —다운로드 및 설치 지침은 설명서를 참조하십시오.AWS SDK for Python (Boto) 이 자습서의 샘플 코드는 SDK for Python 버전 1.4.4를 사용하여 테스트하였습니다.

    SDK for Python을 사용하여 Amazon SES를 통해 이메일을 전송하려면
    1. 텍스트 편집기에서 amazon-ses-sample.py이라는 파일을 만듭니다. 다음 코드를 파일에 붙여넣습니다.

      import boto3 from botocore.exceptions import ClientError # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. SENDER = "Sender Name <sender@example.com>" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. RECIPIENT = "recipient@example.com" # Specify a configuration set. If you do not want to use a configuration # set, comment the following variable, and the # ConfigurationSetName=CONFIGURATION_SET argument below. CONFIGURATION_SET = "ConfigSet" # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. AWS_REGION = "us-west-2" # The subject line for the email. SUBJECT = "Amazon SES Test (SDK for Python)" # The email body for recipients with non-HTML email clients. BODY_TEXT = ("Amazon SES Test (Python)\r\n" "This email was sent with Amazon SES using the " "AWS SDK for Python (Boto)." ) # The HTML body of the email. BODY_HTML = """<html> <head></head> <body> <h1>Amazon SES Test (SDK for Python)</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-python/'> AWS SDK for Python (Boto)</a>.</p> </body> </html> """ # The character encoding for the email. CHARSET = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client('ses',region_name=AWS_REGION) # Try to send the email. try: #Provide the contents of the email. response = client.send_email( Destination={ 'ToAddresses': [ RECIPIENT, ], }, Message={ 'Body': { 'Html': { 'Charset': CHARSET, 'Data': BODY_HTML, }, 'Text': { 'Charset': CHARSET, 'Data': BODY_TEXT, }, }, 'Subject': { 'Charset': CHARSET, 'Data': SUBJECT, }, }, Source=SENDER, # If you are not using a configuration set, comment or delete the # following line ConfigurationSetName=CONFIGURATION_SET, ) # Display an error if something goes wrong. except ClientError as e: print(e.response['Error']['Message']) else: print("Email sent! Message ID:"), print(response['MessageId'])
    2. amazon-ses-sample.py에서 다음 값을 사용자의 값으로 대체합니다.

      • sender@example.com—Amazon SES에서 확인한 이메일 주소로 바꿉니다. 자세한 정보는 확인된 자격 증명을 참조하세요. Amazon SES에서 이메일 주소는 대/소문자를 구분합니다. 입력하는 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.

      • recipient@example.com—수신자의 주소로 바꿉니다. 계정이 아직 샌드박스에 있는 경우, 이 주소를 확인해야 계정을 사용할 수 있습니다. 자세한 내용은 프로덕션 액세스 요청 (Amazon SES 샌드박스 밖으로 이동) 단원을 참조하세요. 입력하는 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.

      • (선택 사항) us-west-2—미국 서부(오레곤) 이외의 리전에서 Amazon SES를 사용하려면 이것을 사용할 리전으로 바꿉니다. Amazon SES를 사용할 수 있는 리전 목록은 AWS 일반 참조Amazon Simple Email Service(Amazon SES)를 참조하세요.

    3. amazon-ses-sample.py을(를) 저장합니다.

    4. 프로그램을 실행하려면 amazon-ses-sample.py와 동일한 디렉터리에서 명령 프롬프트를 열고 python amazon-ses-sample.py를 입력합니다.

    5. 출력 결과를 검토합니다. 이메일이 성공적으로 전송되었으면 콘솔에 "Email sent!"가 표시됩니다. 그렇지 않으면 오류 메시지가 표시됩니다.

    6. 수신자 주소의 이메일 클라이언트에 로그인합니다. 보낸 메시지가 도착해 있을 것입니다.