亚马逊SES通过SMTP接口发送一封电子邮件(c#语言)
更新日期(2014.7.27)
下面的过程显示了如何使用Microsoft Visual Studio创建一个控制台应用程序和修改的 C# 代码,发送电子邮件通过亚马逊 SES。基于跨 Microsoft Visual Studio 的版本,是类似的项目模板创建一个新项目的过程,但我们会经过使用 Microsoft Visual Studio 专业 2012年的程序。
您执行以下步骤前,完成与亚马逊 SES 开始之前和发送的电子邮件通过亚马逊 SES 使用 SMTP中所述的安装任务.
重要
在此获取入门教程中,您一封电子邮件给自己发送以便你可以检查,看看是否你收到它。为进一步的实验或负载测试,使用亚马逊 SES 邮箱模拟器只要有可能。电子邮件,您发送到邮箱模拟器不能算你发送配额或你反弹和投诉率。有关详细信息,请参阅测试亚马逊 SES 的电子邮件发送.
要用 C# 使用亚马逊 SES SMTP 接口发送电子邮件
在 Visual Studio 中创建一个控制台项目,请执行以下步骤:
打开 Microsoft Visual Studio。
单击文件,单击新建,然后单击项目.
在新建项目对话框中的在左窗格中,展开安装,展开模板,然后再展开Visual C#.
在Visual C# 中,单击窗口.
单击控制台应用程序.
在名称字段中,键入AmazonSESSample
。对话框中看起来应该类似于下图。
单击确定.
在 Visual Studio 项目中,用以下代码替换 Program.cs 的全部内容:
using System; namespace AmazonSESSample { class Program { static void Main(string[] args) { const String FROM = "SENDER@EXAMPLE.COM"; // Replace with your "From" address. This address must be verified. const String TO = "RECIPIENT@EXAMPLE.COM"; // Replace with a "To" address. If you have not yet requested // production access, this address must be verified. const String SUBJECT = "Amazon SES test (SMTP interface accessed using C#)"; const String BODY = "This email was sent through the Amazon SES SMTP interface by using C#."; // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials. const String SMTP_USERNAME = "YOUR_SMTP_USERNAME"; // Replace with your SMTP username. const String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD"; // Replace with your SMTP password. // Amazon SES SMTP host name. This example uses the us-east-1 region. const String HOST = "email-smtp.us-east-1.amazonaws.com"; // Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 587 because we will use // STARTTLS to encrypt the connection. const int PORT = 587; // Create an SMTP client with the specified host name and port. using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(HOST, PORT)) { // Create a network credential with your SMTP user name and password. client.Credentials = new System.Net.NetworkCredential(SMTP_USERNAME, SMTP_PASSWORD); // Use SSL when accessing Amazon SES. The SMTP session will begin on an unencrypted connection, and then // the client will issue a STARTTLS command to upgrade to an encrypted connection using SSL. client.EnableSsl = true; // Send the email. try { Console.WriteLine("Attempting to send an email through the Amazon SES SMTP interface..."); client.Send(FROM, TO, SUBJECT, BODY); Console.WriteLine("Email sent!"); } 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(); } } }
在 Program.cs,用您自己的值替换以下电子邮件地址:
重要
电子邮件地址是区分大小写。请确保地址是完全一样的你验证的那些。
SENDER@EXAMPLE.COM — — 替换为您"从"的电子邮件地址。在运行此程序之前,您必须验证此地址。更多的信息,请参阅验证电子邮件地址和域在亚马逊 SES.
RECIPIENT@EXAMPLE.COM — — 替换你"到"电子邮件地址。如果你未有向要求生产的访问,您必须验证此地址,在你使用它之前。有关详细信息,请参阅亚马逊 SES 要求生产访问.
在 Program.cs,用你在获得亚马逊 SES SMTP 凭据获得的值替换以下 SMTP 凭据:
重要
您的 SMTP 凭据是不同于您的 AWS 凭据。有关凭据的详细信息,请参阅使用与亚马逊 SES 的凭据.
YOUR_SMTP_USERNAME — — 替换为您的 SMTP 用户名。请注意您的 SMTP 用户名凭据是 20 个字符的字符串的字母和数字,不容易理解的名称。
YOUR_SMTP_PASSWORD — — 替换为您的 SMTP 密码。
(可选)如果你想要在美国东部 (弗吉尼亚) 以外任何一个区域中使用亚马逊 SES SMTP 的终结点,您需要更改主机上的 Program.cs 到您想要使用的终结点。亚马逊 SES 终结点的列表,请参阅地区和亚马逊 SES.
保存 Program.cs。
要生成的项目,请单击生成,然后单击生成解决方案.
要运行该程序,请单击调试,然后单击启动调试.
审查程序的控制台输出来验证发送成功。(你应该看看"发送的电子邮件!")
登录到收件人地址的电子邮件客户端。你应该找你发送的电子邮件消息。