package com.hf.mail;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
public class mailSender {
/**
* @param args
*/
public static void main(String[] args) {
//不要使用SimpleEmail,会出现乱码问题
HtmlEmail email = new HtmlEmail();
try{
//这里是发送服务器的名字:,163的如下:
email.setHostName("smtp.163.com");
//编码集的设置
email.setCharset("gbk");
//收件人的邮箱
email.addTo("[email protected]");
//发送人的邮箱
email.setFrom("abc@163com", "发送人名称");
//如果需要认证信息的话,设置认证:用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
email.setAuthentication("abc","123");
email.setSubject("下午3:00会议室讨论,请准时参加");
//要发送的信息
email.setMsg("下午3:00会议室讨论,请准时参加");
//发送
email.send();
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
-------------------------------------------------2006/06/12 21:16-------------------------------------------------------------------
今天有同事用了上面讲的方法但是出现错误,在此表示歉意,原因是忘了导入所需要的包:
根据apache的说法:
REQUIREMENTS:
J2SE environments
-----------------
Commons-Email requires at least javamail version 1.2 and should run
with all versions above. Get it http://java.sun.com/products/javamail/
Commons-Email also requires the Java activation framework. Get it from
http://java.sun.com/products/javabeans/glasgow/jaf.html
Commons-Email has been tested with JDK 1.3.1_16, JDK 1.4.2_09 and JDK 1.5.0_04.
J2EE environments
-----------------
Commons-Email should be compatible with J2EE 1.3 and 1.4 compliant
environments and run without additional requirements.
如果在j2se下,需要导入javamail 包以及jaf包,文件可以在上面的两个地址上下载;
如果是兼容J2EE 1.3或 1.4的环境下,则无须其他的附加包.
但是我在resin3.014下测试还是抛出:
[21:31:26.000] javax.servlet.ServletException: java.lang.NoClassDefFoundError: javax/mail/Message
异常.
导入上述两个包之后一切正常,不知道为啥子…………,反正能用拉。