十分钟实现发送邮件服务 (2)

发送结束后将邮件保存到数据库,便于统计和追查邮件问题。

private Mail saveMail(Mail mail) { // todo 发送成功/失败将邮件信息同步到数据库 return mail; } 3.3 发送邮件

发送纯文本邮件

public void sendSimpleMail(Mail mail){ checkMail(mail); SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom(sender); mailMessage.setTo(mail.getReceiver()); mailMessage.setSubject(mail.getSubject()); mailMessage.setText(mail.getText()); mailSender.send(mailMessage); saveMail(mail); }

发送邮件并携带附件

public void sendAttachmentsMail(Mail mail) throws MessagingException { checkMail(mail); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(sender); helper.setTo(mail.getReceiver()); helper.setSubject(mail.getSubject()); helper.setText(mail.getText()); File file = new File(mail.getFilePath()); helper.addAttachment(file.getName(), file); mailSender.send(mimeMessage); saveMail(mail); }

发送模版邮件

public void sendTemplateMail(Mail mail) throws MessagingException { checkMail(mail); // templateEngine 替换掉动态参数,生产出最后的html String emailContent = templateEngine.process(mail.getEmailTemplateName(), mail.getEmailTemplateContext()); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom(sender); helper.setTo(mail.getReceiver()); helper.setSubject(mail.getSubject()); helper.setText(emailContent, true); mailSender.send(mimeMessage); saveMail(mail); } 四、测试及优化 4.1 单元测试

测试附件邮件时,附件放在static文件夹下;

测试模版邮件时,模版放在file文件夹下。

@RunWith(SpringRunner.class) @SpringBootTest public class MailServiceTest { @Resource MailService mailService; /** * 发送纯文本邮件 */ @Test public void sendSimpleMail() { Mail mail = new Mail(); // mail.setReceiver("17098705205@163.com"); mail.setReceiver("van93@qq.com"); mail.setSubject("测试简单邮件"); mail.setText("测试简单内容"); mailService.sendSimpleMail(mail); } /** * 发送邮件并携带附件 */ @Test public void sendAttachmentsMail() throws MessagingException { Mail mail = new Mail(); // mail.setReceiver("17098705205@163.com"); mail.setReceiver("van93@qq.com"); mail.setSubject("测试附件邮件"); mail.setText("附件邮件内容"); mail.setFilePath("file/dusty_blog.jpg"); mailService.sendAttachmentsMail(mail); } /** * 测试模版邮件邮件 */ @Test public void sendTemplateMail() throws MessagingException { Mail mail = new Mail(); // mail.setReceiver("17098705205@163.com"); mail.setReceiver("van93@qq.com"); mail.setSubject("测试模版邮件邮件"); //创建模版正文 Context context = new Context(); // 设置模版需要更换的参数 context.setVariable("verifyCode", "6666"); mail.setEmailTemplateContext(context); // 模版名称(模版位置位于templates目录下) mail.setEmailTemplateName("emailTemplate"); mailService.sendTemplateMail(mail); } } 4.2 优化

因为平时发送邮件还有抄送/密送等需求,这里,封装一个实体和工具类,便于直接调用邮件服务。

邮件信息类

@Data public class MailDomain { /** * 邮件id */ private String id; /** * 邮件发送人 */ private String sender; /** * 邮件接收人 (多个邮箱则用逗号","隔开) */ private String receiver; /** * 邮件主题 */ private String subject; /** * 邮件内容 */ private String text; /** * 抄送(多个邮箱则用逗号","隔开) */ private String cc; /** * 密送(多个邮箱则用逗号","隔开) */ private String bcc; /** * 附件/文件地址 */ private String filePath; /** * 附件/文件名称 */ private String fileName; /** * 是否有附件(默认没有) */ private Boolean isTemplate = false; /** * 模版名称 */ private String emailTemplateName; /** * 模版内容 */ private Context emailTemplateContext; /** * 发送时间(可指定未来发送时间) */ private Date sentDate; }

邮件工具类

@Component public class EmailUtil { @Resource private JavaMailSender mailSender; @Resource TemplateEngine templateEngine; @Value("${spring.mail.username}") private String sender; /** * 构建复杂邮件信息类 * @param mail * @throws MessagingException */ public void sendMail(MailDomain mail) throws MessagingException { //true表示支持复杂类型 MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true); //邮件发信人从配置项读取 mail.setSender(sender); //邮件发信人 messageHelper.setFrom(mail.getSender()); //邮件收信人 messageHelper.setTo(mail.getReceiver().split(",")); //邮件主题 messageHelper.setSubject(mail.getSubject()); //邮件内容 if (mail.getIsTemplate()) { // templateEngine 替换掉动态参数,生产出最后的html String emailContent = templateEngine.process(mail.getEmailTemplateName(), mail.getEmailTemplateContext()); messageHelper.setText(emailContent, true); }else { messageHelper.setText(mail.getText()); } //抄送 if (!StringUtils.isEmpty(mail.getCc())) { messageHelper.setCc(mail.getCc().split(",")); } //密送 if (!StringUtils.isEmpty(mail.getBcc())) { messageHelper.setCc(mail.getBcc().split(",")); } //添加邮件附件 if (mail.getFilePath() != null) { File file = new File(mail.getFilePath()); messageHelper.addAttachment(file.getName(), file); } //发送时间 if (StringUtils.isEmpty(mail.getSentDate())) { messageHelper.setSentDate(mail.getSentDate()); } //正式发送邮件 mailSender.send(messageHelper.getMimeMessage()); } /** * 检测邮件信息类 * @param mail */ private void checkMail(MailDomain mail) { if (StringUtils.isEmpty(mail.getReceiver())) { throw new RuntimeException("邮件收信人不能为空"); } if (StringUtils.isEmpty(mail.getSubject())) { throw new RuntimeException("邮件主题不能为空"); } if (StringUtils.isEmpty(mail.getText()) && null == mail.getEmailTemplateContext()) { throw new RuntimeException("邮件内容不能为空"); } } /** * 将邮件保存到数据库 * @param mail * @return */ private MailDomain saveMail(MailDomain mail) { // todo 发送成功/失败将邮件信息同步到数据库 return mail; } }

具体的测试详见Github 示例代码,这里就不贴出来了。

五、 总结及延伸 5.1 异步发送

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygpyj.html