SpringBoot整合Email

半兽人 发表于: 2020-01-14   最后更新时间: 2020-01-14  

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


您需要解锁本帖隐藏内容请: 点击这里
本帖隐藏的内容




上一条: springboot同时支持jsp和html
下一条: SpringBoot的WebSocket入门例子