注: 本文转载是http://blog.csdn.net/yuaoi/archive/2010/08/18/5818561.aspx 下,仅供自己学习参考。
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、先来一个简单文本邮件发送的例子:
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("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、带附件的邮箱发送:
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("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链接的:
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("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格式邮件发送:
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("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了。
分享到:
相关推荐
这份资料是Apache Commons Email的内容,希望能给有志成为IT人才一点帮助!! Thank you!
Java通过Apache提供的Commons Email工具类实现邮箱发送验证码功能,代码附详细注释文本,实现方法中附有验证码生成方法
apache commons jar(commons所有的jar包,从官网下载提供给大家) 因为涉及jar太多,包括有src源代码,只需要3分,希望大家理解,我也是从官网花了很长时间才一个一个下完,需要的请自取。全部是zip文件,每个对应的...
这份资料是+++利用Apache Commons Email发送邮件+++的内容,希望能给有志成为IT人才一点帮助!你的进步就是对我最大的回报! Thank you!
Apache Commons官网jar包,包含io,cli,codec,net,lang,email等等等等
Maven坐标:org.apache.commons:commons-email:1.5; 标签:apache、commons、email、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,...
daemon-1.0.15-bin commons-dbutils-1.6-bin commons-digester3-3.2-bin commons-el-1.0 commons-email-1.4-bin commons-fileupload-1.0 commons-fileupload-1.1.1 commons-fileupload-1.1 commons-file upload-...
commons-attributes-2.2-src.zip commons-attributes-2.2.zip commons-beanutils-1.8.0-BETA-src.zip commons-beanutils-1.8.0-BETA.zip commons-betwixt-0.8-src.zip commons-betwixt-0.8.zip ...
Apache-commons源码其中包括(commons-email-1.5-src、commons-fileupload-1.4-src、commons-io-2.8.0-src、commons-jelly-1.0.1-src、commons-lang3-3.11-src...)
org.apache.commons相关的所以jar包,包括commons-beanutils-1.8.0-bin.zip;commons-betwixt-0.8.zip;commons-cli-1.1.zip;commons-codec-1.3.zip;commons-collections-3.2.1-bin.zip;commons-digester-1.8.zip...
与Apache Commons Email使用有关的问题应张贴到。在哪里可以获得最新版本? 您可以从我们的下载源代码和二进制文件。 或者,您可以从中央Maven存储库中提取它: < dependency> < groupId>org.apache.commons...
NULL 博文链接:https://liuzidong.iteye.com/blog/1116785
commons-attributes-2.2-src.zip commons-attributes-2.2.zip commons-beanutils-1.8.0-BETA-src.zip commons-beanutils-1.8.0-BETA.zip commons-betwixt-0.8-src.zip commons-betwixt-0.8.zip ...
Maven坐标:org.apache.commons:commons-email:1.5; 标签:apache、commons、email、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译...
用 apache commons email 发送带附件,HTML 格式的 邮件 格式例子 还包括commons-email的jar包,及其他相关资料
org.apache.commons的jar包,Apache Commons包含了很多开源的工具。 包括commons-beanutils-1.8.0-bin、commons-betwixt-0.8、commons-cli-1.1、commons-codec-1.3、commons-collections-3.2.1-bin、commons-...
commons-email-1.2-bin.zip commons-exec-1.1-bin.zip commons-fileupload-1.2.2-bin.zip commons-io-2.4-bin.zip commons-jci-1.0-bin.zip commons-jelly-1.0.zip 剩下的在源文件2.由于超过60M,分成两部分
包括 commons-beanutils-core-1.8.0.jar commons-collections-3.2.1....commons-email-1.2.jar commons-io-1.3.2.jar commons-lang-2.5.jar commons-logging-1.1.1.jar commons-net-1.4.1.jar commons-pool-1.5.4.jar
org.apache.commons 的经典jar 包 commons-beanutils-1.8.0-bin、 commons-betwixt-0.8、 commons-cli-1.1、 commons-codec-1.3、 commons-collections-3.2.1-bin、 commons-digester-1.8、 commons-...