spring mail封裝了javaMail的郵件服務(wù),讓郵件服務(wù)使用起來(lái)更簡(jiǎn)單,下面以qq郵箱服務(wù)器為例,用spring mail服務(wù)來(lái)發(fā)送郵件
配置qq郵箱,“設(shè)置”――“賬戶”,打開(kāi)smtp服務(wù),生成授權(quán)碼

生成授權(quán)碼需要驗(yàn)證手機(jī),接下來(lái)用qq郵箱賬號(hào)和授權(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>
普通文本郵件
首先測(cè)試的是普通文本郵件
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
接下來(lái),保持mailSender不變,修改mail類(lèi)型,發(fā)送內(nèi)容豐富的郵件
簡(jiǎn)單html郵件
讓郵件內(nèi)容以html格式展現(xiàn),只需要修改如下
MimeMessage mail = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mail, true);//true用來(lái)打開(kāi)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)容通常非常豐富,直接寫(xiě)在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字符串
注意:過(guò)程中模板路徑的配置與讀取是個(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);以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選