SpringBoot向outlook發送郵件

首先要登錄outlook郵箱,點擊設置滑到最下面選擇完整設置java

進入後選擇郵件->同步電子郵件web

打開pop如上設置spring

下面是個人application.propertis設置apache

請填上本身的郵箱名與密碼,outlook郵箱目前不須要受權碼,密碼就是本身的郵箱密碼springboot

記得按下面開啓tls驗證,否則會顯示匿名用戶沒法經過驗證app

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代碼:maven

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文件:spring-boot

 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>

 運行成功:測試

相關文章
相關標籤/搜索