1、Java本身提供了JavaMail標准以實現郵件的處理,同時用戶也可以搭建屬於自己的郵件服務器或者直接使用各個郵箱系統實現郵件的發送處理。這里使用QQ郵箱系統進行服務整合。
登錄QQ郵箱,進入郵箱設置頁面,找到郵件服務配置項,如下所示:
修改pom.xml配置文件,引入依賴庫,如下所示;
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 5 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 6 <modelVersion>4.0.0</modelVersion> 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>2.3.5.RELEASE</version> 11 <relativePath /> <!-- lookup parent from repository --> 12 </parent> 13 <groupId>com.example</groupId> 14 <artifactId>demo</artifactId> 15 <version>0.0.1-SNAPSHOT</version> 16 <name>demo</name> 17 <description>Demo project for Spring Boot</description> 18 19 <properties> 20 <java.version>1.8</java.version> 21 <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-web</artifactId> 28 </dependency> 29 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-test</artifactId> 33 <scope>test</scope> 34 <exclusions> 35 <exclusion> 36 <groupId>org.junit.vintage</groupId> 37 <artifactId>junit-vintage-engine</artifactId> 38 </exclusion> 39 </exclusions> 40 </dependency> 41 42 <!-- mysql驅動包 --> 43 <dependency> 44 <groupId>mysql</groupId> 45 <artifactId>mysql-connector-java</artifactId> 46 </dependency> 47 48 <!-- druid連接池 --> 49 <dependency> 50 <groupId>com.alibaba</groupId> 51 <artifactId>druid</artifactId> 52 <version>1.1.10</version> 53 </dependency> 54 55 <dependency> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-starter-data-jpa</artifactId> 58 </dependency> 59 <dependency> 60 <groupId>org.springframework.boot</groupId> 61 <artifactId>spring-boot-starter-cache</artifactId> 62 </dependency> 63 <dependency> 64 <groupId>org.hibernate</groupId> 65 <artifactId>hibernate-ehcache</artifactId> 66 </dependency> 67 68 <!-- activeMQ --> 69 <dependency> 70 <groupId>org.springframework.boot</groupId> 71 <artifactId>spring-boot-starter-activemq</artifactId> 72 </dependency> 73 74 <!-- rabbitMQ --> 75 <dependency> 76 <groupId>org.springframework.boot</groupId> 77 <artifactId>spring-boot-starter-amqp</artifactId> 78 </dependency> 79 80 <!-- kafka --> 81 <dependency> 82 <groupId>org.springframework.kafka</groupId> 83 <artifactId>spring-kafka</artifactId> 84 </dependency> 85 86 <dependency> 87 <groupId>org.springframework.boot</groupId> 88 <artifactId>spring-boot-starter-mail</artifactId> 89 </dependency> 90 </dependencies> 91 92 <build> 93 <plugins> 94 <plugin> 95 <groupId>org.springframework.boot</groupId> 96 <artifactId>spring-boot-maven-plugin</artifactId> 97 </plugin> 98 </plugins> 99 <resources> 100 <resource> 101 <directory>src/main/resources</directory> 102 <includes> 103 <include>**/*.properties</include> 104 <include>**/*.yml</include> 105 <include>**/*.xml</include> 106 <include>**/*.p12</include> 107 <include>**/*.html</include> 108 <include>**/*.jpg</include> 109 <include>**/*.png</include> 110 </includes> 111 </resource> 112 </resources> 113 </build> 114 115 </project>
修改application.yml配置文件,實現郵件配置,如下所示:
1 # 郵箱服務器 2 spring.mail.host=smtp.qq.com 3 # 用戶名 4 spring.mail.username=xxx@qq.com 5 # 授權碼 6 spring.mail.password=xxxx 7 # stmp授權開啟 8 spring.mail.properties.mail.smtp.auth=true 9 # 啟動tls服務 10 spring.mail.properties.mail.smtp.starttls.enable=true 11 # 啟動tls支持 12 spring.mail.properties.mail.smtp.starttls.required=true
由於SpringBoot中已經進行了大量的簡化配置,所以此時的程序只需要注入JavaMailSender對象,並設置好郵件內容,就可以實現郵件信息的發送。
1 package com.demo.controller; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.mail.SimpleMailMessage; 5 import org.springframework.mail.javamail.JavaMailSender; 6 import org.springframework.stereotype.Controller; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.ResponseBody; 9 10 @Controller 11 public class SpringBootController { 12 13 @Autowired 14 private JavaMailSender javaMailSender; 15 16 @RequestMapping(value = "/sendMail") 17 @ResponseBody 18 public String sendMail() { 19 // 要發送的消息內容 20 SimpleMailMessage message = new SimpleMailMessage(); 21 // 發送者 22 message.setFrom("xxx@qq.com"); 23 // 接收者 24 message.setTo("xxx@qq.com"); 25 // 郵件主題 26 message.setSubject("感謝主題"); 27 // 郵件內容 28 message.setText("感謝內容"); 29 30 // 發送郵件 31 this.javaMailSender.send(message); 32 return "success"; 33 } 34 35 }