代码
package com.relation.util;
import com.sun.mail.util.MailSSLSocketFactory;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.File;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 邮件发送
* @author zwb
*/
public class EmailUtil {
//发送者邮箱账号
private static String emailName="163邮箱账号";
//发送者邮箱授权码
private static String emailPassWord="163邮箱授权码";
/**
* 创建message对象
* @Autoword zwb
* @return
* @throws GeneralSecurityException
* @throws MessagingException
*/
public static Message getMessage() throws GeneralSecurityException, MessagingException {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts( true );
Properties props = new Properties();
props.setProperty( "mail.transport.protocol", "SMTP" );
props.setProperty( "mail.host", "smtp.163.com" );
props.setProperty( "mail.smtp.auth", "true" );
props.setProperty( "mail.smtp.ssl.enable", "true" );
props.put( "mail.smtp.ssl.socketFactory",sf );
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( emailName, emailPassWord ); //设置163邮箱的用户和登录授权码
}
};
Session session = Session.getInstance( props, auth );
session.setDebug( true );
Message message = new MimeMessage( session );
message.setFrom( new InternetAddress( emailName ) ); // 设置发送者
return message;
}
/**
* 添加内容
* @Autoword zwb
* @param multipart
* @param body
*/
public static void addBody(MimeMultipart multipart,String body,Map map) throws MessagingException {
//内容替换
ExpressionParser parser = new SpelExpressionParser();
TemplateParserContext parserContext = new TemplateParserContext("${","}");
String content = parser.parseExpression(body,parserContext).getValue(map, String.class);
// 邮件正文
MimeBodyPart text = new MimeBodyPart();
text.setContent(content,"text/html;charset=UTF-8");
multipart.addBodyPart(text);
}
/**
* 邮件类型转换
* @Autoword zwb
* @param email
*/
public static InternetAddress[] getInternetAddress(String email[]) throws AddressException {
//接收人
InternetAddress[] internetAddresses = new InternetAddress[email.length];
if (email!=null&&email.length>0){
for (int i = 0; i < email.length; i++) {
internetAddresses[i] = new InternetAddress(email[i]);
}
}
return internetAddresses;
}
/**
* 添加附件
* @Autoword zwb
* @param multipart
* @param adjunctPath
*/
public static void addAdjunct(MimeMultipart multipart,String adjunctPath[]) throws MessagingException {
//附件
if (adjunctPath!=null&&adjunctPath.length>0){
for (String path : adjunctPath) {
BodyPart messageBodyPart= new MimeBodyPart();
File file = new File(path);
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
}
}
}
/**
* 邮件发送
* @Autoword zwb
* @param subject 主题
* @param body 内容
* @param map 内容替换属性
* @param toEmail 接收人
* @param ccEmail 抄送人
* @param adjunctPath 附件路径
*/
public static void sendEmail(String subject,String body,Map map,String toEmail[],String ccEmail[],String adjunctPath[]) throws GeneralSecurityException, MessagingException {
MimeMultipart multipart = new MimeMultipart();
addBody(multipart,body,map);//内容
addAdjunct(multipart,adjunctPath);//附件
InternetAddress[] addressesTo = getInternetAddress(toEmail);//收件人
InternetAddress[] addressesCc = getInternetAddress(ccEmail);//抄送人
Message message = getMessage();
message.setSubject(subject);//主题
message.setContent(multipart);
message.setRecipients( MimeMessage.RecipientType.TO, addressesTo); // 设置发送方式与接收者
message.setRecipients( MimeMessage.RecipientType.CC, addressesCc); // 设置发送方式与接收者
Transport.send( message );
}
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
//邮件内容
String body = "<h1>这是一个${[test]}内容。。。。</h1><div style='background-color:red'>${[body]}</div>";
//内容替换
Map map = new HashMap();
map.put("test","测试");
map.put("body","看,我的背景是红色的!");
sendEmail("测试",body,map,"接收人".split(","),"抄送人".split(","),"D:\\aa.txt".split(","));
}
}
效果
运行效果图: