一.环境依赖
pom中添加邮件依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二.属性配置
能配置哪些属性详见:org.springframework.boot.autoconfigure.mail.MailProperties
application.properties
spring.mail.username=350148888@qq.com
spring.mail.password=emehwsbaunnsbifh
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true
三.代码示例
package com.miracle.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot04TaskApplicationTests {
@Autowired
JavaMailSenderImpl javaMailSender;
@Test
public void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("主题");
message.setText("内容");
message.setTo("zhao-xb@neusoft.com");
message.setFrom("350148888@qq.com");
javaMailSender.send(message);
}
}