SpringBoot 2.X从0到1实现邮件发送功能
spring中提供了javamailsender接口实现邮件发送功能,在springboot2.x中也封装了发送邮件相关的starter并且提供了自动化配置。
本文目录
一、添加对应的starter2.无法注入javamailsender问题
一、添加对应的starter
pom.xml中添加下面依赖
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-mail</artifactid>
</dependency>
二、添加发送邮件相关的配置
application.properties中添加邮件配置
spring.mail.host = smtp.163.com
spring.mail.port = 465
spring.mail.username = xxx@163.com
spring.mail.password = xxx
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.timeout = 25000
spring.mail.properties.mail.smtp.socketfactory.class = javax.net.ssl.sslsocketfactory
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.starttls.required = true
三、实现发送邮件功能
本例中封装了一个mailserviceimpl,里面注入javamailsender即可,具体代码如下
mailserviceimpl.java部分代码:
/**
* 发送邮件
*
* @author: java碎碎念
*/
public void sendmail(email email) {
long start = system.currenttimemillis();
try {
mimemessage mimemessage = javamailsender.createmimemessage();
//true表示需要创建一个multipart message
mimemessagehelper helper = new mimemessagehelper(mimemessage, true, "utf-8");
helper.setfrom(mailusername);
helper.setto(email.gettoaddress().toarray(new string[email.gettoaddress().size()]));
helper.setsubject(email.getsubject());
helper.settext(email.getcontent(), true);
if (null != email.getattachments() && email.getattachments().size() > 0) {
for (file curfile : email.getattachments()) {
filesystemresource file = new filesystemresource(curfile);
helper.addattachment(mimeutility.encodeword(file.getfilename(), "utf-8", "b"), file);
}
}
log.info("邮件开始发送");
javamailsender.send(mimemessage);
long sendmilltimes = system.currenttimemillis() - start;
log.info("邮件发送成功,sendtimes=" + sendmilltimes);
} catch (exception e) {
log.error("发送html邮件时发生异常!", e);
}
}
mailcontroller.java部分代码:
@requestmapping(value = "/sendmail")
public string sendemail() {
email email_email = new email();
list<string> addresslist = new arraylist<string>();
addresslist.add("xxx@qq.com");
email_email.settoaddress(addresslist);
email_email.setsubject("java碎碎念-主题测试");// 主题
email_email.setcontent("你好!<br><br> 测试邮件发送成功!");
// 发送邮件
mailservice.sendmail(email_email);
return "ok";
}
项目启动成功后访问http://localhost:8080/sendmail即可成功发送邮件,收到邮件截图如下:
收到邮件截图
到此发送邮件功能全部实现,有问题欢迎留言沟通哦!
完整源码地址: https://github.com/suisui2019/springboot-study
四、实现过程中踩过的坑
1.中文附件乱码问题
java mail发邮件是附件名过长默认会被截断显示乱码,在程序启动时设置主动设置为false可正常显示,具体设置代码如下
public static void main(string[] args) {
//java mail发邮件时附件名过长默认会被截断,附件名显示【tcmime.29121.29517.50430.bin】,主动设为false可正常显示附件名
system.setproperty("mail.mime.splitlongparameters", "false");
springapplication.run(sendmailapplication.class, args);
}
2.无法注入javamailsender问题
在application.properties中配置spring.mail.host即可。
推荐阅读
1.spring boot入门-快速搭建web项目
2.spring boot 2.x 整合redis
3.spring boot 2.x 如何优雅的解决跨域问题?
4.spring boot 2.x 如何添加拦截器?
5.spring boot 2.x 集成spring session实现session共享
6.redis cluster搭建高可用redis服务器集群
7.为什么单线程的redis这么快?
8.一篇文章搞定springmvc参数绑定
9.springmvc+mybatis 如何配置多个数据源并切换?
10.spring条件注解@conditional
java碎碎念公众号限时领取免费java相关资料,涵盖了java、redis、mongodb、mysql、zookeeper、spring cloud、dubbo/kafka、hadoop、hbase、flink等高并发分布式、大数据、机器学习等技术。
关注下方公众号即可免费领取: