commons-email筆記

java 代碼
 
  1. package example;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import javax.mail.internet.AddressException;  
  8. import javax.mail.internet.InternetAddress;  
  9. import javax.mail.internet.MimeUtility;  
  10.   
  11. import org.apache.commons.mail.Email;  
  12. import org.apache.commons.mail.EmailAttachment;  
  13. import org.apache.commons.mail.EmailException;  
  14. import org.apache.commons.mail.HtmlEmail;  
  15. import org.apache.commons.mail.MultiPartEmail;  
  16.   
  17. public class SendMail3 {  
  18.   
  19.     public void sendMail() throws AddressException,EmailException {  
  20.         try {  
  21.             Email htmlEmail = new HtmlEmail();  
  22.             // STMP服務器  
  23.             htmlEmail.setHostName("smtp.sina.com.cn");  
  24.             htmlEmail  
  25.                     .setAuthentication("[email protected]""wswlyn841013");  
  26.             htmlEmail.addTo("[email protected]");  
  27.   
  28.             List list = new ArrayList();  
  29.             // throw AddressException  
  30.             list.add(new InternetAddress("[email protected]"));  
  31.             list.add(new InternetAddress("[email protected]"));  
  32.             htmlEmail.setTo(list);  
  33.             // addTo(String email),就是向一個List添加email,  
  34.             // setTo(list)是批量添加email  
  35.   
  36.             htmlEmail.setFrom("[email protected]");  
  37.             htmlEmail.setCharset("GBK");  
  38.             htmlEmail.setSubject("email 測試");  
  39.             htmlEmail.setMsg("email 測試");  
  40.   
  41.             htmlEmail.send();  
  42.             System.out.println("end");  
  43.         } catch (EmailException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.   
  48.     public void sendEmailAttachment() throws UnsupportedEncodingException {  
  49.         EmailAttachment attachment = new EmailAttachment();  
  50.         attachment.setPath("D:\\eBook\\CSS.chm");  
  51.         attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  52.         attachment.setDescription("Picture of John");//附件描述  
  53.         attachment.setName("CSS.chm");// 名字必須帶文件擴展名  
  54.         attachment.setName(MimeUtility.encodeText("需傳送的附件.txt"));  
  55.           
  56. //      HtmlEmail hemail=new HtmlEmail();爲MultiPartEmail子類  
  57. //      hemail.attach(attachment);  
  58.         MultiPartEmail email = new MultiPartEmail();  
  59.         email.setCharset("GBK");  
  60.         email.setHostName("smtp.sina.com.cn");  
  61.         email.setAuthentication("[email protected]""wswlyn841013");  
  62.         try {  
  63.             email.addTo("[email protected]""martin");  
  64.             email.setFrom("[email protected]""martin");  
  65.             email.setSubject("郵件主題");  
  66.             email.setMsg("郵件內容");  
  67.             //中文:將setMsg替換爲setContent()  
  68.             email.setContent("This is a simple test of commons-email""text/plain;charset=GBK");  
  69.             // add attachment  
  70.             email.attach(attachment);  
  71.   
  72.             email.send();  
  73.             System.out.println("end");  
  74.         } catch (EmailException e) {  
  75.             e.printStackTrace();  
  76.         }  
  77.   
  78.     }  
  79. }  
發佈了2 篇原創文章 · 獲贊 1 · 訪問量 4144
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章