SpringBoot 2.x 集成QQ邮箱、网易系邮箱、Gmail邮箱发送邮件 (2)

再写一个组件实现上面的接口并注入JavaMailSender

@Component public class IMailServiceImpl implements IMailService { @Autowired private JavaMailSender mailSender; @Value("${spring.mail.from}") private String from; //具体实现请继续向下阅读 } 发送文本邮件 /** * 发送文本邮件 * @param to * @param subject * @param content */ @Override public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); mailSender.send(message); } @Override public void sendSimpleMail(String to, String subject, String content, String... cc) { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setCc(cc); message.setSubject(subject); message.setText(content); mailSender.send(message); } 发送html邮件 /** * 发送HTML邮件 * @param to * @param subject * @param content */ @Override public void sendHtmlMail(String to, String subject, String content) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); mailSender.send(message); }

省略实现带有抄送方法的实现

发送带附件的邮件 /** * 发送带附件的邮件 * @param to * @param subject * @param content * @param filePath */ public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); mailSender.send(message); }

省略实现带有抄送方法的实现

发送正文中有静态资源的邮件 /** * 发送正文中有静态资源的邮件 * @param to * @param subject * @param content * @param rscPath * @param rscId */ public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(from); helper.setTo(to); helper.setSubject(subject); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(rscPath)); helper.addInline(rscId, res); mailSender.send(message); }

省略实现带有抄送方法的实现

发送模板邮件

发送模板邮件使用的方法与发送HTML邮件的方法一致。只是发送邮件时使用到的模板引擎,这里使用的模板引擎为Thymeleaf。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

模板HTML代码如下:

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>IJPay让支付触手可及</title> <style> body { text-align: center; margin-left: auto; margin-right: auto; } #welcome { text-align: center; position: absolute; } </style> </head> <body> <div> <h3>欢迎使用 <span th:text="${project}"></span> -By <span th:text=" ${author}"></span></h3> <span th:text="${url}"></span> <div> <a href="#" th:href="@{${url}}" target="_bank"> <strong>IJPay让支付触手可及,欢迎Start支持项目发展:)</strong> </a> </div> <div> 如果对你有帮助,请任意打赏 </div> <img src="http://oscimg.oschina.net/oscnet/8e86fed2ee9571eb133096d5dc1b3cb2fc1.jpg"> </div> </body> </html>

如何使用请看测试中实现的代码。

测试 package com.javen.controller; import com.javen.email.impl.IMailServiceImpl; import com.javen.vo.JsonResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; @RestController @RequestMapping("email") public class EmailController { @Autowired private IMailServiceImpl mailService;//注入发送邮件的各种实现方法 @Autowired private TemplateEngine templateEngine;//注入模板引擎 @RequestMapping public JsonResult index(){ try { mailService.sendSimpleMail("xxx@126.com","SpringBoot Email","这是一封普通的SpringBoot测试邮件"); }catch (Exception ex){ ex.printStackTrace(); return new JsonResult(-1,"邮件发送失败!!"); } return new JsonResult(); } @RequestMapping("/htmlEmail") public JsonResult htmlEmail(){ try { mailService.sendHtmlMail(""xxx@126.com","IJPay让支付触手可及","<body style=http://www.likecs.com/\"text-align: center;margin-left: auto;margin-right: auto;\">\n" + " <div id=http://www.likecs.com/\"welcome\" style=http://www.likecs.com/\"text-align: center;position: absolute;\" >\n" +" <h3>欢迎使用IJPay -By Javen</h3>\n" +" <span>https://github.com/Javen205/IJPay</span>" + " <div\n" + " style=http://www.likecs.com/\"text-align: center; padding: 10px\"><a style=http://www.likecs.com/\"text-decoration: none;\" href=http://www.likecs.com/\"https://github.com/Javen205/IJPay\" target=http://www.likecs.com/\"_bank\" ><strong>IJPay 让支付触手可及,欢迎Start支持项目发展:)</strong></a></div>\n" + " <div\n" + " style=http://www.likecs.com/\"text-align: center; padding: 4px\">如果对你有帮助,请任意打赏</div>\n" + " <img width=http://www.likecs.com/\"180px\" height=http://www.likecs.com/\"180px\"\n" + " src=http://www.likecs.com/\"https://javen205.gitbooks.io/ijpay/content/assets/wxpay.png\">\n" + " </div>\n" + "</body>"); }catch (Exception ex){ ex.printStackTrace(); return new JsonResult(-1,"邮件发送失败!!"); } return new JsonResult(); } @RequestMapping("/attachmentsMail") public JsonResult attachmentsMail(){ try { String filePath = "/Users/Javen/Desktop/IJPay.png"; mailService.sendAttachmentsMail("xxx@126.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", filePath); }catch (Exception ex){ ex.printStackTrace(); return new JsonResult(-1,"邮件发送失败!!"); } return new JsonResult(); } @RequestMapping("/resourceMail") public JsonResult resourceMail(){ try { String rscId = "IJPay"; String content = "<html><body>这是有图片的邮件<br/><img src=http://www.likecs.com/\'cid:" + rscId + "\' ></body></html>"; String imgPath = "/Users/Javen/Desktop/IJPay.png"; mailService.sendResourceMail("xxx@126.com", "这邮件中含有图片", content, imgPath, rscId); }catch (Exception ex){ ex.printStackTrace(); return new JsonResult(-1,"邮件发送失败!!"); } return new JsonResult(); } @RequestMapping("/templateMail") public JsonResult templateMail(){ try { Context context = new Context(); context.setVariable("project", "IJPay"); context.setVariable("author", "Javen"); context.setVariable("url", "https://github.com/Javen205/IJPay"); String emailContent = templateEngine.process("emailTemp", context); mailService.sendHtmlMail("xxx@126.com", "这是模板邮件", emailContent); }catch (Exception ex){ ex.printStackTrace(); return new JsonResult(-1,"邮件发送失败!!"); } return new JsonResult(); } } 效果图

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

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