@Test public void sendHTMLMail() throws Exception { MailBean mailBean = new MailBean(); mailBean.setRecipient("15079150591@163.com"); mailBean.setSubject("测试SpringBootMailHTML"); mailUtil.sendHTMLMail(mailBean); }
public void sendHTMLMail(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()); StringBuilder sb = new StringBuilder(); sb.append("<h1>SpirngBoot测试邮件HTML</h1>") .append("\"<p style='color:#F00'>你是真的太棒了！</p>") .append("<p style='text-align:right'>右对齐</p>"); mimeMessageHelper.setText(sb.toString(), true); File file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "image" + Constants.SF_FILE_SEPARATOR + "mail.png"); mimeMessageHelper.addInline("springcloud", file); file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "file" + Constants.SF_FILE_SEPARATOR + "iee.zip"); mimeMessageHelper.addAttachment("iee", file); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } }
MailUtil { public void sendHTMLMail(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()); StringBuilder sb = new StringBuilder(); sb.append("<h1>SpirngBoot测试邮件HTML</h1>") .append("\"<p style='color:#F00'>你是真的太棒了！</p>") .append("<p style='text-align:right'>右对齐</p>"); mimeMessageHelper.setText(sb.toString(), true); File file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "image" + Constants.SF_FILE_SEPARATOR + "mail.png"); mimeMessageHelper.addInline("springcloud", file); file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "file" + Constants.SF_FILE_SEPARATOR + "iee.zip"); mimeMessageHelper.addAttachment("iee", file); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } } }
MailUtil { public void sendHTMLMail(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()); StringBuilder sb = new StringBuilder(); sb.append("<h1>SpirngBoot测试邮件HTML</h1>") .append("\"<p style='color:#F00'>你是真的太棒了！</p>") .append("<p style='text-align:right'>右对齐</p>"); mimeMessageHelper.setText(sb.toString(), true); File file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "image" + Constants.SF_FILE_SEPARATOR + "mail.png"); mimeMessageHelper.addInline("springcloud", file); file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "file" + Constants.SF_FILE_SEPARATOR + "iee.zip"); mimeMessageHelper.addAttachment("iee", file); javaMailSender.send(mimeMailMessage); } catch (Exception e) { logger.error("邮件发送失败", e.getMessage()); } }  }
MailUtil { public void sendHTMLMail(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()); StringBuilder sb = new StringBuilder(); sb.append("<h1>SpirngBoot测试邮件HTML</h1>") .append("\"<p style='color:#F00'>你是真的太棒了！</p>") .append("<p style='text-align:right'>右对齐</p>"); mimeMessageHelper.setText(sb.toString(), true); File file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "image" + Constants.SF_FILE_SEPARATOR + "mail.png"); mimeMessageHelper.addInline("springcloud", file); file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "file" + Constants.SF_FILE_SEPARATOR + "iee.zip"); mimeMessageHelper.addAttachment("iee", file); 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 sendHTMLMail(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()); StringBuilder sb = new StringBuilder(); sb.append("<h1>SpirngBoot测试邮件HTML</h1>") .append("\"<p style='color:#F00'>你是真的太棒了！</p>") .append("<p style='text-align:right'>右对齐</p>"); mimeMessageHelper.setText(sb.toString(), true); File file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "image" + Constants.SF_FILE_SEPARATOR + "mail.png"); mimeMessageHelper.addInline("springcloud", file); file = ResourceUtils.getFile("classpath:static" + Constants.SF_FILE_SEPARATOR + "file" + Constants.SF_FILE_SEPARATOR + "iee.zip"); mimeMessageHelper.addAttachment("iee", file); 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);  }