packagecom.ata.task;/*** 邮件发�?�工具类
*
*@authorxxh
*@link*
*@version$Revision: 1.00 $ $Date: 2009-12-18*/
importjava.io.IOException;importjava.util.Date;importjava.util.Properties;importjavax.mail.Authenticator;importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.Multipart;importjavax.mail.PasswordAuthentication;importjavax.mail.Session;importjavax.mail.Transport;importjavax.mail.internet.AddressException;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeBodyPart;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMultipart;public classMailUtil {/*******
* 邮件发�?�,带用户名和密码验证,测试通过
*
*@paramto
*@paramfrom
*@paramtitle
*@paramcontent
*@paramsmtpServer
*@paramuser
*@parampassword
*@throwsAddressException
*@throwsMessagingException*/@SuppressWarnings({"unchecked"})public static voidsend(String from, String to,String copyTo, String title,
String content, String smtpServer, String user, String password,booleanisHTML)throwsAddressException, MessagingException {
Properties props= newProperties();
Authenticator auth= newMailAuthenticator(user, password);
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", "true");
sendMailSession=Session.getInstance(props, auth);
Message newMessage= newMimeMessage(sendMailSession);
newMessage.setFrom(newInternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(
to));
newMessage.addRecipient(Message.RecipientType.CC,newInternetAddress(
copyTo));
newMessage.setSubject(title);
newMessage.setSentDate(newDate());if(isHTML) {
newMessage.setContent(content,"text/html;charset=UTF-8");
}else{
newMessage.setText(content);
}
transport= sendMailSession.getTransport("smtp");
Transport.send(newMessage);
transport.close();//System.out.println("---------- Mail Send Success ----------");
}/*******************
* 邮件发�?�,不带用户名和密码验证
*@paramto
*@paramfrom
*@paramtitle
*@paramcontent
*@paramsmtpServer
*@throwsAddressException
*@throwsMessagingException*/@SuppressWarnings("static-access")public static voidsend(String to, String from, String title,
String content, String smtpServer)throwsAddressException,
MessagingException {
Properties props= newProperties();
Authenticator auth= newMailAuthenticator();
Session sendMailSession;
Transport transport;
sendMailSession=Session.getInstance(props, auth);
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", "true");
Message newMessage= newMimeMessage(sendMailSession);
newMessage.setFrom(newInternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(
to));
newMessage.setSubject(title);
newMessage.setSentDate(newDate());
newMessage.setText(content);
transport= sendMailSession.getTransport("smtp");
transport.send(newMessage);
}/*********************
* 带附件邮件发送,带用户名和密码验证,测试通过
*@paramto
*@paramfrom
*@paramtitle
*@paramcontent
*@paramsmtpServer
*@paramuser
*@parampassword
*@paramfileNames
*@parampaths
*@throwsAddressException
*@throwsMessagingException*/@SuppressWarnings("static-access")public static voidsend(String to, String from, String title,
String content, String smtpServer, String user, String password,
String fileNames[], String[] paths)throwsAddressException,
MessagingException {try{
Properties props= newProperties();
Authenticator auth= newMailAuthenticator(user, password);
Session sendMailSession;
Transport transport;
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", "true");
sendMailSession=Session.getInstance(props, auth);
MimeMessage newMessage= newMimeMessage(sendMailSession);
newMessage.setFrom(newInternetAddress(from));
newMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(to));//创建 Multipart 并放入每�? MimeBodyPart
Multipart mp = newMimeMultipart();if (title != null && !title.equals("")) {//设置邮件主题
newMessage.setSubject(title);
}else{
newMessage.setSubject("无主�?");
}//第一部分信息
MimeBodyPart mbp1 = newMimeBodyPart();if (content != null && !content.equals("")) {
mbp1.setText(content,"GBK");
}else{
mbp1.setText(new String(), "GBK");
}
mp.addBodyPart(mbp1);// //在第二部分信息中附加文件//if (paths != null) {//for (int i = 0; i < paths.length; i++) {//MimeBodyPart mbp2 = new MimeBodyPart();//FileDataSource fds = new FileDataSource(CommonConstants.ROOTPATH//+ paths[i]);//mbp2.setDataHandler(new DataHandler(fds));//mbp2.setFileName(fileNames[i]);//mp.addBodyPart(mbp2);//}//}//增加 Multipart 到信息体
newMessage.setContent(mp);
newMessage.setSentDate(newDate());
transport= sendMailSession.getTransport("smtp");
transport.send(newMessage);
}catch(Exception e) {
e.printStackTrace();
}
}
}class MailAuthenticator extendsAuthenticator {privateString user;privateString password;publicMailAuthenticator() {
}publicMailAuthenticator(String user, String password) {this.user =user;this.password =password;
}protectedPasswordAuthentication getPasswordAuthentication() {return newPasswordAuthentication(user, password);
}
}