参考:
package com.cy.sendmail.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;
/**
* @author z
* @title: SendEmailUtil
* @projectName sendmail
* @description: 向outlook发送邮件工具
* @date 2020/11/17 13:43
*/
@Component
public class SendEmailUtil {
private Properties props;
private Session session;
private MimeMessage message;
@Autowired
private DateFormatUtil dateFormatUtil;
@Value("${Email.fromEmail}")
private String fromEmail;
@Value("${Email.server}")
private String emailServer;
@Value("${Email.fromEmailPwd}")
private String fromEmailPwd;
/**
* 邮件内容部分,html
* @return
*/
public void emailText(Date pwdLastSet, int days) {
try{
Multipart mimeMultipart = new MimeMultipart();
// 主题
message.setSubject("自动发送:提醒测试","utf-8");
// 内容
MimeBodyPart text = new MimeBodyPart();
text.setContent("您好:<br> " +
"测试邮件内容","text/html;charset=UTF-8");
mimeMultipart.addBodyPart(text);
// 放到Message中
message.setContent(mimeMultipart);
message.saveChanges();
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送邮件
*/
public void send(String toEmail,Date pwdLastSet, int days){
try{
props = new Properties();
//发件人
String from = props.getProperty("fromEmail", this.fromEmail);
//收件人
String to = props.getProperty("toEmail", toEmail);
props.put("mail.transport.protocol", "smtp"); // 设置协议
props.put("mail.smtp.host", emailServer); // 服务器地址
props.put("mail.smtp.port", "587"); // 端口号,默认25,TLS加密587,SSL加密465
props.put("mail.smtp.auth", "true");
//当前smtp host设为可信任
props.put("mail.smtp.ssl.trust", "mail.chaoyue.com.cn");
props.put("mail.smtp.ssl", "true");// 设置是否使用ssl安全连接
props.put("mail.smtp.starttls.enable", "true"); // 设置是否使用tsl安全连接
// props.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
// 用户验证并返回Session
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发件人邮箱前缀,邮箱登录账户名
String userId = props.getProperty("userId", fromEmail.substring(0, fromEmail.indexOf("@")));
//发件人邮箱密码(qq、163等邮箱用的是授权码,outlook是密码)
String password = props.getProperty("password", fromEmailPwd);
return new PasswordAuthentication(userId, password);
}
};
session = Session.getInstance(props, authenticator);
// 创建MimeMessage消息对象,消息头配置了收发邮箱的地址,消息体包含了邮件标题和邮件内容
message = new MimeMessage(session);
message.setFrom(new InternetAddress(from)); // 发送人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));// 接收者类型:TO代表直接发送,CC代表抄送,BCC代表秘密抄送
// 生成文件内容,两个参数是我需要在邮件中显示的内容
emailText(pwdLastSet, days);
// 发送
Transport.send(message);
System.out.println("-------------" + toEmail + "--邮件发送成功!");
}catch (Exception e) {
e.printStackTrace();
System.out.println("*************"+ toEmail +"邮件发送失败!");
}
}
}
存在的问题
①2020.11.23 发送5封邮件后即报该错误:Connection closed by remote host。似乎由于发送过于频繁被邮件服务器锁定,与邮件服务器设置有关,待研究
202012.01 Javax.mail打印详细的Debug信息,打开之前代码中这一行的注释:
// props.put("mail.debug", "true");// 设置是否显示debug信息 true 会在控制台显示相关信息
能看到报错:
421 4.4.2 Message submission rate for this client has exceeded the configured limit
邮件服务器限制了每分钟最大发送数量,需要修改服务器配置参数MessageRateLimit:
参考以下地址修改: