依赖
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
SMTP发送
static String USER_NAME =“@outlook.com";
static String PASSWORD = "”;
public static boolean sendSMTPMail(String to, String text, String title) {
String host = "smtp.office365.com";
String mailStoreType = "smtp";
String popPort = "587";
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
props.put("mail.store.protocol", mailStoreType);
props.put( "mail.smtp.port", popPort );
//开启SSL
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.port",popPort);
props.put("mail.smtp.socketFactory.fallback","false");
try {
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USER_NAME, PASSWORD);//账号密码
}
});
session.setDebug(true);
// 创建邮件消息
MimeMessage message = new MimeMessage(session);
// 设置发件人
InternetAddress form = new InternetAddress(USER_NAME);
message.setFrom(form);
// 设置收件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 设置邮件标题
message.setSubject(title);
// 设置邮件的内容体
message.setContent(text, "text/html;charset=UTF-8");
// 发送邮件
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}