首页 > 技术文章 > springboot 测试发送邮件

cx-code 2018-04-02 09:52 原文

首先在pom文件引入依赖:

<!--email依赖 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

 

配置 文件:

spring.mail.host=smtp.163.com //本人 用的是163邮箱
spring.mail.username=****@163.com //邮箱地址
spring.mail.password=授权码
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

注意:邮箱要开启pop3,smtp服务,获取授权码

 

 写个简单的测试类:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootmailApplicationTests {
@Autowired
private JavaMailSender mailSender;
@Test
public void sendSimpleMail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("****@163.com");    //发送方
message.setTo("***@qq.com");      //目标
message.setSubject("主题:简单邮件");  
message.setText("测试邮件内容");
mailSender.send(message);
}


}

以上是发送邮件的小测试。

推荐阅读