免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1244 | 回复: 0
打印 上一主题 下一主题

使用Apache Commons-email组件发送邮件,简单极了 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-23 20:08 |只看该作者 |倒序浏览
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);
//发送


下面提供一个完整的程序示例:
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!" );
    }
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/16292/showart_388534.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP