本文实例为大家分享了SpringBoot JavaMailSender发送邮件的具体代码,供大家参考,具体内容如下
创新互联于2013年创立,先为色尼等服务建站,色尼等地企业,进行企业商务咨询服务。为色尼企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
引入Maven依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
163邮箱
application.properties
#####163邮箱######## spring.mail.host=smtp.163.com spring.mail.username=*****@163.com #163邮箱密码 spring.mail.password=!@#$%^&* spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
运行类:
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes=Application.class) public class My163MailTest { @Autowired private JavaMailSender javaMailSender; @Value("${spring.mail.username}") private String username; @Test public void testSendSimple() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(username); message.setTo("*******@qq.com"); message.setSubject("标题:测试标题"); message.setText("测试内容部份"); javaMailSender.send(message); } }
QQ邮箱和163邮箱的区别是需要设置授权码而不是密码,具体操作参考: 地址
application.properties
######qq邮箱######## spring.mail.host=smtp.qq.com spring.mail.username=******@qq.com #QQ邮箱授权码 spring.mail.password=xuojxtkdojvzbhjj spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true
运行类:
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes=Application.class) public class MyQQMailTest { @Autowired private JavaMailSender javaMailSender; @Value("${spring.mail.username}") private String username; @Test public void testSendSimple() { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(username); message.setTo("******@qq.com"); message.setSubject("标题:测试标题"); message.setText("测试内容部份"); javaMailSender.send(message); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
分享题目:SpringBootJavaMailSender发送邮件功能
地址分享:https://www.cdcxhl.com/article28/gooccp.html
成都网站建设公司_创新互联,为您提供网站设计公司、微信小程序、网站内链、网站改版、品牌网站建设、企业网站制作
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联