spring mail封裝了javaMail的郵件服務(wù),讓郵件服務(wù)使用起來更簡單,下面以qq郵箱服務(wù)器為例,用spring mail服務(wù)來發(fā)送郵件
配置qq郵箱,“設(shè)置”——“賬戶”,打開smtp服務(wù),生成授權(quán)碼
生成授權(quán)碼需要驗證手機(jī),接下來用qq郵箱賬號和授權(quán)碼就可以發(fā)送郵件了,不需要qq密碼
spring mail服務(wù)在spring-context-support中,配置依賴,然后就可以借助qq郵箱提供的發(fā)件服務(wù)器發(fā)送郵件了
1
2
3
4
5
6
7
8
9
10
|
<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> |
普通文本郵件
首先測試的是普通文本郵件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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.setPassword( "dsruklozelxcbdba" ); //授權(quán)碼 SimpleMailMessage mail = new SimpleMailMessage(); mail.setSubject( "test mail" ); mail.setText( "test mail content" ); mailSender.send(mail); System.out.println( "success" ); } } |
運行,即可發(fā)送一封email,注意:授權(quán)碼而不是密碼,端口并不是25而是587
接下來,保持mailSender不變,修改mail類型,發(fā)送內(nèi)容豐富的郵件
簡單html郵件
讓郵件內(nèi)容以html格式展現(xiàn),只需要修改如下
1
2
3
4
5
6
7
8
9
10
|
MimeMessage mail = mailSender.createMimeMessage(); 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)容即可
1
2
3
4
5
6
7
|
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)容不變,只需要修改如下
1
2
3
4
5
6
|
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()方法中實在太亂了,所以,應(yīng)該將html作為一個文件單獨管理,然后用工具將其內(nèi)容轉(zhuǎn)換為字符串,作為setText()的參數(shù),下面以freemarker模板引擎為例
在工程src/main/resources目錄下新建templates目錄,里面放一個test.ftl文件,內(nèi)容如下
1
2
3
4
5
6
7
|
< 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
1
2
3
4
5
|
<dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version> 2.3 . 23 </version> </dependency> |
新建FreemarkerParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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ā)送郵件的代碼只需要非常小的變化
1
2
3
4
5
6
7
|
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í)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/ywlaker/p/6164809.html