簡化JavaMail:小巧 Jakarta Commons-Email 簡單教程

Jakarta發佈了Commons Emails 1.0 released 版本,目的是爲了簡化JavaMail。

知道有它幾個class嗎?你一定想不到,只有8個!

好了,開始我們的jakarta commons emails 之旅:)

一:Quick Start
通過SimpleEmail發送郵件
1java.lang.Object
2  org.apache.commons.mail.Email
3      org.apache.commons.mail.SimpleEmail

1SimpleEmail email = new SimpleEmail();
2email.setHostName("mail.4ya.cn");
3email.setAuthentication("<username>","<password>")
4email.addTo("[email protected]""martin");
5email.setFrom("[email protected]""martin");
6email.setSubject("測試主題");
7email.setMsg("這裏是郵件內容");
8email.send();

就如代碼裏字面上的意思一樣簡單:
1:創建以SimpleEmail對象
2:設定發送信件的smtp服務器,如果沒有設定,會尋找系統變量中mail.host值。
3:設定smtp的用戶和密碼
4:收件人
5:發件人
6:主題
7:內容
8:發送

二:發送帶附件的郵件
我們可以發送本機的附件,當然我們也可以發送非本機的附件,如果發送的是一個存在網絡上的附件的url,則郵件發送的時候會自動下載,添加到附件中。

   1:)發送本地附件:
1EmailAttachment attachment = new EmailAttachment();
2attachment.setPath("test/test.rar");
3attachment.setDisposition(EmailAttachment.ATTACHMENT);
4attachment.setDescription("python resource");
5attachment.setName("resource");

   2:)發送不存在本地的附件
1EmailAttachment attachment = new EmailAttachment();
2attachment.setURL(new URL("http://www.smilinglibrary.org/sldoc/pics/index03.jpg"));
3attachment.setDisposition(EmailAttachment.ATTACHMENT);
4attachment.setDescription("微笑圖書館");
5attachment.setName("微笑圖書館");


next,添加附件到我們的郵件中
 1MultiPartEmail email = new MultiPartEmail();
 2email.setHostName("mail.4ya.cn");
 3    email.setAuthentication("<username>","<password>")
 4email.addTo("[email protected]""martin");
 5email.setFrom("[email protected]""martin");
 6email.setSubject("郵件主題");
 7email.setMsg("郵件內容");

 8//添加附件
 9email.attach(attachment);
10
11//發送郵件
12email.send();

如果需要發送多個附件,只需創建多個EmailAttachement,即可
1email.attach(attachment1)
2email.attach(attachment2)

三:發送html格式的郵件
通過HtmlEmail我們可以發送Html格式的郵件:

1java.lang.Object
2  org.apache.commons.mail.Email
3      org.apache.commons.mail.MultiPartEmail
4          org.apache.commons.mail.HtmlEmail
5

如下:
 1//HtmlEmail!
 2HtmlEmail email = new HtmlEmail();
 3email.setHostName("mail.4ya.cn");
 3   email.setAuthentication("<username>","<password>")
 5email.addTo("[email protected]"martin");
 6email.setFrom("[email protected]"martin");
 7email.setSubject("主題:該郵件包括html格式內容");
 
 8// embed the image and get the content id
 9// 注意這裏:embed 將幫助我們創建標籤如:cid:xxx url
10URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
11String cid = email.embed(url, "Apache logo");
12
13/**
14set the html message
15我們看到HtmlEmail extends Email的,它依然有setMsg(),但是這裏發送的郵件包括了插入在郵件內容中的圖片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代碼
16**/

17email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
18
19// set the alternative message
20email.setTextMsg("Your email client does not support HTML messages");
21
22//set mail
23email.send();
24

四:最後一步
如果需要實現更復雜authenticator 你可以extends javax.mail.Authenticator ,實現你自己的東西,然後調用Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可

這一點jakarta也做了,給我們提供了一個defaultAuthenticator
1java.lang.Object
2  javax.mail.Authenticator
3      org.apache.commons.mail.DefaultAuthenticator

覆蓋掉該方法,實現你自己的東東 o_o
1protected javax.mail.PasswordAuthentication getPasswordAuthentication()



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章