ÖªµÀÓÐËü¼¸¸öclassÂð£¿ÄãÒ»¶¨Ïë²»µ½£¬Ö»ÓÐ8¸ö£¡
ºÃÁË£¬¿ªÊ¼ÎÒÃǵÄjakarta commons emails Ö®Â㺣©
Ò»£ºQuick Start
ͨ¹ýSimpleEmail·¢ËÍÓʼþ
1
java.lang.Object
2
org.apache.commons.mail.Email
3
org.apache.commons.mail.SimpleEmail
java.lang.Object2
org.apache.commons.mail.Email3
org.apache.commons.mail.SimpleEmail1
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("ÕâÀïÊÇÓʼþÄÚÈÝ");
8
email.send();
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("ÕâÀïÊÇÓʼþÄÚÈÝ");8
email.send();¾ÍÈç´úÂëÀï×ÖÃæÉϵÄÒâ˼һÑù¼òµ¥:
1£º´´½¨ÒÔSimpleEmail¶ÔÏó
2£ºÉ趨·¢ËÍÐżþµÄsmtp·þÎñÆ÷£¬Èç¹ûûÓÐÉ趨£¬»áѰÕÒϵͳ±äÁ¿ÖÐmail.hostÖµ¡£
3£ºÉ趨smtpµÄÓû§ºÍÃÜÂë
4£ºÊÕ¼þÈË
5£º·¢¼þÈË
6£ºÖ÷Ìâ
7£ºÄÚÈÝ
8£º·¢ËÍ
¶þ£º·¢ËÍ´ø¸½¼þµÄÓʼþ
ÎÒÃÇ¿ÉÒÔ·¢Ëͱ¾»úµÄ¸½¼þ£¬µ±È»ÎÒÃÇÒ²¿ÉÒÔ·¢ËͷDZ¾»úµÄ¸½¼þ£¬Èç¹û·¢Ë͵ÄÊÇÒ»¸ö´æÔÚÍøÂçÉϵĸ½¼þµÄurl,ÔòÓʼþ·¢Ë͵Äʱºò»á×Ô¶¯ÏÂÔØ£¬Ìí¼Óµ½¸½¼þÖС£
1£º£©·¢Ëͱ¾µØ¸½¼þ£º
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");
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£º£©·¢ËͲ»´æÔÚ±¾µØµÄ¸½¼þ
1
EmailAttachment attachment = new EmailAttachment();
2
attachment.setURL(new URL("/pic/2006/2/25/1340002.jpg"));
3
attachment.setDisposition(EmailAttachment.ATTACHMENT);
4
attachment.setDescription("΢ЦͼÊé¹Ý");
5
attachment.setName("΢ЦͼÊé¹Ý");
EmailAttachment attachment = new EmailAttachment();2
attachment.setURL(new URL("/pic/2006/2/25/1340002.jpg"));3
attachment.setDisposition(EmailAttachment.ATTACHMENT);4
attachment.setDescription("΢ЦͼÊé¹Ý");5
attachment.setName("΢ЦͼÊé¹Ý");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("ÓʼþÄÚÈÝ");

8
//Ìí¼Ó¸½¼þ
9
email.attach(attachment);
10
11
//·¢ËÍÓʼþ
12
email.send();
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("ÓʼþÄÚÈÝ");
8
//Ìí¼Ó¸½¼þ9
email.attach(attachment);10

11
//·¢ËÍÓʼþ12
email.send();Èç¹ûÐèÒª·¢ËͶà¸ö¸½¼þ£¬Ö»Ðè´´½¨¶à¸öEmailAttachement,¼´¿É
1
email.attach(attachment1)
2
email.attach(attachment2)
email.attach(attachment1)2
email.attach(attachment2)Èý£º·¢ËÍhtml¸ñʽµÄÓʼþ
ͨ¹ýHtmlEmailÎÒÃÇ¿ÉÒÔ·¢ËÍHtml¸ñʽµÄÓʼþ£º
1
java.lang.Object
2
org.apache.commons.mail.Email
3
org.apache.commons.mail.MultiPartEmail
4
org.apache.commons.mail.HtmlEmail
5
java.lang.Object2
org.apache.commons.mail.Email3
org.apache.commons.mail.MultiPartEmail4
org.apache.commons.mail.HtmlEmail5

ÈçÏ£º
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¸ñʽÄÚÈÝ");

8
// embed the image and get the content id
9
// ×¢ÒâÕâÀembed ½«°ïÖúÎÒÃÇ´´½¨±êÇ©È磺cid:xxx url
10
URL url = new URL("/pic/2006/2/25/1340003.gif");
11
String cid = email.embed(url, "Apache logo");
12
13
/** *//**
14
set the html message
15
ÎÒÃÇ¿´µ½HtmlEmail extends EmailµÄ£¬ËüÒÀÈ»ÓÐsetMsg()£¬µ«ÊÇÕâÀï·¢Ë͵ÄÓʼþ°üÀ¨Á˲åÈëÔÚÓʼþÄÚÈÝÖеÄͼƬ£¬ËùÒÔ²»ÄÜÔÚʹÓÃÁË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
//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¸ñʽÄÚÈÝ");
8
// embed the image and get the content id9
// ×¢ÒâÕâÀembed ½«°ïÖúÎÒÃÇ´´½¨±êÇ©È磺cid:xxx url10
URL url = new URL("/pic/2006/2/25/1340003.gif");11
String cid = email.embed(url, "Apache logo");12

13

/** *//**14
set the html message15
ÎÒÃÇ¿´µ½HtmlEmail extends EmailµÄ£¬ËüÒÀÈ»ÓÐsetMsg()£¬µ«ÊÇÕâÀï·¢Ë͵ÄÓʼþ°üÀ¨Á˲åÈëÔÚÓʼþÄÚÈÝÖеÄͼƬ£¬ËùÒÔ²»ÄÜÔÚʹÓÃÁËsetMsg(),¶øÒªÒÔsetHtmlMsg »òsetTextMsg´úÂë16
**/17
email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");18

19
// set the alternative message20
email.setTextMsg("Your email client does not support HTML messages");21

22
//set mail23
email.send();24

ËÄ£º×îºóÒ»²½
Èç¹ûÐèҪʵÏÖ¸ü¸´ÔÓauthenticator Äã¿ÉÒÔextends javax.mail.Authenticator ,ʵÏÖÄã×Ô¼ºµÄ¶«Î÷£¬È»ºóµ÷ÓÃEmail.setAuthenticator(javax.mail.Authenticator newAuthenticator)¼´¿É
ÕâÒ»µãjakartaÒ²×öÁË£¬¸øÎÒÃÇÌṩÁËÒ»¸ödefaultAuthenticator
1
java.lang.Object
2
javax.mail.Authenticator
3
org.apache.commons.mail.DefaultAuthenticator
java.lang.Object2
javax.mail.Authenticator3
org.apache.commons.mail.DefaultAuthenticator¸²¸Çµô¸Ã·½·¨£¬ÊµÏÖÄã×Ô¼ºµÄ¶«¶« o_o
1
protected javax.mail.PasswordAuthentication getPasswordAuthentication()
protected javax.mail.PasswordAuthentication getPasswordAuthentication()Î壺any more?
µ±È»ÓÐÁË o_o ÒÔºóÔÙд.