springboot如何使用outlook傳送郵件

jiutainguixiao發表於2020-10-30

首先要登陸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

相關文章