Spring提供了非常好用的JavaMailSender接口实现邮件发送,在SpringBoot中也提供了相应的自动化配置。
发送邮件
1,在pom.xml中引入spring-boot-starter-mail依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2,在application.properties中配置相应的属性:(偶这里模拟的是163邮箱给QQ邮箱发送邮件)
spring.mail.host=smtp.163.com
spring.mail.username=邮箱用户名so****@163.com
spring.mail.password=邮箱密码
spring.mail.default-encoding=UTF-8
3,写发送邮件的测试类
@RestController
@RequestMapping(“/mail”)
publicclassMailController{
privatefinalLoggerlogger=LoggerFactory.getLogger(this.getClass());
@Autowired
privateJavaMailSendermailSender;
@RequestMapping(“/send”)
publicvoidsendMail(){
SimpleMailMessagemessage=newSimpleMailMessage();
message.setFrom(“so***@163.com”);
message.setTo(“239***@qq.com”);
message.setSubject(“itisatestforspringboot”);
message.setText(“你好,偶是小黄,偶正在测试发送邮件。”);
try{
mailSender.send(message);
logger.info(“小黄的测试邮件已发送。”);
}catch(Exceptione){
logger.error(“小黄发送邮件时发生异常了!”,e);
}
}
}
4,运行启动类
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
@EnableAutoConfiguration
@MapperScan(“cn.yideng.*.dao”)
publicclassDemoApplication{
publicstaticvoidmain(String[]args){
SpringApplication.run(DemoApplication.class,args);
}
}
5,浏览器运行http://localhost:8080/mail/send
6,登录163邮箱,在发送箱里查看
7,登录QQ邮箱,在收件箱里查看
可以看出SpringBoot的starter模块提供了自动化配置,在引入了spring-boot-starter-mail依赖之后,会根据配置文件中的内容去创建JavaMailSender实例,因此大家可以直接在需要使用的地方直接@Autowired来引入JavaMailSender邮件发送对象。
当然在实际使用过程中,不会这么简单的,大家可能会要求带上附件、或使用邮件模块等。这时大家就需要使用MimeMessage来设置更复杂的右键内容,下面就来看看怎么实现它。
发送带附件的邮件
测试类:还是模拟163邮箱给QQ邮箱发送邮件
@RestController
@RequestMapping(“/mail”)
publicclassAttachmentsMailController{
privatefinalLoggerlogger=LoggerFactory.getLogger(this.getClass());
@Autowired
privateJavaMailSendermailSender;
@RequestMapping(“/att”)
publicvoidsendMail()throwsMessagingException{
MimeMessagemimeMessage=mailSender.createMimeMessage();
MimeMessageHelperhelper=newMimeMessageHelper(mimeMessage,true);
helper.setFrom(“so***@163.com”);
helper.setTo(“239**@qq.com”);
helper.setSubject(“主题:发送有附件的邮件”);
helper.setText(“你好,偶是小黄,偶正在测试发送一封有附件的邮件。”);
FileSystemResourcefile1=newFileSystemResource(newFile(“d:\\cat.jpg”));
FileSystemResourcefile2=newFileSystemResource(newFile(“d:\\java-1.jpg”));
helper.addAttachment(“附件-1.jpg”,file1);
helper.addAttachment(“附件-2.jpg”,file2);
try{
mailSender.send(mimeMessage);
logger.info(“小黄的测试带附件的邮件已发送。”);
}catch(Exceptione){
logger.error(“小黄发送带附件邮件时发生异常了!”,e);
}
}
}
2,需要在D盘下准备两张图片cat.jpgjava-1.jpg
3,浏览器运行http://localhost:8080/mail/att
4,登录163邮箱,在发送箱里查看,如下图
5,登录QQ邮箱,在收件箱里查看,如下图
嵌入静态资源的邮件
还有一种是通过嵌入图片等静态资源,可以直接看到图片,而不用从附近中查看具体的图片,来看看吧。
测试类:
@RestController
@RequestMapping(“/mail”)
publicclassStaticResourceMailController{
privatefinalLoggerlogger=LoggerFactory.getLogger(this.getClass());
@Autowired
privateJavaMailSendermailSender;
@RequestMapping(“/static”)
publicvoidsendMail()throwsMessagingException{
MimeMessagemimeMessage=mailSender.createMimeMessage();
MimeMessageHelperhelper=newMimeMessageHelper(mimeMessage,true);
helper.setFrom(“so**@163.com”);
helper.setTo(“239***@qq.com”);
helper.setSubject(“主题:嵌入静态资源”);
helper.setText(“<html><body><imgsrc=\”cid:hello\”></body></html>”,true);
//注意addInline()中资源名称hello必须与text正文中cid:hello对应起来
FileSystemResourcefile1=newFileSystemResource(newFile(“d:\\cat.jpg”));
helper.addInline(“hello“,file1);
try{
mailSender.send(mimeMessage);
logger.info(“小黄的测试嵌入静态资源的邮件已发送。”);
}catch(Exceptione){
logger.error(“小黄发送嵌入静态资源的邮件时发生异常了!”,e);
}
}
}
要特别注意addInline()中资源名称hello必须与text正文中cid:hello对应。
2,需要在D盘下准备两张图片cat.jpg
3,浏览器运行http://localhost:8080/mail/static
4,登录163邮箱,在发送箱里查看,如下图
5,登录QQ邮箱,在收件箱里查看,如下图
好了,以上就是SpringBoot使用JavaMailSender发送邮件,谢谢。
以上供参考,如果您觉得有帮助,请帮忙点赞,转发,或者关注偶,偶是小黄,谢谢。