spring boot如何加入mail郵件支持
這篇文章主要介紹了spring boot如何加入mail郵件支持,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
一、添加依賴
<!-- 郵件整合 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
二、添加mail.properties配置文件
#設(shè)置郵箱主機(jī) spring.mail.host=smtp.qq.com #設(shè)置用戶名 spring.mail.username=xxxxxxx #設(shè)置密碼 #QQ郵箱->設(shè)置->賬戶->POP3/SMTP服務(wù):開啟服務(wù)后會(huì)獲得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)然也得看你的對接的平臺是否支持無密碼進(jìn)行訪問的。 spring.mail.properties.mail.smtp.auth=true #STARTTLS[1] 是對純文本通信協(xié)議的擴(kuò)展。它提供一種方式將純文本連接升級為加密連接(TLS或SSL),而不是另外使用一個(gè)端口作加密通信。 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 附件信息 每個(gè)子項(xiàng)都是一個(gè)文件相關(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è)計(jì)原則之接口隔離原則示例詳解
這篇文章主要為大家介紹了java面向?qū)ο笤O(shè)計(jì)原則之接口隔離原則的示例詳解,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-10-10Spring中@Configuration注解修改的類生成代理原因解析
大家好,本篇文章主要講的是Spring中@Configuration注解修改的類生成代理原因解析,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02基于ElasticSearch Analyzer的使用規(guī)則詳解
這篇文章主要介紹了基于ElasticSearch Analyzer的使用規(guī)則,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07DolphinScheduler容錯(cuò)Master源碼分析
這篇文章主要為大家介紹了DolphinScheduler容錯(cuò)Master源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Java利用Socket類實(shí)現(xiàn)TCP通信程序
TCP通信能實(shí)現(xiàn)兩臺計(jì)算機(jī)之間的數(shù)據(jù)交互,通信的兩端,要嚴(yán)格區(qū)分為客戶端與服務(wù)端,下面我們就來看看Java如何利用Socket類實(shí)現(xiàn)TCP通信程序吧2024-02-02Java String index out of range:100錯(cuò)誤解決方案詳解
這篇文章主要介紹了Java String index out of range:100錯(cuò)誤解決方案詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08詳解Java如何實(shí)現(xiàn)一個(gè)像String一樣不可變的類
說到?String?大家都知道?String?是一個(gè)不可變的類;雖然用的很多,那不知道小伙伴們有沒有想過怎么樣創(chuàng)建一個(gè)自己的不可變的類呢?這篇文章就帶大家來實(shí)踐一下,創(chuàng)建一個(gè)自己的不可變的類2022-11-11java操作solr實(shí)現(xiàn)查詢功能的實(shí)例
下面小編就為大家分享一篇java操作solr實(shí)現(xiàn)查詢功能的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11