다음 절차에서는 Microsoft Visual Studio를 사용해 Amazon SES를 통해 이메일을 전송하는 C# 콘솔 애플리케이션을 만드는 방법을 보여줍니다. 이 단원에서 언급하는 절차는 Visual Studio 2017에 적용되지만 C# 콘솔 애플리케이션을 만드는 프로세스는 전체 Microsoft Visual Studio 에디션과 비슷합니다.
이 시작 자습서에서는 수신 여부를 확인할 수 있도록 자신에게 이메일을 발송합니다. 추가적인 실험 또는 로드 테스트는 Amazon SES 메일박스 시뮬레이터를 사용하십시오. 메일박스 시뮬레이터로 전송되는 이메일은 발신 할당량이나 반송 메일 및 불만 제기 발생률에 포함되지 않습니다. 자세한 내용은 Amazon SES 이메일 전송 테스트단원을 참조하십시오.
C#를 사용하여 Amazon SES SMTP 인터페이스를 통해 이메일을 전송하려면
다음 단계를 수행하여 Visual Studio에서 콘솔 프로젝트를 만듭니다.
Microsoft Visual Studio를 엽니다.
[File] 메뉴에서 [New]와 [Project]를 차례대로 선택합니다.
[New Project] 창에서 왼쪽 창에 있는 [Installed], [Templates], [Visual C#]를 차례대로 확장합니다.
[Visual C#] 아래에서 [Windows Classic Desktop]을 선택합니다.
창의 상단에 있는 메뉴에서 다음 그림과 같이 [.NET Framework 4.5]을 선택합니다.
참고
필요하다면 이후 버전의 .NET Framework를 선택할 수도 있습니다.
[Console App (.NET Framework)]을 선택합니다.
[Name(이름)] 필드에 AmazonSESSample을 입력합니다.
[OK]를 선택합니다.
Visual Studio 프로젝트에서 Program.cs의 전체 내용을 다음 코드로 바꿉니다.
using System;
using System.Net;
using System.Net.Mail;
namespaceAmazonSESSample
{
classProgram
{
staticvoidMain(string[] args){
// Replace sender@example.com with your "From" address. // This address must be verified with Amazon SES.const String FROM = "sender@example.com";
const String FROMNAME = "Sender Name";
// Replace recipient@example.com with a "To" address. If your account // is still in the sandbox, this address must be verified.const String TO = "recipient@example.com";
// Replace smtp_username with your Amazon SES SMTP user name.const String SMTP_USERNAME = "smtp_username";
// Replace smtp_password with your Amazon SES SMTP user name.const String SMTP_PASSWORD = "smtp_password";
// (Optional) the name of a configuration set to use for this message.// If you comment out this line, you also need to remove or comment out// the "X-SES-CONFIGURATION-SET" header below.const String CONFIGSET = "ConfigSet";
// If you're using Amazon SES in a region other than US West (Oregon), // replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP // endpoint in the appropriate Region.const String HOST = "email-smtp.us-west-2.amazonaws.com";
// The port you will connect to on the Amazon SES SMTP endpoint. We// are choosing port 587 because we will use STARTTLS to encrypt// the connection.constint PORT = 587;
// The subject line of the emailconst String SUBJECT =
"Amazon SES test (SMTP interface accessed using C#)";
// The body of the emailconst String BODY =
"<h1>Amazon SES Test</h1>" +
"<p>This email was sent through the "+
"<a href='https://aws.amazon.com/ses'>Amazon SES</a> SMTP interface " +
"using the .NET System.Net.Mail library.</p>";
// Create and build a new MailMessage object
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.From = new MailAddress(FROM,FROMNAME);
message.To.Add(new MailAddress(TO));
message.Subject = SUBJECT;
message.Body = BODY;
// Comment or delete the next line if you are not using a configuration set
message.Headers.Add("X-SES-CONFIGURATION-SET", CONFIGSET);
// Create and configure a new SmtpClient
SmtpClient client =
new SmtpClient(HOST, PORT);
// Pass SMTP credentials
client.Credentials =
new NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD);
// Enable SSL encryption
client.EnableSsl = true;
// Send the email. try
{
Console.WriteLine("Attempting to send email...");
client.Send(message);
Console.WriteLine("Email sent!");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
// Wait for a key press so that you can see the console output
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
}
Program.cs에서 다음 이메일 주소를 사용자의 값으로 바꿉니다.
중요
이메일 주소는 대/소문자를 구분합니다. 주소는 사용자가 확인한 것과 정확하게 동일해야 합니다.
SENDER@EXAMPLE.COM - "From" 이메일 주소로 대체합니다. 이 프로그램을 실행하기 전에 이 주소를 확인해야 합니다. 자세한 내용은 Amazon SES에서 이메일 주소 및 도메인 확인 단원을 참조하십시오.
RECIPIENT@EXAMPLE.COM - "To" 이메일 주소로 대체합니다. 계정이 아직 샌드박스에 있는 경우, 이 주소를 확인해야 계정을 사용할 수 있습니다. 자세한 내용은 Amazon SES 샌드박스 환경에서 나가기 단원을 참조하십시오.