java 使用 Amazon SES发送电子邮件,有三种方式,具体哪三种请移步这里。
这里只介绍一种:利用Amazon SES 的 SMTP 服务,javax.mail发送邮件。此方法也可以直接用于163邮箱(需要自己开启邮箱的SMTP服务),亲测有效。
1、先将发送邮件的邮箱在Amazon SES里面验证一下,具体验证方法请参考这里。在使用过程中,发送送邮件返回错误状态码具体参考这里。
2、Amazon SES默认是Sandbox模式,需要发送申请production access,要等他们review。
3、代码示例:(代码自行抽取)
package com.iot.task;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;
import org.quartz.*;
import redis.clients.jedis.Jedis;
import javax.mail.*;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.util.Properties;
import java.util.List;
public class PushAlarmNotificationTask implements Job {
// Replace sender@example.com with your "From" address.
// This address must be verified with Amazon SES.
private static final String FROM = "";
private static final String FROMNAME = "";
private static final String TO = "";
// Replace smtp_username with your Amazon SES SMTP user name.
private static final String SMTP_USERNAME = "";
// Replace smtp_password with your Amazon SES SMTP password.
private static final String SMTP_PASSWORD = "";
// The name of the Configuration Set to use for this message.
// If you comment out or remove this variable, you will also need to
// comment out or remove the header below.
private static final String CONFIGSET = "ConfigSet";
// Amazon SES SMTP host name. This example uses the 美国西部(俄勒冈) region.
// See https://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html#region-endpoints
// for more information.
private static final String HOST = "email-smtp.us-east-1.amazonaws.com";
// The port you will connect to on the Amazon SES SMTP endpoint.
private static final int PORT = 587;
private static final String SUBJECT = "";
private String BODY = String.join(
System.getProperty("line.separator"),
"");
private String Message = "Hello Email";
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Transport transport = null;
try {
// 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);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "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, FROMNAME));
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(TO));
msg.setSubject(SUBJECT);
BODY = BODY + Message;
msg.setContent(BODY, "text/html");
// Add a configuration set header. Comment or delete the
// next line if you are not using a configuration set
//msg.setHeader("X-SES-CONFIGURATION-SET", CONFIGSET);
// Create a transport.
transport = session.getTransport();
// 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());
sendNum++;
} catch (UnsupportedEncodingException | MessagingException ex) {
ex.printStackTrace();
} finally {
// Close and terminate the connection.
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
}