/***包名: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); // 发送邮件}}