- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 修改application.yml配置文件
spring:
mail:
default-encoding: UTF-8
host: smtp.163.com
port: 465
username: 用户名
password: 密码
properties:
mail.smtp.ssl.enable: true
- 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String username;
@Test
public void testMail() {
//建立邮件消息
SimpleMailMessage mailMessage = new SimpleMailMessage();
//发送者
mailMessage.setFrom(username);
//接收者
mailMessage.setTo("dean.lee@aliyun.com");
//发送的标题
mailMessage.setSubject("主题");
//发送的内容
mailMessage.setText("内容");
//发送邮件
mailSender.send(mailMessage);
}
}
作者公众号