import javax.mail.* ;
import java.util.* ;
import javax.mail.internet.* ;
import java.net.* ;
import javax.activation.* ;
import com.westerasoft.changqingzj.common.commsql.* ;
public class SendMaiBean
{
/**
* 網際郵件擴充協議消息對象
*/
private MimeMessage message = null ;
/**
* 發送器
*/
private Transport transport = null ;
/**
*session對象
*/
private Session session = null ;
private Multipart mm = new MimeMultipart() ;
/**
* 設置mail服務器
*/
public SendMaiBean(String mailServer) throws Exception
{
try
{
Properties props = new Properties() ;
props.put("mail.smtp.host",mailServer) ;
props.put("mail.smtp.auth", "true") ;
session = Session.getInstance(props) ;
session.setDebug(true) ;
message = new MimeMessage(session) ;
this.transport = this.session.getTransport("smtp") ;
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "可能因為網絡故障,連接郵件服務器失敗!") ;
}
}
/**
* 設置發信人的用戶名
* @throws Exception
*/
public void connectServer(String fromUserName, String fromUserPassword
, String serverIp) throws Exception
{
try
{
this.transport.connect(serverIp, fromUserName, fromUserPassword) ;
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "連接郵件服務器失敗,請檢查你的用戶名和密碼和網絡!") ;
}
}
/**
* 發送mail
*/
public void sendMail() throws Exception
{
try
{
message.setContent(mm) ; //把mm作為消息對象的內容
message.setSentDate(new Date()) ;
message.saveChanges() ;
transport.sendMessage(message, message.getAllRecipients()) ;
transport.close() ;
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "可能是因為網絡故障,發送郵件失敗!") ;
}
}
/**
*
*/
public void setMailToAddresses(String[] tos) throws Exception
{
try
{
Address address[]=new Address[tos.length];
for (int i = 0 ; i < tos.length ; i++)
{
InternetAddress to = new InternetAddress(tos) ;
address=to;
}
message.setRecipients(Message.RecipientType.TO,address);
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "收信人地址錯誤!") ;
}
}
/**
* 設置發行人
*/
public void setMailFromAddress(String from) throws Exception
{
try
{
InternetAddress f = new InternetAddress(from) ;
message.setFrom(f) ;
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "發信人地址錯誤!") ;
}
}
/**
* 設置主題
*/
public void setMailTitle(String title) throws Exception
{
try
{
message.setSubject(title,"gb2312") ;
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "發送信件主題錯誤!") ;
}
}
/**
*
*/
public void setMailContent(String tcontent, String emailtype) throws
Exception
{
try
{
//設置信件文本內容
BodyPart mdp = new MimeBodyPart() ; //新建一個存放信件內容的BodyPart對象
mdp.setContent(tcontent, emailtype + ";charset=gb2312") ; //給BodyPart對象設置內容和格式/編碼方式
mm.addBodyPart(mdp) ; //將含有信件內容的BodyPart加入到MimeMultipart對象中
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "發送信件內容錯誤!") ;
}
}
/**
* 增加附件,注:是手輸入的文本信息
*/
public void addTextTypeFile(String content) throws Exception
{
try
{
// 設置信件的附件1(自定義附件:直接將所設文本內容加到自定義文件中作為附件發送)
MimeBodyPart mdp = new MimeBodyPart() ; //新建一個存放附件的BodyPart
DataHandler dh = new DataHandler(content, "text/plain;charset=gb2312") ;
//新建一個DataHandler對象,并設置其內容和格式/編碼方式
mdp.setFileName("text.txt") ; //加上這句將作為附件發送,否則將作為信件的文本內容
mdp.setDataHandler(dh) ; //給BodyPart對象設置內容為dh
mm.addBodyPart(mdp) ; //將含有附件的BodyPart加入到MimeMultipart對象中
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "發送文本附件出錯!") ;
}
}
/**
* 增加附件,注:是本地文件
*/
public void addFileTypeFile(String fileName) throws Exception
{
try
{
//設置信件的附件2(用本地上的文件作為附件)
MimeBodyPart mdp = new MimeBodyPart() ;
FileDataSource fds = new FileDataSource(fileName) ;
DataHandler dh = new DataHandler(fds) ;
int ddd = fileName.lastIndexOf("\\") ;
String fname = fileName.substring(ddd) ; //提取文件名
String ffname = new String(fname.getBytes("gb2312"), "ISO8859-1") ; //處理文件名是中文的情況
mdp.setFileName(ffname) ; //可以和原文件名不一致,但最好一樣
mdp.setDataHandler(dh) ;
mm.addBodyPart(mdp) ;
}
catch (Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "發送文件附件出錯!") ;
}
}
/**
* 增加附件,注:是網絡中的文件
*/
public void addNetTypeFile(String netAddressAndFileName) throws Exception
{
try
{
//設置信件的附件3(用遠程文件作為附件)
MimeBodyPart mdp = new MimeBodyPart() ;
URL urlfj = new URL(netAddressAndFileName) ;
URLDataSource ur = new URLDataSource(urlfj) ;
//注:這里用的參數只能為URL對象,不能為URL字串,在前面類介紹時有誤(請諒解),這里糾正一下.
DataHandler dh = new DataHandler(ur) ;
int ttt = netAddressAndFileName.lastIndexOf("/") ;
String urlname = netAddressAndFileName.substring(ttt) ;
mdp.setFileName(urlname) ;
mdp.setDataHandler(dh) ;
mm.addBodyPart(mdp) ;
}
catch(Exception e)
{
e.printStackTrace() ;
throw new Exception(ExceptionHandle.ERROR_PREFIX + "發送網絡附件出錯出錯!") ;
}
}
}
來自IT資源網IT資源網