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("martin.xus@gmail.com""martin"
);
5email.setFrom("martin@4ya.cn""martin"
);
6email.setSubject("²âÊÔÖ÷Ìâ"
);
7email.setMsg("ÕâÀïÊÇÓʼþÄÚÈÝ"
);
8email.send();

¾ÍÈç´úÂëÀï×ÖÃæÉϵÄÒâ˼һÑù¼òµ¥:
1£º´´½¨ÒÔSimpleEmail¶ÔÏó
2£ºÉ趨·¢ËÍÐżþµÄsmtp·þÎñÆ÷£¬Èç¹ûûÓÐÉ趨£¬»áѰÕÒϵͳ±äÁ¿ÖÐmail.hostÖµ¡£
3£ºÉ趨smtpµÄÓû§ºÍÃÜÂë
4£ºÊÕ¼þÈË
5£º·¢¼þÈË
6£ºÖ÷Ìâ
7£ºÄÚÈÝ
8£º·¢ËÍ

¶þ£º·¢ËÍ´ø¸½¼þµÄÓʼþ
ÎÒÃÇ¿ÉÒÔ·¢Ëͱ¾»úµÄ¸½¼þ£¬µ±È»ÎÒÃÇÒ²¿ÉÒÔ·¢ËͷDZ¾»úµÄ¸½¼þ£¬Èç¹û·¢Ë͵ÄÊÇÒ»¸ö´æÔÚÍøÂçÉϵĸ½¼þµÄurl,ÔòÓʼþ·¢Ë͵Äʱºò»á×Ô¶¯ÏÂÔØ£¬Ìí¼Óµ½¸½¼þÖС£

   1£º£©·¢Ëͱ¾µØ¸½¼þ£º
1EmailAttachment attachment = new EmailAttachment();
2attachment.setPath("test/test.rar"
);
3
attachment.setDisposition(EmailAttachment.ATTACHMENT);
4attachment.setDescription("python resource"
);
5attachment.setName("resource");

   2£º£©·¢ËͲ»´æÔÚ±¾µØµÄ¸½¼þ
1EmailAttachment attachment = new EmailAttachment();
2attachment.setURL(new URL("/pic/2006/2/25/1340002.jpg"
));
3
attachment.setDisposition(EmailAttachment.ATTACHMENT);
4attachment.setDescription("΢ЦͼÊé¹Ý"
);
5attachment.setName("΢ЦͼÊé¹Ý");


next,Ìí¼Ó¸½¼þµ½ÎÒÃǵÄÓʼþÖÐ
 1MultiPartEmail email = new MultiPartEmail();
 2email.setHostName("mail.4ya.cn"
);
 3    email.setAuthentication("<username>","<password>")
 4email.addTo("martin.xus@gmail.com""martin"
);
 5email.setFrom("martin@4ya.cn""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("martin@4ya.cn"martin"
);
 6email.setFrom("martin.xus@gmail.com"martin"
);
 7email.setSubject("Ö÷Ì⣺¸ÃÓʼþ°üÀ¨html¸ñʽÄÚÈÝ"
);
 

 8//
 embed the image and get the content id
 9// ×¢ÒâÕâÀembed ½«°ïÖúÎÒÃÇ´´½¨±êÇ©È磺cid:xxx url

10URL url = new URL("/pic/2006/2/25/1340003.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()


Î壺any more?
µ±È»ÓÐÁË o_o ÒÔºóÔÙд.