Commons-Email 简单教程

一:Quick Start  
通过SimpleEmail发送邮件  
Java代码    收藏代码
  1. java.lang.Object  
  2. org.apache.commons.mail.Email  
  3. org.apache.commons.mail.SimpleEmail  
  4.   
  5. SimpleEmail email = new SimpleEmail();  
  6. email.setHostName("mail.4ya.cn");  
  7. email.setAuthentication("<username>","<password>")  
  8. email.addTo("martin.xus@gmail.com""martin");  
  9. email.setFrom("martin@4ya.cn""martin");  
  10. email.setSubject("测试主题");  
  11. email.setMsg("这里是邮件内容");  
  12. email.send();  

就如代码里字面上的意思一样简单:  
1:创建以SimpleEmail对象 
2:设定发送信件的smtp服务器,如果没有设定,会寻找系统变量中mail.host值。 
3:设定smtp的用户和密码 
4:收件人 
5:发件人 
6:主题 
7:内容 
8:发送
 

二:发送带附件的邮件  
我们可以发送本机的附件,当然我们也可以发送非本机的附件,如果发送的是一个存在网络上的附件的url,则邮件发送的时候会自动下载,添加到附件中。  

 
Java代码    收藏代码
  1.  1:)发送本地附件:  
  2. EmailAttachment attachment = new EmailAttachment();  
  3. attachment.setPath("test/test.rar");  
  4. attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  5. attachment.setDescription("python resource");  
  6. attachment.setName("resource");  
  7.   
  8.    2:)发送不存在本地的附件  
  9. EmailAttachment attachment = new EmailAttachment();  
  10. attachment.setURL(new URL("http://www.smilinglibrary.org/sldoc/pics/index03.jpg"));  
  11. attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  12. attachment.setDescription("微笑图书馆");  
  13. attachment.setName("微笑图书馆");  
  14.   
  15.   
  16. next,添加附件到我们的邮件中  
  17. MultiPartEmail email = new MultiPartEmail();  
  18. email.setHostName("mail.4ya.cn");  
  19. email.setAuthentication("<username>","<password>")  
  20. email.addTo("martin.xus@gmail.com""martin");  
  21. email.setFrom("martin@4ya.cn""martin");  
  22. email.setSubject("邮件主题");  
  23. email.setMsg("邮件内容");  
  24.   
  25. //添加附件  
  26. email.attach(attachment);  
  27.   
  28. //发送邮件  
  29. email.send();  
  30.   
  31. 如果需要发送多个附件,只需创建多个EmailAttachement,即可  
  32. email.attach(attachment1)  
  33. email.attach(attachment2)  

三:发送html格式的邮件  
Java代码    收藏代码
  1. 通过HtmlEmail我们可以发送Html格式的邮件:  
  2.   
  3. java.lang.Object  
  4. org.apache.commons.mail.Email  
  5. org.apache.commons.mail.MultiPartEmail  
  6. org.apache.commons.mail.HtmlEmail  
  7.   
  8.   
  9. 如下:  
  10. 1//HtmlEmail!  
  11. HtmlEmail email = new HtmlEmail();  
  12. email.setHostName("mail.4ya.cn");  
  13. email.setAuthentication("<username>","<password>")  
  14. email.addTo("martin@4ya.cn"martin");  
  15. email.setFrom("martin.xus@gmail.com"martin");  
  16. email.setSubject("主题:该邮件包括html格式内容");  
  17.    
  18. // embed the image and get the content id  
  19. // 注意这里:embed 将帮助我们创建标签如:cid:xxx url  
  20. URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");  
  21. String cid = email.embed(url, "Apache logo");  
  22.   
  23. /** 
  24. set the html message 
  25. 我们看到HtmlEmail extends Email的,它依然有setMsg(),但是这里发送的邮件包括了插入在邮件内容中的图片,所以不能在使用了setMsg(),而要以setHtmlMsg 或setTextMsg代码 
  26. **/  
  27. email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>");  
  28.   
  29. // set the alternative message  
  30. email.setTextMsg("Your email client does not support HTML messages");  
  31.   
  32. //set mail  
  33. email.send();  


四:最后一步  
如果需要实现更复杂authenticator 你可以extends javax.mail.Authenticator ,实现你自己的东西,然后调用Email.setAuthenticator(javax.mail.Authenticator newAuthenticator)即可  

这一点jakarta也做了,给我们提供了一个defaultAuthenticator  
java.lang.Object  
javax.mail.Authenticator  
org.apache.commons.mail.DefaultAuthenticator  

覆盖掉该方法,实现你自己的东东 o_o  
protected javax.mail.PasswordAuthentication getPasswordAuthentication()  

顺便再整理一下,朋友讨论的关于一些jakarta commons email出现乱码的问题:  

一:通过SimpleEmail发送中文内容出现乱码的问题  
SimpleEmail的代码如下  
Java代码    收藏代码
  1. public class SimpleEmail extends Email {  
  2.     /** 
  3.      * Set the content of the mail 
  4.      * 
  5.      * @param msg A String. 
  6.      * @return An Email. 
  7.      * @throws EmailException see javax.mail.internet.MimeBodyPart 
  8.      *                        for definitions 
  9.      * @since 1.0 
  10.      */  
  11.     public Email setMsg(String msg) throws EmailException {  
  12.         if (EmailUtils.isEmpty(msg)) {  
  13.             throw new EmailException("Invalid message supplied");  
  14.         }  
  15.         setContent(msg, Email.TEXT_PLAIN);  
  16.         return this;  
  17.     }  
  18. }  

只是采用默认的,  

1public static final String TEXT_PLAIN = "text/plain";  

并没有指定编码。  

如果通过SimpleEmail发送,需要指定编码:Water Ye@ITO 的说明  
   
1email.setContent("测试邮件", "text/plain;charset=GBK");  

二:关于附件中文名称乱码的问题:  

需使用MimeUtility  

原因是在MIME的相应规范中(RFC2047等)说明了附件标题必须是US-ASCII字符, 所以在发送中文标题的附件时需要编码成US-ASCII字符, 有两种编码方式: B (BASE64), Q (Quoted-Printable), 这些方法在MimeUtility里  
都已经做了封装, 所以在发送附件时使用如下:  
Java代码    收藏代码
  1. 1MimeUtility.encodeText(filename));  
  2.   
  3.   
  4.         EmailAttachment attachment = new EmailAttachment();  
  5.         attachment.setPath("c:\\测试.txt");  
  6.         attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  7.         attachment.setDescription("测试文件");  
  8.            
  9.            //  
  10.         attachment.setName(MimeUtility.encodeText("测试文件.txt"));  
  11.   
  12.          MultiPartEmail email = new MultiPartEmail();  
  13.         email.setHostName("192.168.0.3");  
  14.         email.setAuthentication("martin.xus""1234");  
  15.         email.addTo("martin.xus@192.168.0.3""martin");  
  16.         email.setFrom("martin.xus@192.168.0.3""martin");  
  17.   
  18.         email.setSubject("测试带附件");  
  19.         email.setMsg("该邮件含附件");  
  20.         //添加附件  
  21.         email.attach(attachment);  
  22.         //发送邮件  
  23.         email.send();  
  24.   
  25.   
  26. end  
  27. --------------------------------------------------------------------------------- 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值