Apache Commons Email 简单使用

Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.

Some of the mail classes that are provided are as follows:

  • SimpleEmail - This class is used to send basic text based emails.
  • MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
  • HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
  • EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.

说明:这个是Apache的一个开源项目,是基于另一个开源项目Java Mail上而进行封装的,使用起来更加简单方便。官网:

http://commons.apache.org/email/index.html

首先下载jar包:commons-email-1.2.jar

http://repo1.maven.org/maven2/org/apache/commons/commons-email/1.2/commons-email-1.2.jar

 

1、先来一个简单文本邮件发送的例子:

[java] view plain copy print ?
  1. package com.mail.test;  
  2. import org.apache.commons.mail.DefaultAuthenticator;  
  3. import org.apache.commons.mail.EmailException;       
  4. import org.apache.commons.mail.SimpleEmail;        
  5.    
  6. public class BaseEmailSend     
  7. {    
  8.     public BaseEmailSend()    
  9.     {    
  10.             
  11.     }    
  12.         
  13.         
  14.     public static void send()    
  15.     {    
  16.         SimpleEmail email = new SimpleEmail();       
  17.         //email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验  
  18.         email.setDebug(true);  
  19.         email.setSSL(true);          
  20.         email.setHostName("smtp.163.com");   
  21.         email.setAuthenticator(new DefaultAuthenticator("[email protected]""yuaio"));  
  22.         try     
  23.         {    
  24.          email.setFrom("[email protected]"); //发送方,这里可以写多个  
  25.          email.addTo(www@gmail.com); // 接收方  
  26.          email.addCc("402******@qq.com"); // 抄送方  
  27.          email.addBcc("[email protected]"); // 秘密抄送方  
  28.          email.setCharset("GB2312");  
  29.          email.setSubject("标题哦"); // 标题  
  30.          email.setMsg("测试测试内容,请查阅!!!);// 内容  
  31.          email.send();  
  32.          System.out.println("发送成功");  
  33.         } catch (EmailException e) {    
  34.             e.printStackTrace();    
  35.         }     
  36.     }    
  37.       
  38.     public static void main(String[] args)    
  39.     {    
  40.         send();    
  41.     }    
  42. }  

package com.mail.test; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.SimpleEmail; public class BaseEmailSend { public BaseEmailSend() { } public static void send() { SimpleEmail email = new SimpleEmail(); //email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验 email.setDebug(true); //email.setSSL(true); email.setHostName("smtp.163.com"); email.setAuthenticator(new DefaultAuthenticator("[email protected]", "yuaio")); try { email.setFrom("[email protected]"); //发送方,这里可以写多个 email.addTo([email protected]); // 接收方 email.addCc("402******@qq.com"); // 抄送方 email.addBcc("[email protected]"); // 秘密抄送方 email.setCharset("GB2312"); email.setSubject("标题哦"); // 标题 email.setMsg("测试测试内容,请查阅!!!);// 内容 email.send(); System.out.println("发送成功"); } catch (EmailException e) { e.printStackTrace(); } } public static void main(String[] args) { send(); } }

注意:email.setHostName("smtp.163.com");
         email.setAuthenticator(new DefaultAuthenticator("[email protected]", "yuaio"));

还有email.setTLS(true); email.setSSL(false); 这些都应该是对应的,使用不同的服务商邮箱,这里的HostName需要改一下,同时安全校验也是不同的,据我测试:只有google gmail邮箱这两个校验都需要(google邮箱是我的最爱,好用,快速,最最重要的是安全,给你足够的隐私权。)设email.setTLS(true); email.setSSL(true);

163:两个都不需要校验就能通过;

sina:两个都不需要校验就能通过;

qq邮箱:需要需要校验tls;

email.setDebug(true); 开启debug模式,可以打印一些信息。

 

 

2、带附件的邮箱发送:

[java] view plain copy print ?
  1. package com.mail.test;  
  2. import org.apache.commons.mail.DefaultAuthenticator;  
  3. import org.apache.commons.mail.EmailAttachment;  
  4. import org.apache.commons.mail.EmailException;       
  5. import org.apache.commons.mail.MultiPartEmail;  
  6.    
  7. public class EmailWithAffixSend     
  8. {   
  9.     public EmailWithAffixSend()    
  10.     {    
  11.             
  12.     }    
  13.         
  14.         
  15.     public static void send()    
  16.     {    
  17.      MultiPartEmail  email = new MultiPartEmail();       
  18.         //email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验  
  19.         email.setDebug(true);  
  20.         email.setSSL(true);          
  21.         email.setHostName("smtp.sina.com");   
  22.         email.setAuthenticator(new DefaultAuthenticator("[email protected]""******"));  
  23.         try     
  24.         {    
  25.    // Create the attachment  
  26.    EmailAttachment attachment = new EmailAttachment();  
  27.    //绝对路径  
  28.    attachment.setPath("F://dgjl.jpg");  
  29.    attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  30.    attachment.setDescription("Picture of John");  
  31.    attachment.setName("John");  
  32.    email.setFrom("[email protected]"); //发送方  
  33.    email.addTo("[email protected]"); // 接收方  
  34.    //email.addCc("[email protected]"); // 抄送方  
  35.    //email.addBcc("[email protected]"); // 秘密抄送方  
  36.    email.setCharset("GB2312");//编码  
  37.    email.setSubject("有附件!!!"); // 标题  
  38.    email.setMsg("邮件发送测试"); // 内容  
  39.    email.attach(attachment);  
  40.    email.send();  
  41.    System.out.println("发送成功");  
  42.         } catch (EmailException e) {    
  43.             e.printStackTrace();    
  44.         }     
  45.     }    
  46.       
  47.     public static void main(String[] args)    
  48.     {    
  49.         send();    
  50.     }    
  51. }  

package com.mail.test; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.MultiPartEmail; public class EmailWithAffixSend { public EmailWithAffixSend() { } public static void send() { MultiPartEmail email = new MultiPartEmail(); //email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验 email.setDebug(true); //email.setSSL(true); email.setHostName("smtp.sina.com"); email.setAuthenticator(new DefaultAuthenticator("[email protected]", "******")); try { // Create the attachment EmailAttachment attachment = new EmailAttachment(); //绝对路径 attachment.setPath("F://dgjl.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Picture of John"); attachment.setName("John"); email.setFrom("[email protected]"); //发送方 email.addTo("[email protected]"); // 接收方 //email.addCc("[email protected]"); // 抄送方 //email.addBcc("[email protected]"); // 秘密抄送方 email.setCharset("GB2312");//编码 email.setSubject("有附件!!!"); // 标题 email.setMsg("邮件发送测试"); // 内容 email.attach(attachment); email.send(); System.out.println("发送成功"); } catch (EmailException e) { e.printStackTrace(); } } public static void main(String[] args) { send(); } }

3、附件图片是url链接的:

[java] view plain copy print ?
  1. package com.mail.test;  
  2. import java.net.MalformedURLException;  
  3. import java.net.URL;  
  4. import org.apache.commons.mail.DefaultAuthenticator;  
  5. import org.apache.commons.mail.EmailAttachment;  
  6. import org.apache.commons.mail.EmailException;       
  7. import org.apache.commons.mail.MultiPartEmail;  
  8. import org.apache.commons.mail.SimpleEmail;        
  9.    
  10. public class EmailWithUrlAffixSend     
  11. {    
  12.     public EmailWithUrlAffixSend()    
  13.     {    
  14.             
  15.     }    
  16.         
  17.         
  18.     public static void send()    
  19.     {    
  20.      MultiPartEmail  email = new MultiPartEmail();       
  21.         email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验  
  22.         email.setDebug(false);  
  23.         email.setSSL(true);          
  24.         email.setHostName("smtp.sina.com");   
  25.         email.setAuthenticator(new DefaultAuthenticator("[email protected]""******"));  
  26.         try     
  27.         {    
  28.    // Create the attachment  
  29.    EmailAttachment attachment = new EmailAttachment();  
  30.    attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif"));  
  31.    attachment.setDisposition(EmailAttachment.ATTACHMENT);  
  32.    attachment.setDescription("Apache logo");  
  33.    attachment.setName("我的份");  
  34.   
  35.    email.setFrom("[email protected]"); //发送方  
  36.    email.addTo("[email protected]"); // 接收方  
  37.    //email.addCc("[email protected]"); // 抄送方  
  38.    //email.addBcc("[email protected]"); // 秘密抄送方  
  39.    email.setCharset("GB2312");  
  40.    email.setSubject("有附件!!!"); // 标题  
  41.    email.setMsg("邮件发送测试。"); // 内容  
  42.    email.attach(attachment);  
  43.    email.send();  
  44.    System.out.println("发送成功");  
  45.         } catch (EmailException e) {    
  46.             e.printStackTrace();    
  47.         }catch (MalformedURLException e) {  
  48.    e.printStackTrace();  
  49.   }     
  50.     }    
  51.       
  52.     public static void main(String[] args)    
  53.     {    
  54.         send();    
  55.     }    
  56. }  

package com.mail.test; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.MultiPartEmail; import org.apache.commons.mail.SimpleEmail; public class EmailWithUrlAffixSend { public EmailWithUrlAffixSend() { } public static void send() { MultiPartEmail email = new MultiPartEmail(); email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验 email.setDebug(false); //email.setSSL(true); email.setHostName("smtp.sina.com"); email.setAuthenticator(new DefaultAuthenticator("[email protected]", "******")); try { // Create the attachment EmailAttachment attachment = new EmailAttachment(); attachment.setURL(new URL("http://www.apache.org/images/asf_logo_wide.gif")); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription("Apache logo"); attachment.setName("我的份"); email.setFrom("[email protected]"); //发送方 email.addTo("[email protected]"); // 接收方 //email.addCc("[email protected]"); // 抄送方 //email.addBcc("[email protected]"); // 秘密抄送方 email.setCharset("GB2312"); email.setSubject("有附件!!!"); // 标题 email.setMsg("邮件发送测试。"); // 内容 email.attach(attachment); email.send(); System.out.println("发送成功"); } catch (EmailException e) { e.printStackTrace(); }catch (MalformedURLException e) { e.printStackTrace(); } } public static void main(String[] args) { send(); } }
4、html格式邮件发送:

[java] view plain copy print ?
  1. package com.mail.test;  
  2. import java.net.MalformedURLException;  
  3. import java.net.URL;  
  4. import org.apache.commons.mail.DefaultAuthenticator;  
  5. import org.apache.commons.mail.EmailAttachment;  
  6. import org.apache.commons.mail.EmailException;       
  7. import org.apache.commons.mail.HtmlEmail;  
  8. import org.apache.commons.mail.MultiPartEmail;  
  9. import org.apache.commons.mail.SimpleEmail;        
  10.    
  11. public class HtmlEmailSend     
  12. {    
  13.     public HtmlEmailSend()    
  14.     {    
  15.             
  16.     }    
  17.         
  18.     public static void send()    
  19.     {    
  20.   // Create the email message  
  21.   HtmlEmail email = new HtmlEmail();  
  22.   email.setHostName("smtp.163.com");  
  23.   email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验  
  24.   email.setAuthenticator(new DefaultAuthenticator("[email protected]""******"));  
  25.   try {  
  26.    email.setCharset("GB2312");  
  27.    email.addTo("[email protected]""java");  
  28.    email.setFrom("[email protected]""java");  
  29.    email.setSubject("内嵌图片背景测试");  
  30.    // embed the image and get the content id  
  31.    URL url = new URL("http://www.jianlimuban.com/resume/images/20081112155017201.jpg");  
  32.    String cid = email.embed(url, "Apache logo");  
  33.    // set the html message  
  34.    email.setHtmlMsg("<html>The apache logo - <img src="/" mce_src="/""cid:" + cid + "/"></html>");  
  35.    // 假如图片失效时显示的文字  
  36.    email.setTextMsg("Your email client does not support HTML messages");  
  37.    // send the email  
  38.    email.send();  
  39.    System.out.println("发送成功");  
  40.         } catch (EmailException e) {    
  41.             e.printStackTrace();    
  42.         }catch (MalformedURLException e) {  
  43.    e.printStackTrace();  
  44.   }     
  45.     }    
  46.       
  47.     public static void main(String[] args)    
  48.     {    
  49.         send();    
  50.     }    
  51. }  

package com.mail.test; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.mail.DefaultAuthenticator; import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.MultiPartEmail; import org.apache.commons.mail.SimpleEmail; public class HtmlEmailSend { public HtmlEmailSend() { } public static void send() { // Create the email message HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.163.com"); email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验 email.setAuthenticator(new DefaultAuthenticator("[email protected]", "******")); try { email.setCharset("GB2312"); email.addTo("[email protected]", "java"); email.setFrom("[email protected]", "java"); email.setSubject("内嵌图片背景测试"); // embed the image and get the content id URL url = new URL("http://www.jianlimuban.com/resume/images/20081112155017201.jpg"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg("<html>The apache logo - <img src="/" mce_src="/""cid:" + cid + "/"></html>"); // 假如图片失效时显示的文字 email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send(); System.out.println("发送成功"); } catch (EmailException e) { e.printStackTrace(); }catch (MalformedURLException e) { e.printStackTrace(); } } public static void main(String[] args) { send(); } }

 

异常:开始放在测试项目中还是发送成功的,但是移到另一个项目中,抛异常了:

[java] view plain copy print ?
  1. Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream  

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

查找资料有朋友也遇到过,是因为MyEclipse中系统自带的email.jar与导入的mail.jar包冲突不一致引起的,删除系统中的jar包即可:

这里引用这位朋友的方法吧:

用rar打开X:/Program Files/MyEclipse 6.0/myeclipse/eclipse/plugins/com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710/data/libraryset/EE_5/javaee.jar
,然后删除mail,一切就OK了。

你可能感兴趣的:(java,email)