« NetBeans 6.7 M3值得关注的... | Main | Java读写Excel简介 »
http://developers.sun.com.cn/blog/functionalca/date/20090403 星期五 2009年04月03日

Apache Mail简介

Apache Mail简介
作者:曹祺
Email:Qi.Cao@Sun.Com
Blog:http://blogs.sun.com/greysh
本文难度:入门
源代码下载:
http://developers.sun.com.cn/blog/functionalca/resource/FCA_Greysh_Mail.zip
原文链接:
http://developers.sun.com.cn/blog/functionalca/entry/apache_mail%E7%AE%80%E4%BB%8B 

1.简介
2.Helloworld

  在实际项目项目开发中,很多时候需要用到邮件,比如论坛注册需要用邮件激活。
一般用Javamail发送,目前最新的版本是1.4.2
可以在http://java.sun.com/products/javamail/index.jsp 下载最新版本
如果使用的不是J2SE6,那么需要把 JavaBeans Activation Framework加到环境变量
可以在http://java.sun.com/javase/technologies/desktop/javabeans/jaf/index.jsp 下载
不过为了简化开发,可以直接使用apache common项目的mail
官方网站为: http://commons.apache.org/email/

 apache commons的email项目类的层次结构为
class java.lang.Object

常用的有三种mail类型
SimpleEmail
MultiPartEmail
HtmlEmail
第一种就是最简单的邮件
第二种可以传附件
第三种为HTML邮件

调用起来比直接JavaMail要方便的多,笔者写了一个工具类

Helloworld

package com.greysh.email;

import java.net.MalformedURLException;
import java.net.URL;
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;

/**
 * @author Genix.Cao Blog:http://blogs.sun.com/greysh
 */
public class Email {
 private static final Integer SimpleEmail = 0;
 private static final Integer MultiPartEmail = 1;
 private static final Integer HtmlEmail = 2;
 private String host;
 private String username;
 private String password;
 private String charset;
 private String postEmail;
 private String postName;
 private String receiveEmail;
 private String receiveName;
 private String subject;
 private String content;
 private String path;
 private String name;
 private String description;
 private boolean isLocalFile;
 private boolean isAttach;

 public void initPoster(String host, String username, String password,
   String charset, String postEmail) {
  this.host = host;
  this.username = username;
  this.password = password;
  this.charset = charset;
  this.postEmail = postEmail;
 }

 public void initReceiver(String receiveEmail, String receiveName,
   String subject, String content, boolean isAttach) {
  this.receiveEmail = receiveEmail;
  this.receiveName = receiveName;
  this.subject = subject;
  this.content = content;
  this.isAttach = isAttach;
 }

 public void initAttachment(String path, String name, String description,
   boolean isLocalFile) {
  this.path = path;
  this.name = name;
  this.description = description;
  this.isLocalFile = isLocalFile;
 }

 public void send(Integer emailType) throws EmailException,
   MalformedURLException {
  org.apache.commons.mail.Email email;
  if (emailType.equals(SimpleEmail)) {
   email = new SimpleEmail();
  } else if (emailType.equals(MultiPartEmail)) {
   email = new MultiPartEmail();
   if (isAttach && getEmailAttachment() != null) {
    ((MultiPartEmail) email).attach(getEmailAttachment());
   }
  } else {
   email = new HtmlEmail();
   if (isAttach && getEmailAttachment() != null) {
    ((MultiPartEmail) email).attach(getEmailAttachment());
   }
  }
  email.setHostName(host);
  email.setAuthentication(username, password);
  email.setCharset(charset);
  email.setFrom(postEmail, postName);
  email.addTo(receiveEmail, receiveName);
  email.setSubject(subject);
  if (emailType.equals(HtmlEmail)) {
   ((HtmlEmail) email).setHtmlMsg(content);
  } else {
   email.setMsg(content);
  }
  email.send();
 }

 public EmailAttachment getEmailAttachment() throws MalformedURLException {
  EmailAttachment attachment = new EmailAttachment();
  if (isLocalFile) {
   attachment.setPath(path);
  } else {
   attachment.setURL(new URL(path));
  }
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  attachment.setDescription(description);
  attachment.setName(name);
  return attachment;
 }
}

和javamail相比,使用的时候,程序员跟关注自己的业务,只需要指定发送方和接受方,
以及邮件的类型就可以
在使用笔者的邮件工具类的时候
先调用initPoster初始化发送环境
然后初始化接收方,并明确是否有附件
如果有附件还要初始化附件
apache commons email项目对附件支持很好
可以是本地也可以是远程
如果是远程
只需要指定URL
采用笔者的工具类
可以针对三种不同的邮件类型
如果读者感兴趣,这个工具类可以继续扩展
就笔者感觉,有时候发送邮件是SSL的类型
比如Sun的邮件系统就是SSL的类型
在实际开发的时候,有时候需要传抄送很多人
因此抄送邮件地址是一个List
如果对apache mail的源代码感兴趣,
可以登录
http://commons.apache.org/email/



发表于 Sun Functional 校园大使 [JavaEE] ( 四月 03, 2009 11:43 下午 ) Permalink | 评论[0]
评论:

发表一条评论:
  • HTML语法: 禁用