package com.taover.util; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import javax.mail.Address; 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.MimeMessage; public class UtilEmail { public static Map GLOBAL_CONFIG = new HashMap(){{ put("host", "smtp.taover.com"); put("port", "25"); put("username", "taover-robot@taover.com"); put("password", "Lexi@1798"); put("header", "报警-服务不可用"); put("from", "taover-robot@taover.com"); put("fromName", "八爪云-服务监控"); put("htmlTopic", "text/html; charset=utf-8"); }}; /** * 发�?�HTML格式的邮�? */ public static void sendHtmlMail(List toList, String subject, String htmlContent, Map mailHostConfig) { // 获取系统环境 Properties prop = new Properties(); // 添加必要的信�? prop.put("mail.smtp.host", getValue("host", mailHostConfig)); prop.put("mail.smtp.port", getValue("port", mailHostConfig)); prop.put("mail.smtp.auth", "true"); // 设置对话和邮件服务器进行通讯 Authenticator auth = new SimpleAuthenticator(getValue("username", mailHostConfig), getValue("password", mailHostConfig)); Session session = Session.getDefaultInstance(prop, auth); // 设置邮件对象 Message message = new MimeMessage(session); try { // 设置邮件主题 message.setSubject(subject); // 设置邮件标题 message.setHeader("Header", getValue("header", mailHostConfig)); // 设置发�?�时�? message.setSentDate(new Date()); // 设置发信人地�? �? 名字 Address address = new InternetAddress(getValue("from", mailHostConfig), getValue("fromName", mailHostConfig)); // 把发件人信息添加到信息中 message.setFrom(address); // 设置收件人地�? List
addressList = new ArrayList
(); for(String item: toList) { if(item != null && !"".equals(item.trim())) { addressList.add(new InternetAddress(item)); } } // 设置接收人地�? message.addRecipients(Message.RecipientType.TO, addressList.toArray(new Address[addressList.size()])); // 设置多个收件人地�? // message.addRecipient(Message.RecipientType.TO,new InternetAddress("xxx@xxx.com")); // 设置发�?�信息的内容 下面为发送hmml // 设置邮件格式 message.setContent(htmlContent, getValue("htmlTopic", mailHostConfig)); // 保存上面添加的信�? message.saveChanges(); Transport.send(message); } catch (Exception e) { e.printStackTrace(); } } public static void sendHtmlMail(List toList, String subject, String htmlContent) { sendHtmlMail(toList, subject, htmlContent, null); } public static String getValue(String key, Map localConfig) { if(localConfig == null) { return GLOBAL_CONFIG.get(key); }else { String data = localConfig.get(key); if(data == null) { return GLOBAL_CONFIG.get(key); }else { return data; } } } /** * @param args */ public static void main(String[] args) { sendHtmlMail(Arrays.asList("wangbin@taover.com", "317058383@qq.com"), "测试邮件", "

Hello Email Saturday V2

"); } } class SimpleAuthenticator extends Authenticator{ private String username=""; private String password=""; public SimpleAuthenticator() { super(); } /** * 设置验证的用户名和密�? */ public SimpleAuthenticator(String userName , String password) { super(); this.username = userName; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(this.username,this.password); } }