spring mail借助qq郵箱服務器發(fā)送郵件
spring mail封裝了javaMail的郵件服務,讓郵件服務使用起來更簡單,下面以qq郵箱服務器為例,用spring mail服務來發(fā)送郵件
配置qq郵箱,“設置”——“賬戶”,打開smtp服務,生成授權碼
生成授權碼需要驗證手機,接下來用qq郵箱賬號和授權碼就可以發(fā)送郵件了,不需要qq密碼
spring mail服務在spring-context-support中,配置依賴,然后就可以借助qq郵箱提供的發(fā)件服務器發(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");//授權碼 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"); } }
運行,即可發(fā)送一封email,注意:授權碼而不是密碼,端口并不是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ā)送這個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()方法中實在太亂了,所以,應該將html作為一個文件單獨管理,然后用工具將其內(nèi)容轉(zhuǎn)換為字符串,作為setText()的參數(shù),下面以freemarker模板引擎為例
在工程src/main/resources目錄下新建templates目錄,里面放一個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)換為字符串,這當然需要依賴新的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字符串
注意:過程中模板路徑的配置與讀取是個麻煩事,暫時以這種方式處理
發(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Spring學習筆記3之消息隊列(rabbitmq)發(fā)送郵件功能
- springMVC發(fā)送郵件的簡單實現(xiàn)
- Spring框架JavaMailSender發(fā)送郵件工具類詳解
- Java的Spring框架中實現(xiàn)發(fā)送郵件功能的核心代碼示例
- Spring Boot實戰(zhàn)之發(fā)送郵件示例代碼
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- Spring+quartz實現(xiàn)定時發(fā)送郵件功能實例
- SpringBoot使用FreeMarker模板發(fā)送郵件
- spring+maven實現(xiàn)發(fā)送郵件功能
- SpringBoot實現(xiàn)發(fā)送郵件任務
相關文章
java客戶端Etcd官方倉庫jetcd中KeepAlive接口實現(xiàn)
這篇文章主要為大家介紹了java客戶端Etcd官方倉庫jetcd中KeepAlive接口實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,多多加薪2022-02-02JAVA生成八位不重復隨機數(shù)最快的方法總結(jié)(省時間省空間)
隨機數(shù)在實際中使用很廣泛,比如要隨即生成一個固定長度的字符串、數(shù)字,這篇文章主要給大家介紹了關于JAVA生成八位不重復隨機數(shù)最快的方法,文中介紹的方法省時間省空間,需要的朋友可以參考下2024-03-03springboot?maven?打包插件介紹及注意事項說明
這篇文章主要介紹了springboot?maven?打包插件介紹及注意事項說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12