猫猫1 发表于 2016-12-31 11:01:37

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、先来一个简单文本邮件发送的例子:

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("yuaio@163.com", "yuaio"));   
try      
{   
email.setFrom("yuaio@163.com"); //发送方,这里可以写多个   
email.addTo("www@gmail.com"); // 接收方   
email.addCc("402******@qq.com"); // 抄送方   
email.addBcc("yuaio@163.com"); // 秘密抄送方   
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("yuaio@163.com", "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、带附件的邮箱发送:

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()   
{   
MultiPartEmailemail = new MultiPartEmail();      
//email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验   
email.setDebug(true);   
//email.setSSL(true);         
email.setHostName("smtp.sina.com");   
email.setAuthenticator(new DefaultAuthenticator("java@sina.com", "******"));   
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("java@sina.com"); //发送方   
email.addTo("java@gmail.com"); // 接收方   
//email.addCc("java@qq.com"); // 抄送方   
//email.addBcc("java@163.com"); // 秘密抄送方   
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链接的:

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()   
{   
MultiPartEmailemail = new MultiPartEmail();      
email.setTLS(true); //是否TLS校验,,某些邮箱需要TLS安全校验,同理有SSL校验   
email.setDebug(false);   
//email.setSSL(true);         
email.setHostName("smtp.sina.com");   
email.setAuthenticator(new DefaultAuthenticator("java@sina.com", "******"));   
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("java@sina.com"); //发送方   
email.addTo("java@gmail.com"); // 接收方   
//email.addCc("1234567@qq.com"); // 抄送方   
//email.addBcc("java@163.com"); // 秘密抄送方   
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格式邮件发送:

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("java@163.com", "******"));   
try {   
email.setCharset("GB2312");   
email.addTo("java@gmail.com", "java");   
email.setFrom("java@163.com", "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();   
}   
}

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

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了。
页: [1]
查看完整版本: Apache Commons Email 简单运用