/***包名:com.thinkgem.jeesite.test*描述:package com.thinkgem.jeesite.test;*/
package com.thinkgem.jeesite.test;import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import javax.mail.internet.MimeMessage;import org.apache.commons.mail.HtmlEmail;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;import com.jjqkkkaa.posp.common.utils.DateUtils;/*** MailUtilsss.java* 版權所有(C) 2018 裕福控股有限公司* 創建:gll * 時間:2018年2月8日* 描述:MailUtils*/
public class MailUtils {private static final String from = "aaaaaaaaa@163.com";private static final String fromName = "測試公司";private static final String charSet = "utf-8";private static final String username = "aaaaaaaa@163.com";private static final String password = "123456";private static Map<String, String> hostMap = new HashMap<String, String>();static {// 126hostMap.put("smtp.126", "smtp.126.com");// qqhostMap.put("smtp.qq", "smtp.qq.com");// 163hostMap.put("smtp.163", "smtp.163.com");// sinahostMap.put("smtp.sina", "smtp.sina.com.cn");// tomhostMap.put("smtp.tom", "smtp.tom.com");// 263hostMap.put("smtp.263", "smtp.263.net");// yahoohostMap.put("smtp.yahoo", "smtp.mail.yahoo.com");// hotmailhostMap.put("smtp.hotmail", "smtp.live.com");// gmailhostMap.put("smtp.gmail", "smtp.gmail.com");hostMap.put("smtp.port.gmail", "465");}public static String getHost(String email) throws Exception {Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");Matcher matcher = pattern.matcher(email);String key = "unSupportEmail";if (matcher.find()) {key = "smtp." + matcher.group(1);}if (hostMap.containsKey(key)) {return hostMap.get(key);} else {throw new Exception("unSupportEmail");}}public static int getSmtpPort(String email) throws Exception {Pattern pattern = Pattern.compile("\\w+@(\\w+)(\\.\\w+){1,2}");Matcher matcher = pattern.matcher(email);String key = "unSupportEmail";if (matcher.find()) {key = "smtp.port." + matcher.group(1);}if (hostMap.containsKey(key)) {return Integer.parseInt(hostMap.get(key));} else {return 25;}}/*** 發送普通郵件* * @param toMailAddr* 收信人地址* @param subject* email主題* @param message* 發送email信息*/public static void sendCommonMail(String toMailAddr, String subject,String message) {HtmlEmail hemail = new HtmlEmail();try {hemail.setHostName(getHost(from));hemail.setSmtpPort(getSmtpPort(from));hemail.setCharset(charSet);hemail.addTo(toMailAddr);hemail.setFrom(from, fromName);hemail.setAuthentication(username, password);hemail.setSubject(subject);hemail.setMsg(message);hemail.send();System.out.println("email send true!");} catch (Exception e) {e.printStackTrace();System.out.println("email send error!");}}public static void main(String[] args) {try {sendInlineMail();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 發送帶內嵌文件的HTML格式郵件public static void sendInlineMail() throws Exception {String smtp = "smtp";String host = "smtp.163.com";String sslIs ="true";String authIs = "true";String userName = "aa@163.com";String password = "aa#";// spring提供的郵件實現類JavaMailSenderImpl send = new JavaMailSenderImpl();Properties prop = new Properties();prop.setProperty("mail.transport.protocol", smtp); // 設置郵件發送協議prop.setProperty("mail.host", host); // 郵件服務器地址prop.setProperty("mail.smtps.ssl.enable", sslIs); // 郵件ssl驗證prop.setProperty("mail.smtp.auth", authIs); // 郵件服務身份驗證send.setUsername(userName); // 設置用戶名send.setPassword(password); // 設置密碼send.setJavaMailProperties(prop);MimeMessage msg = send.createMimeMessage();// 指定HTML編碼,參數true表示為multipartMimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");helper.setFrom(userName); // 發送者郵箱helper.setTo("800@163.com"); // 接收者郵箱// helper.setCc(CC_MAIL); // 抄送郵箱// helper.setBcc(BCC_MAIl); // 密送郵箱helper.setSentDate(new Date()); // 發送日期helper.setSubject("工資條(" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss")+ ")");String html = "<font size='5' color='red'>HTML格式測試成功!</font>";helper.setText(html, true); // 郵件內容,參數true表示是html代碼send.send(msg); // 發送郵件}}