欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

SpringBoot JavaMailSender发送邮件功能

时间:2021-03-01 13:43:17|栏目:JAVA代码|点击:

本文实例为大家分享了SpringBoot JavaMailSender发送邮件的具体代码,供大家参考,具体内容如下

引入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);
 }
}

上一篇:Mybatis中Mapper映射文件使用详解

栏    目:JAVA代码

下一篇:Java用BigDecimal类解决Double类型精度丢失的问题

本文标题:SpringBoot JavaMailSender发送邮件功能

本文地址:http://www.codeinn.net/misctech/72185.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有