- jar包下载http://commons.apache.org/proper/commons-email/download_email.cgi
- 工具类MailUtils
package com.utils;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
public class MailUtils {
private static String host = "smtp.xxx.com";
private final static String from = "xxx@xxx.com1";
private final static String password = "xxx";
public static boolean sendEmail(String host,final String from,final String password,String subject, String body, String tos, String[] files) {
Properties prop = new Properties();
prop.put("mail.smtp.host", host);
prop.put("mail.smtp.auth", "true");
Session session = Session.getInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] internetAddressTo = InternetAddress.parse(tos);
message.setRecipients(Message.RecipientType.TO, internetAddressTo);
message.setSentDate(new Date());
message.setSubject(subject);
MimeMultipart mm = new MimeMultipart();
MimeBodyPart html = new MimeBodyPart();
html.setContent(body, "text/html;charset=UTF-8");
mm.addBodyPart(html);
if(files!=null&&files.length>0) {
for (String f : files) {
MimeBodyPart attachment = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(f));
attachment.setDataHandler(dh);
attachment.setFileName(MimeUtility.encodeText(dh.getName()));
mm.addBodyPart(attachment);
}
}
message.setContent(mm);
Transport tran = session.getTransport("smtp");
tran.send(message, message.getAllRecipients());
tran.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static boolean sendEmail(String subject, String body, String tos, String[] files) {
return sendEmail(host, from, password, subject, body, tos, files);
}
public static boolean sendEmail(String host,final String from,final String password,String subject, String body, String tos, String files,HttpServletRequest request) {
ServletContext context= request.getServletContext();
String[] paths=null;
if(files!=null&&files.length()>0) {
String[] fileArr = files.split(",");
paths=new String[fileArr.length];
for (int i=0;i<fileArr.length;i++) {
paths[i]=context.getRealPath(fileArr[i]);
}
}
return sendEmail(host, from, password, subject, body, tos, paths);
}
public static boolean sendEmail(String subject, String body, String tos, String files,HttpServletRequest request) {
return sendEmail(host, from, password, subject, body, tos, files,request);
}
}
MailUtils.sendEmail("我是主题1", "<h1 style='color:red;'>我是主体内容1</h1>", "a@qq.com,b@qq.com", "/img/register.gif,/index.html",request);
MailUtils.sendEmail("smtp.qq.com","from@qq.com","password","我是主题3", "<h1>我是主体内容3</h1>", "a@qq.com,b@qq.com", new String[] { "D:\\Tomcat\\apache-tomcat-7.0.52\\wtpwebapps\\Store\\insert.jsp","D:\\Tomcat\\apache-tomcat-7.0.52\\wtpwebapps\\Store\\中文.txt"});