package com.asql.base;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class mailsender {
public final static boolean sendmail(loadlog sendlog,string smtphost,string mailfrom,
boolean mailauth,string mailuser,string mailpassword,
string mailto,string mailcc,string mailsubject,
string mailmessage,string mailattach)
{
mimemessage mimemsg;
mimemultipart mp;
session session;
string sep[]={";"};
properties props=new java.util.properties();
int i;
props.put("mail.smtp.host",smtphost);
try{
session = session.getdefaultinstance(props,null);
mimemsg = new mimemessage(session);
mp = new mimemultipart();
if(mailauth)
props.put("mail.smtp.auth","true");
else
props.put("mail.smtp.auth","false");
if (sendlog!=null)
sendlog.println("mail host address: "+smtphost);
}
catch(exception e)
{
if (sendlog!=null)
sendlog.println(e.getmessage());
return false;
}
try
{
mimemsg.setfrom(new internetaddress(mailfrom));
if (sendlog!=null)
sendlog.println("mail from address: "+mailfrom);
}
catch(exception e)
{
if (sendlog!=null)
sendlog.println(e.getmessage());
return false;
}
try{
java.util.vector temp = wordsconvert.getwords(mailto,sep);
if (temp.size()==0)
{
if (sendlog!=null)
sendlog.println("mail target address requried.");
return false;
}
address toaddress[] = new address[temp.size()];
for(i=0;i
mimemsg.setrecipients(message.recipienttype.to,toaddress);
if (sendlog!=null)
sendlog.println("mail to address: "+mailto);
}
catch(exception e)
{
if (sendlog!=null)
sendlog.println("error mail to,"+e);
return false;
}
if(mailcc != null && mailcc.length()>0)
{
try{
java.util.vector temp = wordsconvert.getwords(mailcc,sep);
if (temp.size()>0)
{
address ccaddress[] = new address[temp.size()];
for(i=0;i
mimemsg.setrecipients(message.recipienttype.cc,ccaddress);
if (sendlog!=null)
sendlog.println("mail cc address: "+mailcc);
}
}
catch(exception e)
{
if (sendlog!=null)
sendlog.println(e.getmessage());
return false;
}
}
try
{
mimemsg.setsubject(mailsubject,"gb2312");
bodypart bp = new mimebodypart();
bp.setcontent(""+
mailmessage,"text/html;charset=gb2312");
mp.addbodypart(bp);
}
catch(exception e)
{
if (sendlog!=null)
sendlog.println(e.getmessage());
return false;
}
if(mailattach != null && mailattach.length()>0)
{
try{
java.util.vector temp = wordsconvert.getwords(mailattach,sep);
for(i=0;i
mimebodypart bp = new mimebodypart();
filedatasource fileds = new filedatasource(temp.elementat(i).tostring());
datahandler dh = new datahandler(fileds);
bp.setdisposition(part.attachment);
bp.setfilename(fileds.getname());
bp.setdatahandler(dh);
mp.addbodypart(bp);
}
}
catch(exception e)
{
if (sendlog!=null)
sendlog.println(e.getmessage());
return false;
}
}
try{
mimemsg.setcontent(mp);
mimemsg.savechanges();
session mailsession = session.getinstance(props,null);
transport transport = mailsession.gettransport("smtp");
transport.connect((string)props.get("mail.smtp.host"),mailuser,mailpassword);
transport.sendmessage(mimemsg,mimemsg.getallrecipients());
if (sendlog!=null)
sendlog.println("mail successfully sended!");
transport.close();
}
catch(exception e)
{
if (sendlog!=null)
sendlog.println(e.getmessage());;
return false ;
}
return true;
}
}
闽公网安备 35060202000074号