參考:
1、用Java發(fā)送郵件 帶有圖片和附件
2、java代碼如何發(fā)送一封帶圖片的郵件
3、Javamail官方論壇-沒啥人
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發(fā)送郵件工具
* @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;
/**
* 郵件內(nèi)容部分,html
* @return
*/
public void emailText(Date pwdLastSet, int days) {
try{
Multipart mimeMultipart = new MimeMultipart();
// 主題
message.setSubject("自動發(fā)送:提醒測試","utf-8");
// 內(nèi)容
MimeBodyPart text = new MimeBodyPart();
text.setContent("您好:<br> " +
"測試郵件內(nèi)容","text/html;charset=UTF-8");
mimeMultipart.addBodyPart(text);
// 放到Message中
message.setContent(mimeMultipart);
message.saveChanges();
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 發(fā)送郵件
*/
public void send(String toEmail,Date pwdLastSet, int days){
try{
props = new Properties();
//發(fā)件人
String from = props.getProperty("fromEmail", this.fromEmail);
//收件人
String to = props.getProperty("toEmail", toEmail);
props.put("mail.transport.protocol", "smtp"); // 設(shè)置協(xié)議
props.put("mail.smtp.host", emailServer); // 服務(wù)器地址
props.put("mail.smtp.port", "587"); // 端口號,默認(rèn)25,TLS加密587,SSL加密465
props.put("mail.smtp.auth", "true");
//當(dāng)前smtp host設(shè)為可信任
props.put("mail.smtp.ssl.trust", "mail.chaoyue.com.cn");
props.put("mail.smtp.ssl", "true");// 設(shè)置是否使用ssl安全連接
props.put("mail.smtp.starttls.enable", "true"); // 設(shè)置是否使用tsl安全連接
// props.put("mail.debug", "true");// 設(shè)置是否顯示debug信息 true 會在控制臺顯示相關(guān)信息
// 用戶驗(yàn)證并返回Session
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//發(fā)件人郵箱前綴,郵箱登錄賬戶名
String userId = props.getProperty("userId", fromEmail.substring(0, fromEmail.indexOf("@")));
//發(fā)件人郵箱密碼(qq、163等郵箱用的是授權(quán)碼,outlook是密碼)
String password = props.getProperty("password", fromEmailPwd);
return new PasswordAuthentication(userId, password);
}
};
session = Session.getInstance(props, authenticator);
// 創(chuàng)建MimeMessage消息對象,消息頭配置了收發(fā)郵箱的地址,消息體包含了郵件標(biāo)題和郵件內(nèi)容
message = new MimeMessage(session);
message.setFrom(new InternetAddress(from)); // 發(fā)送人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));// 接收者類型:TO代表直接發(fā)送,CC代表抄送,BCC代表秘密抄送
// 生成文件內(nèi)容,兩個參數(shù)是我需要在郵件中顯示的內(nèi)容
emailText(pwdLastSet, days);
// 發(fā)送
Transport.send(message);
System.out.println("-------------" + toEmail + "--郵件發(fā)送成功!");
}catch (Exception e) {
e.printStackTrace();
System.out.println("*************"+ toEmail +"郵件發(fā)送失??!");
}
}
}
存在的問題
①2020.11.23 發(fā)送5封郵件后即報該錯誤:Connection closed by remote host。似乎由于發(fā)送過于頻繁被郵件服務(wù)器鎖定,與郵件服務(wù)器設(shè)置有關(guān),待研究

報錯.png
202012.01 Javax.mail打印詳細(xì)的Debug信息,打開之前代碼中這一行的注釋:
// props.put("mail.debug", "true");// 設(shè)置是否顯示debug信息 true 會在控制臺顯示相關(guān)信息
能看到報錯:
421 4.4.2 Message submission rate for this client has exceeded the configured limit