commons-email是apache提供的一个开源的API,是对javamail的封装,因此在使用时要将javamail.jar加 到 class path中,主要包括SimpleEmail,MultiPartEmail,HtmlEmail,EmailAttachment四个类。
SimpleEmail:发送简单的email,不能添加附件
MultiPartEmail:文本邮件,可以添加多个附件
HtmlEmail:HTML格式邮件,同时具有MultiPartEmail类所有“功能”
EmailAttchment:附件类,可以添加本地资源,也可以指定网络上资源,在发送时自动将网络上资源下载发送。
发送基本文本格式邮件:
==============
SimpleEmail email = new SimpleEmail();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//发送
email.send();
发送文本格式,带附件邮件:
==================
//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
email.send();
发送HTML格式带附件邮件:
=================
//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
HtmlEmail email = new HtmlEmail ();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setHtmlMsg(" This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
SimpleEmail:发送简单的email,不能添加附件
MultiPartEmail:文本邮件,可以添加多个附件
HtmlEmail:HTML格式邮件,同时具有MultiPartEmail类所有“功能”
EmailAttchment:附件类,可以添加本地资源,也可以指定网络上资源,在发送时自动将网络上资源下载发送。
发送基本文本格式邮件:
==============
SimpleEmail email = new SimpleEmail();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//发送
email.send();
发送文本格式,带附件邮件:
==================
//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
MultiPartEmail email = new MultiPartEmail();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setMsg("This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
email.send();
发送HTML格式带附件邮件:
=================
//附件,可以定义多个附件对象
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("e://1.pdf");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
//
HtmlEmail email = new HtmlEmail ();
//smtp host
email.setHostName("mail.test.com");
//登陆邮件服务器的用户名和密码
email.setAuthentication("test","testpassword");
//接收人
email.addTo("jdoe@somewhere.org", "John Doe");
//发送人
email.setFrom("me@apache.org", "Me");
//标题
email.setSubject("Test message");
//邮件内容
email.setHtmlMsg(" This is a simple test of commons-email");
//添加附件
email.attach(attachment);
//发送
下面提供一个完整的程序示例:
package zieckey
import org.apache.commons.mail.*;
public class SendEMail
{
public static void main ( String[] arg ) throws Exception
{
SimpleEmail email = new SimpleEmail ( );
// smtp host
email.setHostName ( "smtp.163.com" );
// 登陆邮件服务器的用户名和密码
email.setAuthentication ( "zieckey", "123456" );
// 接收人
email.addTo ( "zieckey@yahoo.com.cn", "Zieckey" );
// 发送人
email.setFrom ( "zieckey@163.com", "Me" );
// 标题
email.setSubject ( "Test message" );
// 邮件内容
email.setMsg ( "This is a simple test of commons-email" );
// 发送
email.send ( );
System.out.println ( "Send email successful!" );
}
}