http://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-java.html 中,有一个关于如何通过AWS-SES发送电子邮件的说明。它指的是access_key和secret_key。但我拥有的是我在门户上生成的SMTP用户名和SMTP密码。
目前我的代码如下:
AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
client.sendEmail(request);
AmazonSimpleEmailServiceClient 的构造函数采用 AWSCredentials,但不采用 smtp 凭证。知道如何使用SMTP凭据吗?
使用 JavaMail 作为 Amazon SES SMTP 的传输。Amazon SES 文档中还提供了有关使用 SMTP 终端节点的说明和示例代码。
如果您打算通过 Amazon SES API 发送电子邮件,请使用开发工具包。
You can use below code to send mail through SMTP
InternetAddress[] parse = InternetAddress.parse(toAddess , true);
Properties props = System.getProperties();
//Add properties
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.port", port);
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(fromemail));
msg.setRecipients(javax.mail.Message.RecipientType.TO, parse);
msg.setSubject(subject);
msg.setContent(body,"text/html");
// Create a transport.
Transport transport = session.getTransport();
// Send the message.
try
{
logger.info("Attempting to send an email through the Amazon SES SMTP interface to "+toAddess);
// Connect to Amazon SES using the SMTP username and password specified above.
transport.connect(smtpHost, port, smtpuserName, smtpPassword);
// Send the email.
transport.sendMessage(msg, msg.getAllRecipients());
logger.info("MessageID"+ msg.getMessageID());
logger.info("Email sent!");
return msg.getMessageID();
}
catch (Exception ex) {
logger.error("The email was not sent. Error message: " + ex.getMessage());
}
finally
{
// Close and terminate the connection.
transport.close();
}