Java快速开发平台

www.fastunit.com

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  23 Posts :: 0 Stories :: 273 Comments :: 0 Trackbacks

一、简介

Commons Email封装了javamail,使用非常简单。
功能:简单文本邮件、带附件的邮件、HTML格式的邮件
官方首页:http://commons.apache.org/email/,目前最新版本是1.1
官方示例:http://commons.apache.org/email/userguide.html

使用前需引入3个jar文件:commons-email-1.1.jar、javamail.jar、activation.jar

二、简单文本邮件

    SimpleEmail email = new SimpleEmail();
    email.setHostName(
"mail.fastunit.com");
    email.setAuthentication(
"support@fastunit.com""***");//邮件服务器验证:用户名/密码
    email.setCharset("UTF-8");// 必须放在前面,否则乱码
    email.addTo("fastunit.com@hotmail.com");
    email.setFrom(
"support@fastunit.com""support");
    email.setSubject(
"subject中文");
    email.setMsg(
"msg中文");
    email.send();

三、带附件的邮件

    MultiPartEmail email = new MultiPartEmail();
    email.setHostName(
"mail.fastunit.com");
    email.setAuthentication(
"support@fastunit.com""***");
    email.setCharset(
"UTF-8");
    email.addTo(
"fastunit.com@hotmail.com");
    email.setFrom(
"support@fastunit.com""support");
    email.setSubject(
"subject中文");
    email.setMsg(
"msg中文");
    
    EmailAttachment attachment 
= new EmailAttachment();
    attachment.setPath(
"d:/a.gif");// 本地文件
    
// attachment.setURL(new URL("http://xxx/a.gif"));//远程文件
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription(
"a");
    attachment.setName(
"a");
    
    email.attach(attachment);
    email.send();

四、HTML格式邮件

    HtmlEmail email = new HtmlEmail();
    email.setHostName(
"mail.fastunit.com");
    email.setAuthentication(
"support@fastunit.com""***");
    email.setCharset(
"UTF-8");
    email.addTo(
"fastunit.com@hotmail.com");
    email.setFrom(
"support@fastunit.com""support");
    email.setSubject(
"subject中文");
    email.setHtmlMsg(
"<b>msg中文</b>");
    email.send();

五、其他

使用免费邮箱来测试时,有些免费邮箱不提供此服务,无法通过邮件服务器验证。

posted on 2008-01-25 16:40 FastUnit 阅读(8613) 评论(3)  编辑  收藏 所属分类: Java

Feedback

# re: 用Apache Commons Email简化邮件发送 2008-01-26 10:39 如坐春风
不错。  回复  更多评论
  

# re: 用Apache Commons Email简化邮件发送 2008-01-26 21:06 千里冰封
确实不错  回复  更多评论
  

# re: 用Apache Commons Email简化邮件发送 2013-08-13 15:50 october
着实不错  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: