- 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 = "[email protected]";
private final static String password = "xxx";
/**
* 发送邮件
* @param host 主机名,一般是stmp.xxx.com
* @param from 发送者邮箱帐号
* @param password 发送者邮箱密码,有些邮箱需要用授权码代替密码
* @param subject 邮件标题
* @param body 邮件内容,可以是html格式的
* @param tos 收件人邮箱帐号,多个收件人用逗号隔开
* @param files 附件,多个附件用逗号隔开,附件地址要物理路径
* @return 发送过程不出异常则返回true,否则返回false
*/
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;
}
/**
* 发送邮件
* 发件人信息使用默认配置
* @param subject 邮件标题
* @param body 邮件内容,可以是html格式的
* @param tos 收件人邮箱帐号,多个收件人用逗号隔开
* @param files 附件,多个附件用逗号隔开,附件地址要物理路径
* @return 发送过程不出异常则返回true,否则返回false
*/
public static boolean sendEmail(String subject, String body, String tos, String[] files) {
return sendEmail(host, from, password, subject, body, tos, files);
}
/**
* 发送邮件
* @param host 主机名,一般是stmp.xxx.com
* @param from 发送者邮箱帐号
* @param password 发送者邮箱密码,有些邮箱需要用授权码代替密码
* @param subject 邮件标题
* @param body 邮件内容,可以是html格式的
* @param tos 收件人邮箱帐号,多个收件人用逗号隔开
* @param files 附件,多个附件用逗号隔开,附件地址使用的是相对于网站根目录的路径
* @param request Request请求对象,用于获取附件的物理路径
* @return 发送过程不出异常则返回true,否则返回false
*/
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;ireturn sendEmail(host, from, password, subject, body, tos, paths);
}
/**
* 发送邮件
* 发件人信息使用默认配置
* @param subject 邮件标题
* @param body 邮件内容,可以是html格式的
* @param tos 收件人邮箱帐号,多个收件人用逗号隔开
* @param files 附件,多个附件用逗号隔开,附件地址使用的是相对于网站根目录的路径
* @param request Request请求对象,用于获取附件的物理路径
* @return 发送过程不出异常则返回true,否则返回false
*/
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", "我是主体内容1
", "[email protected],[email protected]", "/img/register.gif,/index.html",request);
MailUtils.sendEmail("smtp.qq.com","[email protected]","password","我是主题3", "我是主体内容3
", "[email protected],[email protected]", new String[] { "D:\\Tomcat\\apache-tomcat-7.0.52\\wtpwebapps\\Store\\insert.jsp","D:\\Tomcat\\apache-tomcat-7.0.52\\wtpwebapps\\Store\\中文.txt"});