其他分享
首页 > 其他分享> > SimpleMailMessage使用outlook/office365 发送邮件

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

}

标签:outlook,SimpleMailMessage,spring,org,office365,import,mail,message,com
来源: https://blog.csdn.net/weixin_43372858/article/details/118695647