spring boot:发送带附件的邮件和html内容的邮件(以163.com邮箱为例/spring boot 2.3.2)
一,网站哪些情况下需要发送电子邮件?
作为一个电商网站,以下情况需要发邮件通知用户:
注册成功的信息
用邮箱接收验证码
找回密码时发链接
发送推广邮件
下单成功后的订单通知
给商户的对账单邮件
说明:刘宏缔的架构森林是一个专注架构的博客,
网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/24/springboot-fa-song-dai-fu-jian-de-you-jian-he-html-nei-rong-de-you-jian-yi-163com-you-xiang-wei-li/
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,演示项目说明
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端口
可以参见这一篇文章:
四,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 = "demouser@163.com"; String[] to = {"371125307@qq.com"}; 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 = "demouser@163.com"; String[] to = {"371125307@qq.com"}; 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)