Jakarta發(fā)布了Commons Emails 1.1leased 版本,目的是為了簡化JavaMail。
該項目主頁:http://commons.apache.org/email/
知道有它幾個class嗎?你一定想不到,只有8個!
好了,開始我們的jakarta commons emails 之旅:)
一:Quick Start
通過SimpleEmail發(fā)送郵件
java.lang.Object
org.apache.commons.mail.Email
org.apache.commons.mail.SimpleEmail
SimpleEmail email = new SimpleEmail();
email.setHostName("mail.4ya.cn");
email.setAuthentication("<username>","<password>")
email.addTo("martin.xus@gmail.com", "martin");
email.setFrom("martin@4ya.cn", "martin");
email.setSubject("測試主題");
email.setMsg("這里是郵件內(nèi)容");
email.send();
就如代碼里字面上的意思一樣簡單:
1:創(chuàng)建以SimpleEmail對象
2:設(shè)定發(fā)送信件的smtp服務(wù)器,如果沒有設(shè)定,會尋找系統(tǒng)變量中mail.host值。
3:設(shè)定smtp的用戶和密碼
4:收件人
5:發(fā)件人
6:主題
7:內(nèi)容
8:發(fā)送
二:發(fā)送帶附件的郵件
我們可以發(fā)送本機的附件,當(dāng)然我們也可以發(fā)送非本機的附件,如果發(fā)送的是一個存在網(wǎng)絡(luò)上的附件的url,則郵件發(fā)送的時候會自動下載,添加到附件中。
1:)發(fā)送本地附件:
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("test/test.rar");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("python resource");
attachment.setName("resource");
2:)發(fā)送不存在本地的附件
EmailAttachment attachment = new EmailAttachment();
attachment.setURL(new URL("/pic/2006/2/25/1340002.jpg"));
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("微笑圖書館");
attachment.setName("微笑圖書館");
next,添加附件到我們的郵件中
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.4ya.cn");
email.setAuthentication("<username>","<password>")
email.addTo("martin.xus@gmail.com", "martin");
email.setFrom("martin@4ya.cn", "martin");
email.setSubject("郵件主題");
email.setMsg("郵件內(nèi)容");

//添加附件
email.attach(attachment);

//發(fā)送郵件
email.send();
如果需要發(fā)送多個附件,只需創(chuàng)建多個EmailAttachement,即可
email.attach(attachment1)
email.attach(attachment2)
三:發(fā)送html格式的郵件
通過HtmlEmail我們可以發(fā)送Html格式的郵件:
java.lang.Object
org.apache.commons.mail.Email
org.apache.commons.mail.MultiPartEmail
org.apache.commons.mail.HtmlEmail
如下:
//HtmlEmail!
HtmlEmail email = new HtmlEmail();
email.setHostName("mail.4ya.cn");
email.setAuthentication("<username>","<password>")
email.addTo("martin@4ya.cn"martin");
email.setFrom("martin.xus@gmail.com"martin");
email.setSubject("主題:該郵件包括html格式內(nèi)容");
// embed the image and get the content id
// 注意這里:embed 將幫助我們創(chuàng)建標(biāo)簽如:cid:xxx url
URL url = new URL("/pic/2006/2/25/1340003.gif");
String cid = email.embed(url, "Apache logo");


/** *//** *//** *//**
set the html message
我們看到HtmlEmail extends Email的,它依然有setMsg(),但是這里發(fā)送的郵件包括了插入在郵件內(nèi)容中的圖片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代碼
**/
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");

// set the alternative message
email.setTextMsg("Your email client does not support HTML messages");
//set mail
email.send();

四:最后一步
如果需要實現(xiàn)更復(fù)雜authenticator 你可以extends javax.mail.Authenticator ,實現(xiàn)你自己的東西,然后調(diào)用
Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可
這一點jakarta也做了,給我們提供了一個defaultAuthenticator
java.lang.Object
javax.mail.Authenticator
org.apache.commons.mail.DefaultAuthenticator
覆蓋掉該方法,實現(xiàn)你自己的東東 o_o
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
如果需要運行在web容器里,可能需要輔助的mail.jar和activation.jar包.
可以到我的網(wǎng)盤下載;下載