import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* <p>title: 使用javamail发送邮件</p>
* <p>description: 演示如何使用javamail包发送电子邮件。这个实例可发送多附件</p>
* <p>copyright: copyright (c) 2003</p>
* <p>filename: mail.java</p>
* @version 1.0
*/
public class mail {
string to = "";//收件人
string from = "";//发件人
string host = "";//smtp主机
string username = "" ;
string password = "" ;
string filename = "";//附件文件名
string subject = "";//邮件主题
string content = "";//邮件正文
vector file = new vector();//附件文件集合
/**
*<br>方法说明:默认构造器
*<br>输入参数:
*<br>返回类型:
*/
public mail(){
}
/**
*<br>方法说明:构造器,提供直接的参数传入
*<br>输入参数:
*<br>返回类型:
*/
public mail(string to,string from,string smtpserver,string username,string password,string subject,string content){
this.to = to;
this.from = from;
this.host = smtpserver;
this.username = username;
this.password = password;
this.subject = subject;
this.content = content;
}
/**
*<br>方法说明:设置邮件服务器地址
*<br>输入参数:string host 邮件服务器地址名称
*<br>返回类型:
*/
public void sethost(string host){
this.host = host;
}
/**
*<br>方法说明:设置登录服务器校验密码
*<br>输入参数:
*<br>返回类型:
*/
public void setpassword(string pwd){
this.password = pwd;
}
/**
*<br>方法说明:设置登录服务器校验用户
*<br>输入参数:
*<br>返回类型:
*/
public void setusername(string usn){
this.username = usn;
}
/**
*<br>方法说明:设置邮件发送目的邮箱
*<br>输入参数:
*<br>返回类型:
*/
public void setto(string to){
this.to = to;
}
/**
*<br>方法说明:设置邮件发送源邮箱
*<br>输入参数:
*<br>返回类型:
*/
public void setfrom(string from){
this.from = from;
}
/**
*<br>方法说明:设置邮件主题
*<br>输入参数:
*<br>返回类型:
*/
public void setsubject(string subject){
this.subject = subject;
}
/**
*<br>方法说明:设置邮件内容
*<br>输入参数:
*<br>返回类型:
*/
public void setcontent(string content){
this.content = content;
}
/**
*<br>方法说明:把主题转换为中文
*<br>输入参数:string strtext
*<br>返回类型:
*/
public string transferchinese(string strtext){
try{
strtext = mimeutility.encodetext(new string(strtext.getbytes(), "gb2312"), "gb2312", "b");
}catch(exception e){
e.printstacktrace();
}
return strtext;
}
/**
*<br>方法说明:往附件组合中添加附件
*<br>输入参数:
*<br>返回类型:
*/
public void attachfile(string fname){
file.addelement(fname);
}
/**
*<br>方法说明:发送邮件
*<br>输入参数:
*<br>返回类型:boolean 成功为true,反之为false
*/
public boolean sendmail(){
//构造mail session
properties props = system.getproperties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
session session=session.getdefaultinstance(props, new authenticator(){
public passwordauthentication getpasswordauthentication(){
return new passwordauthentication(username,password);
}
});
try {
//构造mimemessage 并设定基本的值
mimemessage msg = new mimemessage(session);
msg.setfrom(new internetaddress(from));
internetaddress[] address={new internetaddress(to)};
msg.setrecipients(message.recipienttype.to,address);
subject = transferchinese(subject);
msg.setsubject(subject);
//构造multipart
multipart mp = new mimemultipart();
//向multipart添加正文
mimebodypart mbpcontent = new mimebodypart();
mbpcontent.settext(content);
//向mimemessage添加(multipart代表正文)
mp.addbodypart(mbpcontent);
//向multipart添加附件
enumeration efile=file.elements();
while(efile.hasmoreelements()){
mimebodypart mbpfile = new mimebodypart();
filename=efile.nextelement().tostring();
filedatasource fds = new filedatasource(filename);
mbpfile.setdatahandler(new datahandler(fds));
mbpfile.setfilename(fds.getname());
//向mimemessage添加(multipart代表附件)
mp.addbodypart(mbpfile);
}
file.removeallelements();
//向multipart添加mimemessage
msg.setcontent(mp);
msg.setsentdate(new date());
//发送邮件
transport.send(msg);
} catch (messagingexception mex) {
mex.printstacktrace();
exception ex = null;
if ((ex=mex.getnextexception())!=null){
ex.printstacktrace();
}
return false;
}
return true;
}
/**
*<br>方法说明:主方法,用于测试
*<br>输入参数:
*<br>返回类型:
*/
public static void main(string[] args){
mail sendmail = new mail();
sendmail.sethost("smtp.sohu.com");
sendmail.setusername("du_jiang");
sendmail.setpassword("31415926");
sendmail.setto("dujiang@sricnet.com");
sendmail.setfrom("du_jiang@sohu.com");
sendmail.setsubject("你好,这是测试!");
sendmail.setcontent("你好这是一个带多附件的测试!");
//mail sendmail = new mail("dujiang@sricnet.com","du_jiang@sohu.com","smtp.sohu.com","du_jiang","31415926","你好","胃,你好吗?");
sendmail.attachfile("c:/test.txt");
sendmail.attachfile("dnd.jar");
sendmail.sendmail();
}
}//end
闽公网安备 35060202000074号