springboot如何使用outlook发送邮件

首先要登陆outlook邮箱,点击设置滑到最下面选择完整设置

在这里插入图片描述

进入后选择邮件->同步电子邮件
在这里插入图片描述

打开pop如上设置

下面是我的application.propertis设置

请填上自己的邮箱名与密码,outlook邮箱目前不需要授权码,密码就是自己的邮箱密码

记得按下面开启tls验证,不然会显示匿名用户无法通过验证

1 spring.mail.username=xxxxxxx@outlook.com
2 spring.mail.password=xxxxxxxxx
3 spring.mail.port=587
4 spring.mail.host=smtp-mail.outlook.com
5 # 设置ssl认证
6 # spring.mail.properties.mail.smtp.ssl.enable=true
7 # 设置TLS认证
8 spring.mail.properties.mail.smtp.starttls.required=true

下方是我的java代码:

//setSubject是邮件标题
//setText是邮件内容
//setTo是发送给哪个邮箱
//setFrom是从哪个邮箱发出
复制代码
 1 import org.junit.Test;
 2 import org.junit.runner.RunWith;
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.boot.test.context.SpringBootTest;
 5 import org.springframework.mail.SimpleMailMessage;
 6 import org.springframework.mail.javamail.JavaMailSenderImpl;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 @RunWith(SpringRunner.class)
10 @SpringBootTest
11 public class Springboot04TaskApplicationTests {
12 
13     @Autowired
14     JavaMailSenderImpl mailSender;
15 
16     @Test
17     public void contextLoads() {
18         SimpleMailMessage message = new SimpleMailMessage();
19         // 邮箱设置
20         message.setSubject("通知-程序测试");
21         message.setText("正在进行程序测试");
22 
23         message.setTo("xxxxxx@163.com");
24         message.setFrom("xxxxxx@outlook.com");
25 
26         mailSender.send(message);
27     }
28 
29 }

下面是pom文件:

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5 
 6     <groupId>com.atguigu</groupId>
 7     <artifactId>springboot-04-task</artifactId>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <packaging>jar</packaging>
10 
11     <name>springboot-04-task</name>
12     <description>Demo project for Spring Boot</description>
13 
14     <parent>
15         <groupId>org.springframework.boot</groupId>
16         <artifactId>spring-boot-starter-parent</artifactId>
17         <version>1.5.15.RELEASE</version>
18         <relativePath/> <!-- lookup parent from repository -->
19     </parent>
20 
21     <properties>
22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24         <java.version>1.8</java.version>
25     </properties>
26 
27     <dependencies>
28         <dependency>
29             <groupId>org.springframework.boot</groupId>
30             <artifactId>spring-boot-starter-web</artifactId>
31         </dependency>
32 
33         <dependency>
34             <groupId>org.springframework.boot</groupId>
35             <artifactId>spring-boot-starter-mail</artifactId>
36         </dependency>
37 
38         <dependency>
39             <groupId>org.springframework.boot</groupId>
40             <artifactId>spring-boot-starter-test</artifactId>
41             <scope>test</scope>
42         </dependency>
43     </dependencies>
44 
45     <build>
46         <plugins>
47             <plugin>
48                 <groupId>org.springframework.boot</groupId>
49                 <artifactId>spring-boot-maven-plugin</artifactId>
50             </plugin>
51         </plugins>
52     </build>
53 
54 
55 </project>

复制代码
运行成功:

在这里插入图片描述

转载自 https://www.cnblogs.com/renlei-213/p/9476031.html

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页