日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

016_Spring中电子邮件

發(fā)布時間:2025/4/17 javascript 85 豆豆
生活随笔 收集整理的這篇文章主要介紹了 016_Spring中电子邮件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1. Spring提供了一個實用的發(fā)送電子郵件庫, 它為使用者屏蔽了郵件系統(tǒng)的底層細節(jié)和客戶端的底層資源處理。

2. 依賴庫: 使用Spring框架的郵件功能需要將JavaMail的Jar包添加到依賴中。

3. Spring郵件相關(guān)功能在spring-context-support-4.2.4.RELEASE.jar下的org.springframework.mail包中

4. 其中MailSender是發(fā)送郵件的核心接口, JavaMailSenderImpl是核心接口的重要實現(xiàn)類

5. 其中MailMessage是郵件的核心接口, SimpleMailMessage類是對郵件屬性(發(fā)件人、收件人以等)進行簡單的封裝。

6. 例子

6.1. 創(chuàng)建一個名為SpringJavaMail的Java項目, 添加相關(guān)jar包

6.2. 添加幾個文件

6.3. 在src目錄下創(chuàng)建mail.properties

6.4. 在src目錄下創(chuàng)建applicationContext.xml

?6.5. 編輯SimpleMail.java

package com.zr.mail;import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** 發(fā)送純文本郵件*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class SimpleMail {@Resource(name="javaMailSenderImpl")private JavaMailSenderImpl javaMailSenderImpl;@Testpublic void sendMail() {SimpleMailMessage templateMessage = new SimpleMailMessage();templateMessage.setFrom("xxxxxxxxxx@qq.com");templateMessage.setTo("ooooooooo@qq.com");templateMessage.setSubject("測試Spring中電子郵件");templateMessage.setText("SimpleMailMessage只能發(fā)送純文字");javaMailSenderImpl.send(templateMessage);}}

7. 使用MimeMessageHelper

7.1. org.springframework.mail.javamail.MimeMessageHelper是一個處理JavaMail消息的好工具, 它屏蔽了很多JavaMail API的細節(jié), 所以使用MimeMessageHelper可以很簡便的創(chuàng)建一個MimeMessage。

JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl(); MimeMessage message = javaMailSenderImpl.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); javaMailSenderImpl.send(message);

7.2. 郵件允許添加附件和內(nèi)聯(lián)資源。嵌入資源是你嵌入到郵件中的圖片或樣式,但又不希望顯示為附件。

7.3. 嵌入資源需要使用contentId添加到MIME消息中。文本和嵌入資源添加是有順序的, 需要按照先添加文本, 再添加嵌入資源的順序。否則, 它將不會工作。郵件內(nèi)容內(nèi)嵌的圖片addInline中的contentId要和郵件內(nèi)容中的cid相對應(yīng)。

MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setText("<html><body><img src='cid:ani_08' /><br />郵件包含文字、圖片和文件</body></html>", true); FileSystemResource res = new FileSystemResource(new File("config/ani_08.png")); helper.addInline("ani_08", res);

7.4. 添加附件

MimeMessageHelper helper = new MimeMessageHelper(message, true); MimeBodyPart file = new MimeBodyPart(); file.attachFile("config/ActiveMQ中文教程參考手冊.pdf"); helper.getRootMimeMultipart().addBodyPart(file);

7.5. 編輯MailHelper.java

package com.zr.mail;import java.io.File; import java.io.IOException; import javax.annotation.Resource; import javax.mail.MessagingException; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.core.io.FileSystemResource; import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** 發(fā)送帶圖片和文件的郵件*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class MailHelper {@Resource(name="javaMailSenderImpl")private JavaMailSenderImpl javaMailSenderImpl;@Testpublic void sendMail() {try {MimeMessage message = javaMailSenderImpl.createMimeMessage();// 發(fā)送帶附件和內(nèi)聯(lián)元素的郵件需要將第二個參數(shù)設(shè)置為trueMimeMessageHelper helper = new MimeMessageHelper(message, true);// 發(fā)送方郵箱和配置文件中的mail.username要一致helper.setFrom("xxxxxxxxxxx@qq.com");// 接收方helper.setTo("oooooooooooo@qq.com");// 主題helper.setSubject("測試Spring中電子郵件");// 郵件內(nèi)容helper.setText("<html><body><img src='cid:ani_08' /><br />發(fā)送文本、圖片和文件</body></html>", true);// 郵件內(nèi)容內(nèi)嵌的圖片addInline中的contentId要和郵件內(nèi)容中的cid相對應(yīng)FileSystemResource res = new FileSystemResource(new File("config/ani_08.png"));helper.addInline("ani_08", res);// 郵件附件, JavaMail 1.6,2版本使用MimeBodyPart的attachFile方法才不會有中文亂碼MimeBodyPart file = new MimeBodyPart();file.attachFile("config/ActiveMQ中文教程參考手冊.pdf");helper.getRootMimeMultipart().addBodyPart(file);javaMailSenderImpl.send(message);} catch (MailException | MessagingException | IOException e) {e.printStackTrace();}} }

8. 使用模板庫創(chuàng)建電子郵件內(nèi)容

8.1. 在簡單的情況下, 像前面例子那樣使用API就可以滿足我們的需要了。

8.2. 在典型的企業(yè)應(yīng)用程序中, 下面的原因讓你不定會使用上面的方法創(chuàng)建你的郵件內(nèi)容。

8.2.1. 在Java代碼中創(chuàng)建HTML的電子郵件內(nèi)容冗長, 且容易出錯。

8.2.2. 呈現(xiàn)邏輯和業(yè)務(wù)邏輯混雜。

8.2.3. 更改電子郵件內(nèi)容的展示結(jié)構(gòu)需要編寫Java代碼, 重新編譯, 重新部署。

8.3. 通常解決方法是使用模板框架定義電子郵件的呈現(xiàn)邏輯, 如FreeMarker。分離呈現(xiàn)邏輯和業(yè)務(wù)邏輯使得你的代碼更清晰。當(dāng)你的郵件的內(nèi)容變的復(fù)雜時, 這絕對是一個最佳實踐, 而且Spring框架對FreeMarker有很好的支持。

總結(jié)

以上是生活随笔為你收集整理的016_Spring中电子邮件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。