commons-email笔记

  1. import  java.io.UnsupportedEncodingException;  
  2. import  java.util.ArrayList;  
  3. import  java.util.List;  
  4.   
  5. import  javax.mail.internet.AddressException;  
  6. import  javax.mail.internet.InternetAddress;  
  7. import  javax.mail.internet.MimeUtility;  
  8.   
  9. import  org.apache.commons.mail.Email;  
  10. import  org.apache.commons.mail.EmailAttachment;  
  11. import  org.apache.commons.mail.EmailException;  
  12. import  org.apache.commons.mail.HtmlEmail;  
  13. import  org.apache.commons.mail.MultiPartEmail;  
  14.   
  15. public   class  SendMail3 {  
  16.   
  17.     public   void  sendMail()  throws  AddressException,EmailException {  
  18.         try  {  
  19.             Email htmlEmail = new  HtmlEmail();  
  20.             // STMP服务器   
  21.             htmlEmail.setHostName("smtp.sina.com.cn" );  
  22.             htmlEmail  
  23.                     .setAuthentication("[email protected]" "wswlyn841013" );  
  24.             htmlEmail.addTo("[email protected]" );  
  25.   
  26.             List list = new  ArrayList();  
  27.             // throw AddressException   
  28.             list.add(new  InternetAddress( "[email protected]" ));  
  29.             list.add(new  InternetAddress( "[email protected]" ));  
  30.             htmlEmail.setTo(list);  
  31.             // addTo(String email),就是向一个List添加email,   
  32.             // setTo(list)是批量添加email   
  33.   
  34.             htmlEmail.setFrom("[email protected]" );  
  35.             htmlEmail.setCharset("GBK" );  
  36.             htmlEmail.setSubject("email 测试" );  
  37.             htmlEmail.setMsg("email 测试" );  
  38.   
  39.             htmlEmail.send();  
  40.             System.out.println("end" );  
  41.         } catch  (EmailException e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.     }  
  45.   
  46.     public   void  sendEmailAttachment()  throws  UnsupportedEncodingException {  
  47.         EmailAttachment attachment = new  EmailAttachment();  
  48.         attachment.setPath("D:\\eBook\\CSS.chm" );  
  49.         attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  50.         attachment.setDescription("Picture of John" ); //附件描述   
  51.         attachment.setName("CSS.chm" ); // 名字必须带文件扩展名   
  52.         attachment.setName(MimeUtility.encodeText("需传送的附件.txt" ));  
  53.           
  54. //      HtmlEmail hemail=new HtmlEmail();为MultiPartEmail子类   
  55. //      hemail.attach(attachment);   
  56.         MultiPartEmail email = new  MultiPartEmail();  
  57.         email.setCharset("GBK" );  
  58.         email.setHostName("smtp.sina.com.cn" );  
  59.         email.setAuthentication("[email protected]" "wswlyn841013" );  
  60.         try  {  
  61.             email.addTo("[email protected]" "martin" );  
  62.             email.setFrom("[email protected]" "martin" );  
  63.             email.setSubject("邮件主题" );  
  64.             email.setMsg("邮件内容" );  
  65.             //中文:将setMsg替换为setContent()   
  66.             email.setContent("This is a simple test of commons-email" "text/plain;charset=GBK" );  
  67.             // add attachment   
  68.             email.attach(attachment);  
  69.   
  70.             email.send();  
  71.             System.out.println("end" );  
  72.         } catch  (EmailException e) {  
  73.             e.printStackTrace();  
  74.         }  
  75.   
  76.     }  

你可能感兴趣的:(apache,css)