spring boot如何加入mail郵件支持
這篇文章主要介紹了spring boot如何加入mail郵件支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
一、添加依賴
<!-- 郵件整合 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、添加mail.properties配置文件
#設(shè)置郵箱主機 spring.mail.host=smtp.qq.com #設(shè)置用戶名 spring.mail.username=xxxxxxx #設(shè)置密碼 #QQ郵箱->設(shè)置->賬戶->POP3/SMTP服務(wù):開啟服務(wù)后會獲得QQ的授權(quán)碼 spring.mail.password=xxxxxxxxxxxxxxxx #端口 spring.mail.port=465 #協(xié)議 #spring.mail.protocol=smtp #設(shè)置是否需要認(rèn)證,如果為true,那么用戶名和密碼就必須的, #如果設(shè)置false,可以不設(shè)置用戶名和密碼,當(dāng)然也得看你的對接的平臺是否支持無密碼進行訪問的。 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 郵件標(biāo)題
* @param content 郵件內(nèi)容
*/
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的內(nèi)容
*
* @param toEmail 收件人郵箱
* @param title 郵件標(biāo)題
* @param htmlContent 郵件內(nèi)容
*/
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 郵件標(biāo)題
* @param content 文本內(nèi)容
* @param aboutFiles 附件信息 每個子項都是一個文件相關(guān)信息的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ā)送即可以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java面向?qū)ο笤O(shè)計原則之接口隔離原則示例詳解
這篇文章主要為大家介紹了java面向?qū)ο笤O(shè)計原則之接口隔離原則的示例詳解,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進步早日升職加薪2021-10-10
Spring中@Configuration注解修改的類生成代理原因解析
大家好,本篇文章主要講的是Spring中@Configuration注解修改的類生成代理原因解析,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02
基于ElasticSearch Analyzer的使用規(guī)則詳解
這篇文章主要介紹了基于ElasticSearch Analyzer的使用規(guī)則,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
Java String index out of range:100錯誤解決方案詳解
這篇文章主要介紹了Java String index out of range:100錯誤解決方案詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
詳解Java如何實現(xiàn)一個像String一樣不可變的類
說到?String?大家都知道?String?是一個不可變的類;雖然用的很多,那不知道小伙伴們有沒有想過怎么樣創(chuàng)建一個自己的不可變的類呢?這篇文章就帶大家來實踐一下,創(chuàng)建一個自己的不可變的類2022-11-11

