Spring Boot发送邮件详解
2018-08-17 17:14
246 查看
相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送,在Spring Boot的Starter模块中也为此提供了自动化配置。下面通过实例来讲解如何在Spring Boot中使用JavaMailSender发送邮件。
一、引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、application.properties配置
##端口号 server.port=8888 ##根据自己的情况填写 ##邮箱服务器地址 ##QQ smtp.qq.com ##sina smtp.sina.cn ##aliyun smtp.aliyun.com ##163 smtp.163.com spring.mail.host=smtp.qq.com ##邮箱用户名 spring.mail.username=949118693@qq.com ##邮箱密码(注意:QQ邮箱应该使用授权码) spring.mail.password=**************** ##编码格式 spring.mail.default-encoding=UTF-8 ##发送邮件地址 mail.fromMail.sender=949118693@qq.com ##接收邮件地址 mail.fromMail.receiver=1415954494@qq.com
三、邮件发送控制器
package com.xieke.test.demo; import java.io.File; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 邮件发送控制器 * * @author 邪客 * */ @RestController public class MailController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${mail.fromMail.sender}") private String sender;// 发送者 @Value("${mail.fromMail.receiver}") private String receiver;// 接受者 @Autowired private JavaMailSender javaMailSender; /** * @Description http://localhost:8888/sendMail * @method 发送文本邮件 * @return */ @RequestMapping("/sendMail") public String sendMail() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(sender); message.setTo(receiver); message.setSubject("邪客发送文本邮件测试");// 标题 message.setText("世界,你好!--->文本邮件");// 内容 try { javaMailSender.send(message); logger.info("文本邮件发送成功!"); return "success"; } catch (Exception e) { logger.error("文本邮件发送异常!", e); return "failure"; } } /** * @Description http://localhost:8888/sendHtmlMail * @method 发送html邮件 * @return */ @RequestMapping("/sendHtmlMail") public String testHtmlMail() { String content = "<html><body><h3>hello world ! --->Html邮件</h3></body></html>"; MimeMessage message = javaMailSender.createMimeMessage(); try { //true表示需要创建一个multipart message MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("邪客发送Html邮件测试"); helper.setText(content, true); javaMailSender.send(message); logger.info("Html邮件发送成功!"); return "success"; } catch (MessagingException e) { logger.error("Html邮件发送异常!", e); return "failure"; } } /** * @Description http://localhost:8888/sendFilesMail * @method 发送附件邮件 * @return */ @RequestMapping("/sendFilesMail") public String sendFilesMail() { String filePath = "F:\\select_from_user.sql"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("邪客发送附件邮件测试"); helper.setText("一封带附件的邮件", true); FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = filePath.substring(filePath.lastIndexOf(File.separator)); helper.addAttachment(fileName, file); javaMailSender.send(message); logger.info("附件邮件发送成功!"); return "success"; } catch (MessagingException e) { logger.error("附件邮件发送异常!", e); return "failure"; } } /** * @Description http://localhost:8888/sendInlineResourceMail * @method 发送图片邮件 * @return */ @RequestMapping("/sendInlineResourceMail") public String sendInlineResourceMail() { String id = "xieke90"; // cid:内嵌资源 String content = "<html><body>带有图片的邮件:<img src=\'cid:" + id + "\'></body></html>"; String imgPath = "F:\\spring-cloud.png"; MimeMessage message = javaMailSender.createMimeMessage(); try { MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setFrom(sender); helper.setTo(receiver); helper.setSubject("邪客发送图片邮件测试"); helper.setText(content, true); FileSystemResource res = new FileSystemResource(new File(imgPath)); helper.addInline(id, res); javaMailSender.send(message); logger.info("图片邮件发送成功!"); return "success"; } catch (MessagingException e) { logger.error("图片邮件发送异常!", e); return "failure"; } } }
四、分别测试文本、Html、附件、图片邮件发送
<1>、访问http://localhost:8888/sendMail,测试结果如下:
<2>、访问http://localhost:8888/sendHtmlMail,测试结果如下:
<3>、访问http://localhost:8888/sendFilesMail,测试结果如下:
<4>、访问http://localhost:8888/sendInlineResourceMail,测试结果如下:
代码已上传至码云:https://gitee.com/xieke90/spring_boot_demo (springboot-mail-demo)
转载请注明出处: http://xieke90.iteye.comhttps://blog.csdn.net/blog/2428892
相关文章推荐
- Spring Boot发送邮件详解
- spring-boot实战【12】:Spring Boot中使用JavaMailSender发送邮件
- Spring Boot发送qq邮件
- SpringBoot 2.x 集成QQ邮箱、网易系邮箱、Gmail邮箱发送邮件
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
- yx: SpringBoot发送邮件功能
- SpringBoot添加Email发送功能及常见异常详解
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
- Springboot JavaMailSender发送邮件(QQ和163)
- SpringBoot(十一):SpringBoot发送邮件
- (十八)SpringBoot之发送QQ邮件
- SpringBoot-Thymeleaf发送模板带附件邮件
- Spring Boot系列(十三) Spring Boot 发送邮件
- SpringBoot(十一):SpringBoot发送邮件
- 使用job自动抓取网页数据发送邮件,以springboot服务启动
- springboot中发送各种类型的邮件
- spring boot 发送邮件
- 第五章:SpringBoot集成spring-boot-starter-mail发送邮件
- springboot发送邮件
- spring-boot 速成(10) -【个人邮箱/企业邮箱】发送邮件