Springboot+Javamail實現郵件傳送
使用的是spring-context-support-5.2.6.RELEASE.jar裡的javamail
javamail 官方文件:javamail
程式碼在:gitee
官方程式碼介紹
Sample code for an HTML mail with an inline image and a PDF attachment:
帶有影像和PDF附件的HTML郵件的示例程式碼:
mailSender.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException {
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
message.setFrom("me@mail.com");
message.setTo("you@mail.com");
message.setSubject("my subject");
message.setText("my text <img src='cid:myLogo'>", true);
message.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
message.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));
}
});
MimeMessageHelper引數說明
- from(發件人)
- to(收件人)
- subject(郵件主題)
- text(內容)
- inline(內嵌圖片或者圖片附件)
- attachment(附件)
- cc(抄送人)
- bcc(密送人)
Springboot 程式碼示例
開啟POP3/SMTP服務,並拿到授權碼
首先登入163郵箱 開啟POP3/SMTP服務
拿到授權碼
application.yml配置檔案不要填郵箱密碼,要填授權碼,不然會報錯:535 Error: authentication failed
application.yml配置檔案
spring:
mail:
default-encoding: UTF-8
host: smtp.163.com
username: owl_email@163.com
# 不要填郵箱密碼,要填授權碼,不然會報錯:535 Error: authentication failed
password:
protocol: smtp
properties:
mail:
smtp:
auth: true
port: 994 #465或者994
starttls:
enable: true
required: true
ssl:
enable: true
display:
sendmail: Javen
sendname: Spring Boot Guide Email
MailDetail(引數封裝)
記得setter、getter
public class MailDetail {
/**
* 發件人名稱
*/
private String senderName;
/**
* 發件人郵件地址
*/
private String senderMail;
/**
* 收件人地址
*/
private String addresseeMail;
/**
* 郵件標題
*/
private String mailTitle;
/**
* 抄送人
*/
private String[] cc;
/**
* 郵件內容
*/
private String content;
/**
* true內容為HTML,false內容為文字 預設文字
*/
private Boolean html = false;
}
傳送郵件Service層
@Service
public class MailServiceImpl implements MailService {
private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
private final JavaMailSender javaMailSender;
@Autowired
public MailServiceImpl(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
@Override
public boolean sendMessage(MailDetail detail) {
log.info("mailServiceImpl.sendMessage.request:{}", new Gson().toJson(detail));
try {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
// 發件人地址
InternetAddress fromAddress = new InternetAddress(MimeUtility.encodeText(detail.getSenderName()) + "<" + detail.getSenderMail() + ">");
messageHelper.setFrom(fromAddress);
// 收件人地址
InternetAddress toAddress = new InternetAddress(MimeUtility.encodeText(detail.getAddresseeMail()) + "<" + detail.getAddresseeMail() + ">");
messageHelper.setTo(toAddress);
// 郵件名稱
messageHelper.setSubject(detail.getMailTitle());
// 第二個引數指定傳送的是HTML格式
messageHelper.setText(detail.getContent(), detail.getHtml());
// 抄送人
if (detail.getCc() != null && detail.getCc().length > 0) {
messageHelper.setCc(detail.getCc());
}
// 測試圖片附件(ClassPathResource要把圖片放到resources,並且編譯程式碼把圖片載入到target裡)
// messageHelper.addInline("myLogo", new ClassPathResource("WechatIMG2602.jpeg"));
javaMailSender.send(message);
return true;
} catch (MessagingException | UnsupportedEncodingException e) {
log.error("mailServiceImpl.sendMessage.error:{},{}", e.getMessage(), e.getStackTrace());
}
return false;
}
}
傳送郵件Controller層
主要controller實現類ApplicationRunner 的run 專案啟動則執行
@RestController
public class MailController implements ApplicationRunner {
private static final Logger log = LoggerFactory.getLogger(MailController.class);
protected final MailService mailService;
@Autowired
public MailController(MailService mailService) {
this.mailService = mailService;
}
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("syncController.run");
MailDetail mailDetail = new MailDetail();
// 發件人名稱
mailDetail.setSenderName("雲深小麥");
// 發件人郵箱
mailDetail.setSenderMail("owl_email@163.com");
// 郵件名稱
mailDetail.setMailTitle("這裡是測試");
// 收件人郵箱
mailDetail.setAddresseeMail("guoqingyan_email@163.com");
// 傳送純文字 setHtml(false) 預設false
// mailDetail.setContent("你好!我是雲深小麥");
// 傳送HTML setHtml(true)
mailDetail.setContent("<html><head></head><body><h1>你好!我是雲深小麥</h1></body></html>");
// 內容是否為HTML
mailDetail.setHtml(true);
boolean b = mailService.sendMessage(mailDetail);
if (b) {
log.info("傳送成功");
}
}
}
結果
一些問題
535 Error: authentication failed
Caused by: javax.mail.AuthenticationFailedException: 535 Error: authentication failed
呼叫163郵箱伺服器來傳送郵件,我們需要開啟POP3/SMTP服務,這時163郵件會讓我們設定客戶端授權碼,這個授權碼替代上面程式碼部分的passwd即可成功傳送郵件
550 Invalid User
com.sun.mail.smtp.SMTPSenderFailedException: 550 Invalid User
該錯誤表示:收件地址為無效帳號。
建議:
1,請檢查收件人地址是否存在語法錯誤,例如說有攜帶多餘的標點符號。地址與地址之間的分隔號是英文字元下的分號。
2,請檢查收件人地址是否有錯誤,收件人不存在等情況。
3,請檢查發件人在系統的狀態,是否被刪除、禁用或者凍結狀態。