邮件发送服务 spring-boot-mail

交互设计

  演示图

  启动说明

  项目中RPC框架使用的是当当维护的DubboX,现在阿里已经处于维护状态中,请自行更新配置Dubbo需要安装注册中心zookeeper: http://www.52itstyle.top/thread-19791-1-1.html如果不想使用Dubbo和安装zookeeper,又想启动看下效果,请注释掉 Application 类中的@ImportResource({"classpath:spring-context-dubbo.xml"}), 同时由于接口扫描注解使用的是Dubbo的 com.alibaba.dubbo.config.annotation.Service; 请自行替换成spring的 org.springframework.stereotype.Service(废弃);Sql文件位于src/main/resource/sql下,自行导入即可、里面有一条测试数据API: http://localhost:8080/springboot_mail/swagger-ui.html、 可以自行测试发送邮件,前提是要修改application-dev.properties中的邮箱配置为自己可用的2022-10-25 原spring-context-dubbo.xml 配置 替换为 dubbo-spring-boot-starter 2.0.0执行 com.itstyle.mail.test.SpringbootMailApplication main 方法流程图

  平台架构

  进程内邮件队列

  项目结构

  ├─src│ ├─main│ │ ├─java│ │ │ └─com│ │ │ └─itstyle│ │ │ └─mail│ │ │ │ Application.java│ │ │ │ │ │ │ ├─demo│ │ │ │ CountDownLatchDemo.java│ │ │ │ Ticket.java│ │ │ │ TicketRun.java│ │ │ │ │ │ │ ├─model│ │ │ │ Email.java│ │ │ │ │ │ │ ├─queue│ │ │ │ ConsumeMailQueue.java│ │ │ │ MailQueue.java│ │ │ │ │ │ │ ├─redis│ │ │ │ Receiver.java│ │ │ │ RedisConfig.java│ │ │ │ RedisListener.java│ │ │ │ │ │ │ ├─service│ │ │ │ │ IMailService.java│ │ │ │ │ │ │ │ │ └─impl│ │ │ │ MailServiceImpl.java│ │ │ │ │ │ │ ├─task│ │ │ │ SMail.java│ │ │ │ │ │ │ └─util│ │ │ CommonUtil.java│ │ │ Constants.java│ │ │ MailUtil.java│ │ │ │ │ ├─resources│ │ │ │ application-dev.properties│ │ │ │ application-prod.properties│ │ │ │ application-test.properties│ │ │ │ application.yml│ │ │ │ spring-context-dubbo.xml│ │ │ │ spring-context-task.xml│ │ │ │ │ │ │ └─static│ │ │ ├─file│ │ │ │ 科帮网获取更多源码.zip│ │ │ │ │ │ │ ├─image│ │ │ │ springcloud.png│ │ │ │ │ │ │ └─template│ │ │ welcome.flt│ │ │ welcome.html│ │ │ │ │ └─webapp│ │ │ index.jsp│ │ │ │ │ └─WEB-INF│ │ web.xml│ │ │ └─test│ └─java│ └─com│ └─itstyle│ └─mail│ └─test│ SpringbootMailApplication.java普通文本发送富文本发送(图片、附件)freeMarker模版发送邮件thymeleaf模版发送邮件评测生成模版时间对比(1000次循环)

  Thymeleaf用时:2686msFreemarker用时:498ms对比测试,建议使用Freemarker模版

  application.properties中配置以下内容:

  spring.mail.host=smtp.qq.comspring.mail.username=345849402@qq.com#授权码g,在QQ邮箱客户端生成 修改成自己的 设置-账户-开启服务-获取授权码spring.mail.password=XXXXXXXspring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true#freemarkerspring.freemarker.template-loader-path=classpath:/static/template/spring.freemarker.enabled=truespring.freemarker.cache=falsespring.freemarker.charset=UTF-8spring.freemarker.content-type=text/htmlspring.freemarker.allow-request-override=falsespring.freemarker.check-template-location=truespring.freemarker.expose-request-attributes=falsespring.freemarker.expose-session-attributes=falsespring.freemarker.expose-spring-macro-helpers=false#thymeleafspring.thymeleaf.prefix=classpath:/static/template/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false封装实体

  首先我们,封装一个Email实体Email.java:

  /** * Email封装类 * 创建者 科帮网 * 创建时间 2022年7月20日 * */public class Email implements Serializable { private static final long serialVersionUID = 1L; //必填参数 private String email;//接收方邮件 private String subject;//主题 private String content;//邮件内容 //选填 private String template;//模板 private HashMap<String, String> kvMap;// 自定义参数 ... 省略 get set业务实现

  既然用了spring,就按照spring的方式来,先定义一个接口IMailService,接着是实现MailServiceImpl。

  以下代码,实现了四种方式:纯文本,富文本(图片,附件),Freemarker模版以及Thymeleaf模版。

  这里需要注意的是,springboot 1.4.0以后 Velocity 废弃了,官方建议用freemaker。而thymeleaf是博主自己实现的,显然效率没有freemaker高(评测对比见文章底部)。

  @Servicepublic class MailServiceImpl implements IMailService { @Autowired private JavaMailSer mailSer;//执行者 @Autowired public Configuration configuration;//freemarker @Autowired private SpringTemplateEngine templateEngine;//thymeleaf @Value("${spring.mail.username}") public String USER_NAME;//发送者 @Override public void s(Email mail) throws Exception { MailUtil mailUtil = new MailUtil(); SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(USER_NAME); message.setTo(mail.getEmail()); message.setSubject(mail.getSubject()); message.setText(mail.getContent()); mailUtil.start(mailSer, message); } @Override public void sHtml(Email mail) throws Exception { MailUtil mailUtil = new MailUtil(); MimeMessage message = mailSer.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(USER_NAME); helper.setTo(mail.getEmail()); helper.setSubject(mail.getSubject()); helper.setText( "<html><body><img src=\"cid:springcloud\" ></body></html>", true); // 发送图片 File file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "image" + Constants.SF_FILE_SEPARATOR + "springcloud.png"); helper.addInline("springcloud", file); // 发送附件 file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "file" + Constants.SF_FILE_SEPARATOR + "科帮网获取更多源码.zip"); helper.addAttachment("科帮网", file); mailUtil.startHtml(mailSer, message); } @Override public void sFreemarker(Email mail) throws Exception { MimeMessage message = mailSer.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(USER_NAME); helper.setTo(mail.getEmail()); helper.setSubject(mail.getSubject()); Map<String, Object> model = new HashMap<String, Object>(); model.put("content", mail.getContent()); Template template = configuration.getTemplate(mail.getTemplate()+".flt"); String text = FreeMarkerTemplateUtils.processTemplateIntoString( template, model); helper.setText(text, true); mailSer.s(message); } @Override public void sThymeleaf(Email mail) throws Exception { MimeMessage message = mailSer.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(USER_NAME); helper.setTo(mail.getEmail()); helper.setSubject(mail.getSubject()); Context context = new Context(); context.setVariable("email", mail); String text = templateEngine.process(mail.getTemplate(), context); helper.setText(text, true); mailSer.s(message); }}测试用例

  老司机带你去开车SpringbootMailApplication.java:

  @SpringBootApplication@ComponentScan(basePackages={"com.itstyle.mail"})public class SpringbootMailApplication implements CommandLineRunner { @Autowired private IMailService mailService; public static void main(String[] args) { SpringApplication.run(SpringbootMailApplication.class, args); } @Override public void run(String... args) throws Exception { Email mail = new Email(); mail.setEmail("345849402@qq.com"); mail.setSubject("你个小逗比"); mail.setContent("科帮网欢迎您"); mail.setTemplate("welcome"); mailService.sFreemarker(mail); }}

标签: 交互设计