簡化JavaMail:小巧 Jakarta Commons-Email 簡單教程
Jakarta發(fā)布了Commons Emails 1.0 released 版本,目的是為了簡化JavaMail。知道有它幾個(gè)class嗎?你一定想不到,只有8個(gè)!
好了,開始我們的jakarta commons emails 之旅:)
一:Quick Start
通過SimpleEmail發(fā)送郵件
1
java.lang.Object
2
org.apache.commons.mail.Email
3
org.apache.commons.mail.SimpleEmail

2

3

1
SimpleEmail email = new SimpleEmail();
2
email.setHostName("mail.4ya.cn");
3
email.setAuthentication("<username>","<password>")
4
email.addTo("martin.xus@gmail.com", "martin");
5
email.setFrom("martin@4ya.cn", "martin");
6
email.setSubject("測試主題");
7
email.setMsg("這里是郵件內(nèi)容");
8
email.send();

2

3

4

5

6

7

8

就如代碼里字面上的意思一樣簡單:
1:創(chuàng)建以SimpleEmail對象
2:設(shè)定發(fā)送信件的smtp服務(wù)器,如果沒有設(shè)定,會(huì)尋找系統(tǒng)變量中mail.host值。
3:設(shè)定smtp的用戶和密碼
4:收件人
5:發(fā)件人
6:主題
7:內(nèi)容
8:發(fā)送
二:發(fā)送帶附件的郵件
我們可以發(fā)送本機(jī)的附件,當(dāng)然我們也可以發(fā)送非本機(jī)的附件,如果發(fā)送的是一個(gè)存在網(wǎng)絡(luò)上的附件的url,則郵件發(fā)送的時(shí)候會(huì)自動(dòng)下載,添加到附件中。
1:)發(fā)送本地附件:
1
EmailAttachment attachment = new EmailAttachment();
2
attachment.setPath("test/test.rar");
3
attachment.setDisposition(EmailAttachment.ATTACHMENT);
4
attachment.setDescription("python resource");
5
attachment.setName("resource");

2

3

4

5

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

2

3

4

5

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

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

2

3 email.setAuthentication("<username>","<password>")
4

5

6

7


8

9

10

11

12

如果需要發(fā)送多個(gè)附件,只需創(chuàng)建多個(gè)EmailAttachement,即可
1
email.attach(attachment1)
2
email.attach(attachment2)

2

三:發(fā)送html格式的郵件
通過HtmlEmail我們可以發(fā)送Html格式的郵件:
1
java.lang.Object
2
org.apache.commons.mail.Email
3
org.apache.commons.mail.MultiPartEmail
4
org.apache.commons.mail.HtmlEmail
5

2

3

4

5

如下:
1
//HtmlEmail!
2
HtmlEmail email = new HtmlEmail();
3
email.setHostName("mail.4ya.cn");
3 email.setAuthentication("<username>","<password>")
5
email.addTo("martin@4ya.cn"martin");
6
email.setFrom("martin.xus@gmail.com"martin");
7
email.setSubject("主題:該郵件包括html格式內(nèi)容");

8
// embed the image and get the content id
9
// 注意這里:embed 將幫助我們創(chuàng)建標(biāo)簽如:cid:xxx url
10
URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
11
String cid = email.embed(url, "Apache logo");
12
13
/** *//**
14
set the html message
15
我們看到HtmlEmail extends Email的,它依然有setMsg(),但是這里發(fā)送的郵件包括了插入在郵件內(nèi)容中的圖片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代碼
16
**/
17
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");
18
19
// set the alternative message
20
email.setTextMsg("Your email client does not support HTML messages");
21
22
//set mail
23
email.send();
24

2

3

3 email.setAuthentication("<username>","<password>")
5

6

7


8

9

10

11

12

13


14

15

16

17

18

19

20

21

22

23

24

四:最后一步
如果需要實(shí)現(xiàn)更復(fù)雜authenticator 你可以extends javax.mail.Authenticator ,實(shí)現(xiàn)你自己的東西,然后調(diào)用Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可
這一點(diǎn)jakarta也做了,給我們提供了一個(gè)defaultAuthenticator
1
java.lang.Object
2
javax.mail.Authenticator
3
org.apache.commons.mail.DefaultAuthenticator

2

3

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

五:any more?
當(dāng)然有了 o_o 以后再寫.
posted on 2005-09-29 10:34 martin xus 閱讀(7870) 評論(22) 編輯 收藏 所屬分類: java