spring mail借助qq郵箱服務(wù)器發(fā)送郵件
spring mail封裝了javaMail的郵件服務(wù),讓郵件服務(wù)使用起來更簡單,下面以qq郵箱服務(wù)器為例,用spring mail服務(wù)來發(fā)送郵件
配置qq郵箱,“設(shè)置”——“賬戶”,打開smtp服務(wù),生成授權(quán)碼
生成授權(quán)碼需要驗(yàn)證手機(jī),接下來用qq郵箱賬號和授權(quán)碼就可以發(fā)送郵件了,不需要qq密碼
spring mail服務(wù)在spring-context-support中,配置依賴,然后就可以借助qq郵箱提供的發(fā)件服務(wù)器發(fā)送郵件了
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.17.RELEASE</version> </dependency>
普通文本郵件
首先測試的是普通文本郵件
package com.xmyself.mail; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; public class Main { public static void main(String[] args) { JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); mailSender.setHost("smtp.qq.com"); mailSender.setPort(587); mailSender.setUsername("573215750@qq.com"); mailSender.setPassword("dsruklozelxcbdba");//授權(quán)碼 SimpleMailMessage mail = new SimpleMailMessage(); mail.setTo("573215750@qq.com"); mail.setFrom("573215750@qq.com"); mail.setSubject("test mail"); mail.setText("test mail content"); mailSender.send(mail); System.out.println("success"); } }
運(yùn)行,即可發(fā)送一封email,注意:授權(quán)碼而不是密碼,端口并不是25而是587
接下來,保持mailSender不變,修改mail類型,發(fā)送內(nèi)容豐富的郵件
簡單html郵件
讓郵件內(nèi)容以html格式展現(xiàn),只需要修改如下
MimeMessage mail = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mail, true);//true用來打開multipart模式,添加圖片或附件 helper.setTo("573215750@qq.com"); helper.setFrom("573215750@qq.com"); helper.setSubject("test mail"); helper.setText("<html><head></head><body>" + "<h1>hello!!spring html Mail</h1>" + "</body></html>" , true);
依然使用mailSender發(fā)送這個(gè)mail
mailSender.send(mail);
帶圖片的html郵件
在郵件的html內(nèi)容中插入圖片顯示,修改text內(nèi)容即可
helper.setText("<html><head></head><body>" + "<h1>hello!!spring html Mail</h1>" + "<img src=\"cid:image\" />" + "</body></html>" , true); FileSystemResource image = new FileSystemResource(new File("d:/test.jpg")); helper.addInline("image", image);
帶附件的html郵件
為郵件添加附件,text內(nèi)容不變,只需要修改如下
helper.setText("<html><head></head><body>" + "<h1>hello!!spring html Mail</h1>" + "</body></html>" , true); FileSystemResource image = new FileSystemResource(new File("d:/test.jpg")); helper.addAttachment("test.jpg", image);
freemarker模板郵件
html內(nèi)容通常非常豐富,直接寫在setText()方法中實(shí)在太亂了,所以,應(yīng)該將html作為一個(gè)文件單獨(dú)管理,然后用工具將其內(nèi)容轉(zhuǎn)換為字符串,作為setText()的參數(shù),下面以freemarker模板引擎為例
在工程src/main/resources目錄下新建templates目錄,里面放一個(gè)test.ftl文件,內(nèi)容如下
<html> <head></head> <body> <p>test freemarker template, welcome ${username}</p> <img src="cid:image" /> </body> </html>
然后,用freemarker和spring提供的工具將內(nèi)容轉(zhuǎn)換為字符串,這當(dāng)然需要依賴新的jar
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.23</version> </dependency>
新建FreemarkerParser.java
package com.xmyself.mail; import java.util.Map; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import freemarker.template.Configuration; import freemarker.template.Template; public class FreemarkerParser { public String toHtmlString(String name, Map<String, String> data) { @SuppressWarnings("deprecation") Configuration config = new Configuration(); config.setClassForTemplateLoading(this.getClass(), "/templates/"); try { Template template = config.getTemplate(name); return FreeMarkerTemplateUtils.processTemplateIntoString(template, data); } catch (Exception e) { e.printStackTrace(); } return "fail"; } }
用map中的值替換掉模板中的${}內(nèi)容,將模板文件轉(zhuǎn)換為String字符串
注意:過程中模板路徑的配置與讀取是個(gè)麻煩事,暫時(shí)以這種方式處理
發(fā)送郵件的代碼只需要非常小的變化
Map<String, String> data = new HashMap<String, String>(); data.put("username", "chengyi"); String text = new FreemarkerParser().toHtmlString("test.ftl", data); helper.setText(text, true); FileSystemResource image = new FileSystemResource(new File("d:/test.jpg")); helper.addInline("image", image);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring學(xué)習(xí)筆記3之消息隊(duì)列(rabbitmq)發(fā)送郵件功能
- springMVC發(fā)送郵件的簡單實(shí)現(xiàn)
- Spring框架JavaMailSender發(fā)送郵件工具類詳解
- Java的Spring框架中實(shí)現(xiàn)發(fā)送郵件功能的核心代碼示例
- Spring Boot實(shí)戰(zhàn)之發(fā)送郵件示例代碼
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- Spring+quartz實(shí)現(xiàn)定時(shí)發(fā)送郵件功能實(shí)例
- SpringBoot使用FreeMarker模板發(fā)送郵件
- spring+maven實(shí)現(xiàn)發(fā)送郵件功能
- SpringBoot實(shí)現(xiàn)發(fā)送郵件任務(wù)
相關(guān)文章
Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序
最近在學(xué)習(xí)Java,然后上個(gè)月迷上了九宮格數(shù)獨(dú),玩了幾天,覺得實(shí)在有趣,就想著能不能用編程來解決,于是就自己寫了個(gè),還真解決了。下面這篇文章就給大家主要介紹了Java實(shí)現(xiàn)解數(shù)獨(dú)的小程序,需要的朋友可以參考借鑒。2017-01-01java客戶端Etcd官方倉庫jetcd中KeepAlive接口實(shí)現(xiàn)
這篇文章主要為大家介紹了java客戶端Etcd官方倉庫jetcd中KeepAlive接口實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,多多加薪2022-02-02JAVA生成八位不重復(fù)隨機(jī)數(shù)最快的方法總結(jié)(省時(shí)間省空間)
隨機(jī)數(shù)在實(shí)際中使用很廣泛,比如要隨即生成一個(gè)固定長度的字符串、數(shù)字,這篇文章主要給大家介紹了關(guān)于JAVA生成八位不重復(fù)隨機(jī)數(shù)最快的方法,文中介紹的方法省時(shí)間省空間,需要的朋友可以參考下2024-03-03快速入門介紹Java中強(qiáng)大的String.format()
這篇文章主要給大家介紹了如何快速入門介紹Java中強(qiáng)大的String.format()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03springboot?maven?打包插件介紹及注意事項(xiàng)說明
這篇文章主要介紹了springboot?maven?打包插件介紹及注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12