spring boot如何加入mail郵件支持
這篇文章主要介紹了spring boot如何加入mail郵件支持,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
一、添加依賴
<!-- 郵件整合 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、添加mail.properties配置文件
#設置郵箱主機 spring.mail.host=smtp.qq.com #設置用戶名 spring.mail.username=xxxxxxx #設置密碼 #QQ郵箱->設置->賬戶->POP3/SMTP服務:開啟服務后會獲得QQ的授權碼 spring.mail.password=xxxxxxxxxxxxxxxx #端口 spring.mail.port=465 #協(xié)議 #spring.mail.protocol=smtp #設置是否需要認證,如果為true,那么用戶名和密碼就必須的, #如果設置false,可以不設置用戶名和密碼,當然也得看你的對接的平臺是否支持無密碼進行訪問的。 spring.mail.properties.mail.smtp.auth=true #STARTTLS[1] 是對純文本通信協(xié)議的擴展。它提供一種方式將純文本連接升級為加密連接(TLS或SSL),而不是另外使用一個端口作加密通信。 spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
三、添加MailConfig.java
package com.spring.config;
import java.io.File;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
@Configuration
public class MailConfig {
@Resource
private JavaMailSenderImpl mailSender;
@Value("${spring.mail.username}")
private String username;
/**
* 發(fā)送純文本形式的email
*
* @param toEmail 收件人郵箱
* @param title 郵件標題
* @param content 郵件內容
*/
public void sendTextMail(String toEmail, String title, String content) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(username);
msg.setTo(toEmail);
msg.setSubject(title);
msg.setText(content);
mailSender.send(msg);
}
/**
* 發(fā)送帶有html的內容
*
* @param toEmail 收件人郵箱
* @param title 郵件標題
* @param htmlContent 郵件內容
*/
public void sendHtmlMail(String toEmail, String title, String htmlContent) throws MessagingException {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");
helper.setFrom(username);
helper.setTo(toEmail);
helper.setSubject(title);
helper.setText(htmlContent, true);
mailSender.send(msg);
}
/**
* 添加附件的email發(fā)送
*
* @param toEmail 收件人地址
* @param title 郵件標題
* @param content 文本內容
* @param aboutFiles 附件信息 每個子項都是一個文件相關信息的map Map<String,String>: 1.filePath
* 2.fileName
* @throws Exception 異常
*/
public void sendAttachmentMail(String toEmail, String title, String content, List<Map<String, String>> aboutFiles) throws Exception {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
helper.setFrom(username);
helper.setTo(toEmail);
helper.setSubject(title);
helper.setText(content);
FileSystemResource resource = null;
for (Map<String, String> file : aboutFiles) {
resource = new FileSystemResource(file.get("filePath"));
if (resource.exists()) {// 是否存在資源
File attachmentFile = resource.getFile();
helper.addAttachment(file.get("fileName"), attachmentFile);
}
}
mailSender.send(msg);
}
}
四、使用MailConfig
@Autowired private MailConfig mailConfig;
使用MailConfig里面的方法發(fā)送即可以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java中struts2和spring MVC的區(qū)別_動力節(jié)點Java學院整理
這篇文章主要介紹了Java中struts2和spring MVC的區(qū)別,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09
SpringBoot集成Mybatis實現(xiàn)對多數(shù)據(jù)源訪問原理
本文主要分析討論在SpringBoot應用中我們該如何配置SqlSessionFactoryBean對象,進而實現(xiàn)對多個不同的數(shù)據(jù)源的操縱,文章通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-11-11
Java關鍵字volatile和synchronized作用和區(qū)別
這篇文章主要為大家詳細介紹了Java關鍵字volatile和synchronized的作用和區(qū)別,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
springboot3.X版本集成mybatis遇到的問題及解決
在將SpringBoot3.X版本與MyBatis集成時,直接參考基于SpringBoot2.X的配置方法會導致各種報錯,尤其是無法注入mapper的bean問題,這主要是因為SpringBoot3.X版本需要搭配MyBatis3.0.3及以上版本才能正常工作,通過更新maven配置至MyBatis3.0.3版本,可以解決這一問題2024-09-09
spring boot + jpa + kotlin入門實例詳解
這篇文章主要介紹了spring boot + jpa + kotlin入門實例詳解 ,需要的朋友可以參考下2017-07-07
Java中comparator接口和Comparable接口的比較解析
這篇文章主要介紹了Java中comparator接口和Comparable接口的比較解析,Java提供了一個用于比較的接口Comparator和Comparable接口,提供了一個比較的方法,所有實現(xiàn)該接口的類,都動態(tài)的實現(xiàn)了該比較方法,需要的朋友可以參考下2023-08-08
MyBatis游標Cursor在Oracle數(shù)據(jù)庫上的測試方式
這篇文章主要介紹了MyBatis游標Cursor在Oracle數(shù)據(jù)庫上的測試方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

