SimpleMailMessage使用outlook/office365 发送邮件

SimpleMailMessage使用outlook/office365 发送邮件

配置文件

# 邮箱配置
spring.mail.host=smtp.office365.com
spring.mail.port=587
spring.mail.username=xxxxx@outlook.com
spring.mail.password=xxxxxxx
spring.mail.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.socketFactory.class=com.sun.mail.util.MailSSLSocketFactory
spring.mail.properties.mail.smtp.ssl.socketFactory.fallback=false
spring.mail.default-encoding=UTF-8

使用

package com.xxx.xxx;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
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;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class DepotTest {

    @Autowired
    private JavaMailSender javaMailSender;

    @Test
    public void testSend() {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("xxxx@outlook.com"); //发送者邮箱地址
        message.setTo("xxxx@126.com"); //收件人邮箱地址
        message.setSubject("测试主题");
        message.setText("测试内容");
        javaMailSender.send(message);
    }

}
上一篇:SpringBoot-邮件任务


下一篇:java向oracle数据库Clob读取,写入数据