亚马逊SES通过SMTP接口发送一封电子邮件(java#语言)
更新日期(2014.7.27)
此示例使用Eclipse IDE 的 Java EE 开发人员和 JavaMail API 发送邮件通过亚马逊 SES 使用 SMTP 界面。JavaMail API 包括在Java EE 平台,可作为与Java SE 平台使用的可选包。如果您没有安装 JavaMail API,它从JavaMail安装.
您执行以下步骤前,完成与亚马逊 SES 开始之前和发送的电子邮件通过亚马逊 SES 使用 SMTP中所述的安装任务.
重要
在此获取入门教程中,您一封电子邮件给自己发送以便你可以检查,看看是否你收到它。为进一步的实验或负载测试,使用亚马逊 SES 邮箱模拟器只要有可能。电子邮件,您发送到邮箱模拟器不能算你发送配额或你反弹和投诉率。有关详细信息,请参阅测试亚马逊 SES 的电子邮件发送.
要用 Java 使用亚马逊 SES SMTP 接口发送电子邮件
在 Eclipse 中创建一个项目,请执行以下步骤:
打开 Eclipse。
在 Eclipse 中,单击文件,单击新建,,然后单击Java 项目.
在创建 Java 项目对话框中,键入项目的名称并单击下。
在Java 设置对话框中,单击库选项卡。
单击添加外部 jar 文件.
浏览到您的 JavaMail 的安装,单击 mail.jar,然后单击打开。Java 设置对话框中现在应该类似于下图:
在Java 设置对话框中,单击完成.
在 Eclipse 中,在包资源管理器窗口中,展开您的项目。
根据您的项目,用鼠标右键单击src目录,单击新建,,然后单击类.
在新的 Java 类对话框中,在名称字段中,键入AmazonSESSample
,然后单击完成.
AmazonSESSample.java 的整个内容替换为下面的代码:
import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class AmazonSESSample { static final String FROM = "SENDER@EXAMPLE.COM"; // Replace with your "From" address. This address must be verified. static final String TO = "RECIPIENT@EXAMPLE.COM"; // Replace with a "To" address. If you have not yet requested // production access, this address must be verified. static final String BODY = "This email was sent through the Amazon SES SMTP interface by using Java."; static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)"; // Supply your SMTP credentials below. Note that your SMTP credentials are different from your AWS credentials. static final String SMTP_USERNAME = "YOUR_SMTP_USERNAME"; // Replace with your SMTP username. static final String SMTP_PASSWORD = "YOUR_SMTP_PASSWORD"; // Replace with your SMTP password. // Amazon SES SMTP host name. This example uses the us-east-1 region. static final String HOST = "email-smtp.us-east-1.amazonaws.com"; // Port we will connect to on the Amazon SES SMTP endpoint. We are choosing port 25 because we will use // STARTTLS to encrypt the connection. static final int PORT = 25; public static void main(String[] args) throws Exception { // Create a Properties object to contain connection configuration information. Properties props = System.getProperties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.port", PORT); // Set properties indicating that we want to use STARTTLS to encrypt the connection. // The SMTP session will begin on an unencrypted connection, and then the client // will issue a STARTTLS command to upgrade to an encrypted connection. props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.starttls.required", "true"); // Create a Session object to represent a mail session with the specified properties. Session session = Session.getDefaultInstance(props); // Create a message with the specified information. MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(FROM)); msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO)); msg.setSubject(SUBJECT); msg.setContent(BODY,"text/plain"); // Create a transport. Transport transport = session.getTransport(); // Send the message. try { System.out.println("Attempting to send an email through the Amazon SES SMTP interface..."); // Connect to Amazon SES using the SMTP username and password you specified above. transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD); // Send the email. transport.sendMessage(msg, msg.getAllRecipients()); System.out.println("Email sent!"); } catch (Exception ex) { System.out.println("The email was not sent."); System.out.println("Error message: " + ex.getMessage()); } finally { // Close and terminate the connection. transport.close(); } } }
在 AmazonSESSample.java,用您自己的值替换以下电子邮件地址:
重要
电子邮件地址是区分大小写。请确保地址是完全一样的你验证的那些。
SENDER@EXAMPLE.COM — — 替换为您"从"的电子邮件地址。在运行此程序之前,您必须验证此地址。更多的信息,请参阅验证电子邮件地址和域在亚马逊 SES.
RECIPIENT@EXAMPLE.COM — — 替换你"到"电子邮件地址。如果你未有向要求生产的访问,您必须验证此地址,在你使用它之前。有关详细信息,请参阅亚马逊 SES 要求生产访问.
在 AmazonSESSample.java,用您在获得亚马逊 SES SMTP 凭据中获得的值替换以下 SMTP 凭据:
重要
您的 SMTP 凭据是不同于您的 AWS 凭据。有关凭据的详细信息,请参阅使用与亚马逊 SES 的凭据.
YOUR_SMTP_USERNAME — — 替换为您的 SMTP 用户名凭据。请注意您的 SMTP 用户名凭据是 20 个字符的字符串的字母和数字,不容易理解的名称。
YOUR_SMTP_PASSWORD — — 替换为您的 SMTP 密码。
(可选)如果你想要在美国东部 (弗吉尼亚) 以外任何一个区域中使用亚马逊 SES SMTP 的终结点,您需要更改主机上的 AmazonSESSample.java 到您想要使用的终结点。亚马逊 SES 终结点的列表,请参阅地区和亚马逊 SES.
保存 AmazonSESSample.java。
要生成的项目,请单击项目,然后单击生成项目。(如果此选项被禁用,然后你可能有启用的自动生成。
要启动该程序并发送电子邮件,单击运行,然后再次单击运行。
审查程序的控制台输出来验证发送成功。(你应该看看"发送的电子邮件!")
登录到收件人地址的电子邮件客户端。你应该找你发送的电子邮件消息。