日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MailUtils

發(fā)布時間:2023/12/13 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MailUtils 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/***包名: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* 版權(quán)所有(C) 2018 裕福控股有限公司* 創(chuàng)建: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;}}/*** 發(fā)送普通郵件* * @param toMailAddr* 收信人地址* @param subject* email主題* @param message* 發(fā)送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();}}// 發(fā)送帶內(nèi)嵌文件的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提供的郵件實(shí)現(xiàn)類JavaMailSenderImpl send = new JavaMailSenderImpl();Properties prop = new Properties();prop.setProperty("mail.transport.protocol", smtp); // 設(shè)置郵件發(fā)送協(xié)議prop.setProperty("mail.host", host); // 郵件服務(wù)器地址prop.setProperty("mail.smtps.ssl.enable", sslIs); // 郵件ssl驗(yàn)證prop.setProperty("mail.smtp.auth", authIs); // 郵件服務(wù)身份驗(yàn)證send.setUsername(userName); // 設(shè)置用戶名send.setPassword(password); // 設(shè)置密碼send.setJavaMailProperties(prop);MimeMessage msg = send.createMimeMessage();// 指定HTML編碼,參數(shù)true表示為multipartMimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");helper.setFrom(userName); // 發(fā)送者郵箱helper.setTo("800@163.com"); // 接收者郵箱// helper.setCc(CC_MAIL); // 抄送郵箱// helper.setBcc(BCC_MAIl); // 密送郵箱helper.setSentDate(new Date()); // 發(fā)送日期helper.setSubject("工資條(" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss")+ ")");String html = "<font size='5' color='red'>HTML格式測試成功!</font>";helper.setText(html, true); // 郵件內(nèi)容,參數(shù)true表示是html代碼send.send(msg); // 發(fā)送郵件}}

  

轉(zhuǎn)載于:https://www.cnblogs.com/guoziyi/p/8432331.html

總結(jié)

以上是生活随笔為你收集整理的MailUtils的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。