使用JAVAMAIL发邮件的一个例子
首先引入j2ee的包路径到ClassPath,已经安装Jbuilder的可以在它的安装路径lib目录下找到mail.jar,activation.jar,将这两个包加入系统的环境变量,那么即使不用JBuilder开发的话也可以
使用了.
程序:
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
* <p>Title: 简易MailBean</p>
* <p>Description: 提供简便的Mail发送功能(/p>
* <p>Copyright: Copyright (c) 2003</p>
* @version 1.0
*/
class ExtendString {
public ExtendString() {
}
/**
去掉字符串两端的空白字符,并将字符串转化为中国的标准字符gb2312的字符串.
*/
public String CS(String str) { //去掉字符串2端的空白字符
try {
if (str == null)
return "";
str = str.trim();
if (str == null)
return "";
str = new String(str.getBytes("8859_1"), "GBK");
}
catch (Exception e) {
System.out.println(e);
}
return str;
}
}
public class SendMail
{
private String errMsg = "";
private ExtendString ExStr = new ExtendString();
private String sender = "";//发件人地址
private String smtpHost = "";//邮件发送服务器(smtp)
private String user = ""; //登录用户名
private String password = "";//登录密码
private String subject = "";//mail主题
public SendMail()
{
this.setPropertiesAttri();
}
private void setPropertiesAttri()
{
try
{
InputStream is = getClass().getResourceAsStream("MailServer.properties");
Properties prop = new Properties();
prop.load(is);
this.setSmtpHost(prop.get("SmtpHost").toString());
补充:Jsp教程,邮件