@Test public void sendTemplateMail() throws Exception { MailBean mailBean = new MailBean(); mailBean.setRecipient("15079150591@163.com"); mailBean.setSubject("测试SpringBoot发送带FreeMarker邮件"); mailBean.setContent("希望大家能够学到自己想要的东西，谢谢各位的帮助！！！"); mailUtil.sendTemplateMail(mailBean); }
public void sendTemplateMail(MailBean mailBean) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); mimeMessageHelper.setFrom(MAIL_SENDER); mimeMessageHelper.setTo(mailBean.getRecipient()); mimeMessageHelper.setSubject(mailBean.getSubject()); Map<String, Object> model = new HashMap<String, Object>(); model.put("content", mailBean.getContent()); model.put("title", "标题Mail中使用了FreeMarker"); Template template = configuration.getTemplate("mail.ftl"); String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); mimeMessageHelper.setText(text, true); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } }
MailUtil { public void sendTemplateMail(MailBean mailBean) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); mimeMessageHelper.setFrom(MAIL_SENDER); mimeMessageHelper.setTo(mailBean.getRecipient()); mimeMessageHelper.setSubject(mailBean.getSubject()); Map<String, Object> model = new HashMap<String, Object>(); model.put("content", mailBean.getContent()); model.put("title", "标题Mail中使用了FreeMarker"); Template template = configuration.getTemplate("mail.ftl"); String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); mimeMessageHelper.setText(text, true); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } } }
MailUtil { public void sendTemplateMail(MailBean mailBean) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); mimeMessageHelper.setFrom(MAIL_SENDER); mimeMessageHelper.setTo(mailBean.getRecipient()); mimeMessageHelper.setSubject(mailBean.getSubject()); Map<String, Object> model = new HashMap<String, Object>(); model.put("content", mailBean.getContent()); model.put("title", "标题Mail中使用了FreeMarker"); Template template = configuration.getTemplate("mail.ftl"); String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); mimeMessageHelper.setText(text, true); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } }  }
MailUtil { public void sendTemplateMail(MailBean mailBean) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); mimeMessageHelper.setFrom(MAIL_SENDER); mimeMessageHelper.setTo(mailBean.getRecipient()); mimeMessageHelper.setSubject(mailBean.getSubject()); Map<String, Object> model = new HashMap<String, Object>(); model.put("content", mailBean.getContent()); model.put("title", "标题Mail中使用了FreeMarker"); Template template = configuration.getTemplate("mail.ftl"); String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); mimeMessageHelper.setText(text, true); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } }  void sendSimpleMail(MailBean mailBean); void sendHTMLMail(MailBean mailBean); void sendAttachmentMail(MailBean mailBean); void sendInlineMail(MailBean mailBean); void sendTemplateMail(MailBean mailBean); void sendMailByQueue(MailBean mailBean); }
MailUtil { public void sendTemplateMail(MailBean mailBean) { MimeMessage mimeMailMessage = null; try { mimeMailMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true); mimeMessageHelper.setFrom(MAIL_SENDER); mimeMessageHelper.setTo(mailBean.getRecipient()); mimeMessageHelper.setSubject(mailBean.getSubject()); Map<String, Object> model = new HashMap<String, Object>(); model.put("content", mailBean.getContent()); model.put("title", "标题Mail中使用了FreeMarker"); Template template = configuration.getTemplate("mail.ftl"); String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model); mimeMessageHelper.setText(text, true); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } }  void sendSimpleMail(MailBean mailBean); void sendHTMLMail(MailBean mailBean); void sendAttachmentMail(MailBean mailBean); void sendInlineMail(MailBean mailBean); void sendTemplateMail(MailBean mailBean); void sendMailByQueue(MailBean mailBean);  }