SpringBoot整合Email

半兽人 发表于: 2020-01-14   最后更新时间: 2021-05-13 16:30:40  
{{totalSubscript}} 订阅, 1,988 游览

step 1:引入SpringBoot-email依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

step 2:配置email基本信息

spring.mail.host=smtp.orchome.com
spring.mail.username=*******@orchome.com
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

step 3:发送简单邮件

import org.dreams.forepart.Application;
import org.dreams.forepart.test.BaseSpringTest;
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.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class},webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EmailTest{

    @Autowired
    private JavaMailSender mailSender;

    @Test
    public void test() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("*******@orchome.com");
        message.setTo("*******@qq.com");
        message.setSubject("主题:简单邮件");
        message.setText("测试邮件内容");
        mailSender.send(message);
    }
}
更新于 2021-05-13

查看SpringBoot更多相关的文章或提一个关于SpringBoot的问题,也可以与我们一起分享文章