SpringBoot实现发送邮件功能过程图解
首先创建一个邮箱账号,建议@126.com,@163.com,@qq.com都可以
开启smtp,以下是使用图解:
创建SpringBoot项目导入依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-mail
application.properties文件中配置:
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.163.com
#发送者的邮箱密码
spring.mail.password=xxxxx
#端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#发送者的邮箱账号
spring.mail.username=xxxxxxx@163.com
server.port=8081
以文本的形式发送:
packagecom.example.demo;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.mail.SimpleMailMessage;
importorg.springframework.mail.javamail.JavaMailSender;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.RestController;
/**
*@author
*@site
*@company
*@create2020-03-071:06
*/
@RestController
publicclassMailController{
@Autowired
JavaMailSenderjsm;
@Value("${spring.mail.username}")
privateStringusername;
@GetMapping("/send")
publicStringsend(){
//建立邮箱消息
SimpleMailMessagemessage=newSimpleMailMessage();
//发送者
message.setFrom(username);
//接收者
message.setTo("1352192872@qq.com");
//发送标题
message.setSubject("测试");
//发送内容
message.setText("测试数据");
jsm.send(message);
return"1";
}
}
结果:
发送方:
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。