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("weishuwei112@sina.com""wswlyn841013");  
  26.             htmlEmail.addTo("weishuwei112@sina.com");  
  27.   
  28.             List list = new ArrayList();  
  29.             // throw AddressException  
  30.             list.add(new InternetAddress("jonekolly@126.com"));  
  31.             list.add(new InternetAddress("weishuwei112@sina.com"));  
  32.             htmlEmail.setTo(list);  
  33.             // addTo(String email),就是向一个List添加email,  
  34.             // setTo(list)是批量添加email  
  35.   
  36.             htmlEmail.setFrom("weishuwei112@sina.com");  
  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("weishuwei112@sina.com""wswlyn841013");  
  62.         try {  
  63.             email.addTo("weishuwei112@sina.com""martin");  
  64.             email.setFrom("weishuwei112@sina.com""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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值