1.主要编写了邮箱发送工具类,里面每行代码有详细注释,直接复制修改个人邮箱信息就能进行测试,希望对大家有用。
public class SendMailUtil {
private static final String from = "test233@qq.com";
private static final String fromName = "XXX";
private static final String charSet = "utf-8";
private static final String username = "test233@qq.com";
private static final String password = "sdfksakjasdj";
private static Map<String, String> hostMap = new HashMap<String, String>();
static {
hostMap.put("smtp.126", "smtp.126.com");
hostMap.put("smtp.qq", "smtp.qq.com");
hostMap.put("smtp.163", "smtp.163.com");
hostMap.put("smtp.sina", "smtp.sina.com.cn");
hostMap.put("smtp.tom", "smtp.tom.com");
hostMap.put("smtp.263", "smtp.263.net");
hostMap.put("smtp.yahoo", "smtp.mail.yahoo.com");
hostMap.put("smtp.hotmail", "smtp.live.com");
hostMap.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;
}
}
public static void sendFtlMail(String toMailAddr ,String[] copyToMailAddr , String subject,
String templatePath, Map<String, Object> map ) {
Template template = null;
Configuration freeMarkerConfig = null;
HtmlEmail hemail = new HtmlEmail();
try {
hemail.setHostName(getHost(from));
hemail.setSmtpPort(getSmtpPort(from));
hemail.setCharset(charSet);
hemail.addTo(toMailAddr);
if(copyToMailAddr != null &©ToMailAddr.length > 0){
hemail.addCc(copyToMailAddr);
}
hemail.setFrom(from, fromName);
hemail.setAuthentication(username, password);
hemail.setSubject(subject);
freeMarkerConfig = new Configuration();
freeMarkerConfig.setDirectoryForTemplateLoading(new File(
getFilePath()));
template = freeMarkerConfig.getTemplate(getFileName(templatePath),
new Locale("Zh_cn"), "UTF-8");
String htmlText = FreeMarkerTemplateUtils
.processTemplateIntoString(template, map);
System.out.println(htmlText);
hemail.setMsg(htmlText);
hemail.send();
System.out.println("email send true!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("email send error!");
}
}
public static void sendCommonMail(String[] toMailAddr,String[] copyToMailAddr , String subject,
String message ) {
HtmlEmail hemail = new HtmlEmail();
try {
hemail.setHostName(getHost(from));
hemail.setSmtpPort(getSmtpPort(from));
hemail.setCharset(charSet);
hemail.addTo(toMailAddr);
if(copyToMailAddr != null && copyToMailAddr.length > 0){
hemail.addCc(copyToMailAddr);
}
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 String getHtmlText(String templatePath,
Map<String, Object> map) {
Template template = null;
String htmlText = "";
try {
Configuration freeMarkerConfig = null;
freeMarkerConfig = new Configuration();
freeMarkerConfig.setDirectoryForTemplateLoading(new File(
getFilePath()));
template = freeMarkerConfig.getTemplate(getFileName(templatePath),
new Locale("Zh_cn"), "UTF-8");
htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(
template, map);
System.out.println(htmlText);
} catch (Exception e) {
e.printStackTrace();
}
return htmlText;
}
private static String getFilePath() {
String path = getAppPath(SendMailUtil.class);
path = path + File.separator + "mailtemplate" + File.separator;
path = path.replace("\\", "/");
System.out.println(path);
return path;
}
private static String getFileName(String path) {
path = path.replace("\\", "/");
System.out.println(path);
return path.substring(path.lastIndexOf("/") + 1);
}
public static String getAppPath(Class<?> cls) {
if (cls == null)
throw new java.lang.IllegalArgumentException("参数不能为空!");
ClassLoader loader = cls.getClassLoader();
String clsName = cls.getName() + ".class";
Package pack = cls.getPackage();
String path = "";
if (pack != null) {
String packName = pack.getName();
if (packName.startsWith("java.") || packName.startsWith("javax."))
throw new java.lang.IllegalArgumentException("不要传送系统类!");
clsName = clsName.substring(packName.length() + 1);
if (packName.indexOf(".") < 0)
path = packName + "/";
else {
int start = 0, end = 0;
end = packName.indexOf(".");
while (end != -1) {
path = path + packName.substring(start, end) + "/";
start = end + 1;
end = packName.indexOf(".", start);
}
path = path + packName.substring(start) + "/";
}
}
java.net.URL url = loader.getResource(path + clsName);
String realPath = url.getPath();
int pos = realPath.indexOf("file:");
if (pos > -1)
realPath = realPath.substring(pos + 5);
pos = realPath.indexOf(path + clsName);
realPath = realPath.substring(0, pos - 1);
if (realPath.endsWith("!"))
realPath = realPath.substring(0, realPath.lastIndexOf("/"));
try {
realPath = java.net.URLDecoder.decode(realPath, "utf-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
System.out.println("realPath----->" + realPath);
return realPath;
}
public static void main(String[] args) {
String[] toMailAddr = {"test123@163.com","test1234@163.com"};
String[] copy ={"test12345@163.com"};
sendCommonMail(toMailAddr,
copy,
"【提醒】加班预警",
"<!DOCTYPE html>\n" +
"<html>\n" +
" <body>\n" +
" <table width=\"800\" border=\"1\" align=\"center\">\n" +
" <caption>【提醒】加班预警</caption>\n" +
" <thead>\n" +
" <tr>\n" +
" <th>加班类型</th>\n" +
" <th>姓名</th>\n" +
" <th>编号</th>\n" +
"\t\t\t\t\t\t<th>日期</th>\n" +
" <th>打卡开始时间</th>\n" +
"\t\t\t\t\t\t<th>打卡结束时间</th>\n" +
" </tr>\n" +
" </thead>\n" +
" <tbody>\n" +
" <tr>\n" +
" <td>工作日加班</td>\n" +
" <td>女孩子</td>\n" +
" <td>00100</td>\n" +
" <td>2020-09-02</td>\n" +
" <td>2020-09-02 08:43:56</td>\n" +
" <td>2020-09-02 21:07:08</td>\n" +
" </tr> \n" +
" </tbody>\n" +
" </table>\n" +
" </body>\n" +
"</html>");
}
}