spring boot:傳送帶附件的郵件和html內容的郵件(以163.com郵箱為例/spring boot 2.3.2)
阿新 • • 發佈:2020-07-28
一,網站哪些情況下需要傳送電子郵件?
作為一個電商網站,以下情況需要發郵件通知使用者:
註冊成功的資訊
用郵箱接收驗證碼
找回密碼時發連結
傳送推廣郵件
下單成功後的訂單通知
給商戶的對賬單郵件
說明:劉巨集締的架構森林是一個專注架構的部落格,地址:https://www.cnblogs.com/architectforest
對應的原始碼可以訪問這裡獲取:https://github.com/liuhongdi/
說明:作者:劉巨集締 郵箱: [email protected]
二,演示專案說明
1,專案地址:
https://github.com/liuhongdi/sendmail
2,專案說明:
分別演示了傳送帶有附件的郵件和html內容的郵件
3,專案結構:如圖:
三,配置檔案說明
1,pom.xml
<!--mail begin--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!--mail end--> <!--thymeleaf begin--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--thymeleaf end-->
說明:引入thymeleaf依賴,是因為需要有html內容的郵件需要傳送
2,application.properties
spring.mail.host=smtp.163.com spring.mail.username=demouser@163.com spring.mail.password=demopassword spring.mail.default-encoding=UTF-8 spring.mail.protocol=smtps spring.mail.port=465 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
說明:注意此處的password不是我們登入到163郵箱的密碼
而是從郵箱中獲取到的授權碼
另外如果在阿里雲ecs,也不要使用smtp的25埠,
而是需要使用smtps的465埠
可以參見這一篇文章:
https://www.cnblogs.com/architectforest/p/12924395.html
四,java程式碼說明
1,MailUtil.java
@Component public class MailUtil { @Resource private JavaMailSender javaMailSender; @Resource TemplateEngine templateEngine; //傳送普通文字內容的郵件 public void sendTextMail(String from,String[] to,String[] cc,String[] bcc,String subject,String content,String[] files) throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); helper.setSentDate(new Date()); helper.setText(content); //抄送,收到郵件使用者可以看到其他收件人 if (cc != null && cc.length > 0) { helper.setCc(cc); } //密送 收到郵件使用者看不到其他收件人 if (bcc != null && bcc.length > 0) { helper.setBcc(bcc); } //附件: if (files != null && files.length > 0) { for (String filePath : files) { File tmpOne = new File(filePath); helper.addAttachment(tmpOne.getName(),tmpOne); } } javaMailSender.send(mimeMessage); } //傳送html內容的郵件,使用thymeleaf渲染頁面 public void sendHtmlMail(String from, String[] to, String[] cc, String[] bcc, String subject, String templateName, HashMap<String,String> content) throws MessagingException { MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject(subject); helper.setFrom(from); helper.setTo(to); //抄送,收到郵件使用者可以看到其他收件人 if (cc != null && cc.length > 0) { helper.setCc(cc); } //密送 收到郵件使用者看不到其他收件人 if (bcc != null && bcc.length > 0) { helper.setBcc(bcc); } helper.setSentDate(new Date()); //生成郵件模板上的內容 Context context = new Context(); if (content != null && content.size() > 0) { for (String key : content.keySet()) { context.setVariable(key, content.get(key)); } } String process = templateEngine.process(templateName, context); helper.setText(process,true); javaMailSender.send(mimeMessage); } }
說明:html郵件和普通郵件的處理不一樣,
我們用map傳遞引數給thymeleaf模板
2,MailServiceImpl.java
@Service public class MailServiceImpl implements MailService { @Resource private MailUtil mailUtil; //傳送text格式的郵件 @Override public void sendAuthMail() { String from = "[email protected]"; String[] to = {"[email protected]"}; String subject = "老劉程式碼庫傳送給您的驗證碼"; String[] cc = {}; String[] bcc = {}; String content = "您的驗證碼:543210,請勿洩露"; String[] files = {"/data/springboot2/logo.jpg"}; try { mailUtil.sendTextMail(from,to,cc,bcc,subject,content,files); } catch (MessagingException e) { e.printStackTrace(); System.out.println("郵件傳送出錯"); } } //傳送html格式的郵件 @Override public void sendRegMail() { String from = "[email protected]"; String[] to = {"[email protected]"}; String subject = "恭喜您成功註冊老劉程式碼庫網站"; HashMap<String,String> content= new HashMap<String,String>(); content.put("username","laoliu"); content.put("nickname","老劉"); content.put("id","0000001"); String templateName= "mail/regmail.html"; try { mailUtil.sendHtmlMail(from, to, null, null, subject, templateName, content); } catch (MessagingException e) { e.printStackTrace(); System.out.println("郵件傳送出錯"); } } }
說明:注意cc和bcc的區別:
cc方式接收到的使用者可以看到其他接收使用者
bcc方式接收使用者看不到其他接收使用者
3,html內容郵件的模板:
regmail.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>您好,註冊成功! 以下是您在本網站的註冊資訊:</h1> <table border="1"> <tr> <td>使用者名稱</td> <td th:text="${username}">${username}</td> </tr> <tr> <td>暱稱</td> <td th:text="${nickname}">${nickname}</td> </tr> <tr> <td>ID</td> <td th:text="${id}">${id}</td> </tr> </table> <div style="color: #ff1a0e">本站網址:http://springio.com</div> </body> </html>
五,測試效果:
1,測試帶附件的郵件效果:
訪問:
http://127.0.0.1:8080/home/authmail
收到的郵件如圖:
2,測試傳送html內容的郵件:
訪問:
http://127.0.0.1:8080/home/regmail
收到郵件如圖:
六,檢視spring boot版本
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.2.RELEASE)