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. }  

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