springboot傳送郵件
阿新 • • 發佈:2020-08-28
springboot傳送郵件
概述
使用場景
- 註冊驗證
- 營銷推送
- 觸發機制
- 監控報警
郵件傳輸協議
- SMTP協議:全稱為 Simple Mail Transfer Protocol,簡單郵件傳輸協議。它定義了郵件客戶端軟體和SMTP郵件伺服器之間,以及兩臺SMTP郵件伺服器之間的通訊規則。
- POP3協議:全稱為 Post Office Protocol,郵局協議。它定義了郵件客戶端軟體和POP3郵件伺服器的通訊規則。
- IMAP協議:全稱為 Internet Message Access Protocol,Internet訊息訪問協議,它是對POP3協議的一種擴充套件,也是定義了郵件客戶端軟體和IMAP郵件伺服器的通訊規則。
springboot傳送郵件
準備郵箱
開啟服務,新增授權密碼
引入依賴
pom.xml依賴spring-boot-starter-mail模組:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
新增配置
spring: #郵件 mail: host: smtp.163.com # 你的163郵箱 username: 163傳送郵箱 # 注意這裡不是郵箱密碼,而是SMTP授權密碼 password: smtp授權碼 port: 25 protocol: smtp default-encoding: UTF-8
程式碼
服務類
@Slf4j @Service public class MailService { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender mailSender; /** * 設定郵件附件名不被擷取,解決附件中文名亂碼問題 */ public MailService() { System.setProperty("mail.mime.splitlongparameters", "false"); } /** * 簡單文字郵件 * * @param to 接收者郵件 * @param subject 郵件主題 * @param contnet 郵件內容 */ public void sendSimpleMail(String to, String subject, String contnet) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(to); message.setSubject(subject); message.setText(contnet); message.setFrom(from); mailSender.send(message); } /** * HTML 文字郵件 * * @param to 接收者郵件 * @param subject 郵件主題 * @param contnet HTML內容 * @throws MessagingException */ public void sendHtmlMail(String to, String subject, String contnet) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(contnet, true); helper.setFrom(from); mailSender.send(message); } /** * 附件郵件 * * @param to 接收者郵件 * @param subject 郵件主題 * @param contnet HTML內容 * @param filePath 附件路徑 * @throws MessagingException */ public void sendAttachmentsMail(String to, String subject, String contnet, String filePath) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(contnet, true); helper.setFrom(from); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = file.getFilename(); helper.addAttachment(fileName, file); mailSender.send(message); } /** * 圖片郵件 * * @param to 接收者郵件 * @param subject 郵件主題 * @param contnet HTML內容 * @param rscPath 圖片路徑 * @param rscId 圖片ID * @throws MessagingException */ public void sendInlinkResourceMail(String to, String subject, String contnet, String rscPath, String rscId) { log.info("傳送靜態郵件開始: {},{},{},{},{}", to, subject, contnet, rscPath, rscId); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message, true); helper.setTo(to); helper.setSubject(subject); helper.setText(contnet, true); helper.setFrom(from); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); log.info("傳送靜態郵件成功!"); } catch (MessagingException e) { log.info("傳送靜態郵件失敗: ", e); } } }
測試類
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HealthApplication.class)
public class MailServiceTest {
@Autowired
private MailService mailService;
private String toMail = "[email protected]";
@Test
public void sendSimpleMail() {
mailService.sendSimpleMail(toMail,"測試spring boot imail-主題","測試spring boot imail - 內容");
}
@Test
public void sendHtmlMail() throws MessagingException {
String content = "<html>\n" +
"<body>\n" +
"<h3>hello world</h3>\n" +
"<h1>html</h1>\n" +
"<body>\n" +
"</html>\n";
mailService.sendHtmlMail(toMail,"這是一封HTML郵件",content);
}
@Test
public void sendAttachmentsMail() throws MessagingException {
String filePath = "/Users/liufq/Downloads/心電獲客排名2020-08-26 16_26_54.xlsx";
String content = "<html>\n" +
"<body>\n" +
"<h3>hello world</h3>\n" +
"<h1>html</h1>\n" +
"<h1>附件傳輸</h1>\n" +
"<body>\n" +
"</html>\n";
mailService.sendAttachmentsMail(toMail,"這是一封HTML郵件",content, filePath);
}
@Test
public void sendInlinkResourceMail() throws MessagingException {
//TODO 改為本地圖片目錄
String imgPath = "/ijiangtao/img/blob/dd9899b4cf95cbf074ddc4607007046c022564cb/blog/animal/dog/dog-at-work-with-computer-2.jpg?raw=true";
String rscId = "admxj001";
String content = "<html>" +
"<body>" +
"<h3>hello world</h3>" +
"<h1>html</h1>" +
"<h1>圖片郵件</h1>" +
"<img src='cid:"+rscId+"'></img>" +
"<body>" +
"</html>";
mailService.sendInlinkResourceMail(toMail,"這是一封圖片郵件",content, imgPath, rscId);
}
}
部署
在生產環境,一般郵件服務會單獨部署,並通過HTTP或MQ等方式暴露出來。
問題
附件顯示名中文亂碼的解決
原因:編碼後的檔名長度如果大於60並且splitLongParameters的值為true,encodeParameters的值為true,檔名就會被擷取
service的構造中新增設定,或在主啟動類中新增設定
public MailService() {
System.setProperty("mail.mime.splitlongparameters", "false");
}
550 User has no permission
傳送方郵箱為開啟smtp授權
參考資料
https://blog.csdn.net/sinat_26342009/article/details/89425836