利用JavaMail發(fā)送HTML模板郵件
本文實(shí)例為大家分享了用JavaMail發(fā)送HTML模板郵件的具體代碼,供大家參考,具體內(nèi)容如下
依賴
<dependency> ? ? <groupId>org.jsoup</groupId> ? ? <artifactId>jsoup</artifactId> ? ? <version>1.10.3</version> </dependency> <dependency> ? ? <groupId>javax.mail</groupId> ? ? <artifactId>mail</artifactId> ? ? <version>1.4.1</version> </dependency>
工具類(lèi)
package test.email;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
?* 發(fā)送郵件工具類(lèi)
?*/
public class MailUtil {
?? ?
?? ?private final static Logger logger = LoggerFactory.getLogger(MailUtil.class);
?? ?
?? ?/**
?? ? * 郵件發(fā)送
?? ? * @param mailHost 郵件服務(wù)地址
?? ? * @param fromMail 發(fā)件人
?? ? * @param fromName 發(fā)件人名
?? ? * @param fromMailPwd 發(fā)件人密碼
?? ? * @param toMails 收件人,多個(gè)用英文逗號(hào)分隔
?? ? * @param mailTitle 郵件標(biāo)題
?? ? * @param mailContent 郵件內(nèi)容
?? ? * @throws Exception
?? ? */
?? ?public static void sendMail(String mailHost, String fromMail, String fromName, String fromMailPwd,
?? ??? ??? ?String toMails, String mailTitle, String mailContent) throws Exception {
?? ??? ?String[] toMailArr = null;
?? ??? ?if (toMails != null && !toMails.equals("")) {
?? ??? ??? ?toMailArr = toMails.split(",");
?? ??? ?} else {
?? ??? ??? ?throw new Exception("郵件發(fā)送人不能為空");
?? ??? ?}
?? ??? ?
?? ??? ?// 郵件屬性信息
?? ??? ?Properties props = new Properties();
?? ??? ?props.put("mail.host", mailHost);
?? ??? ?props.put("mail.transport.protocol", "smtp");
?? ??? ?props.put("mail.smtp.auth", "true");
?? ??? ?
?? ??? ?Session session = Session.getInstance(props); // 根據(jù)屬性新建一個(gè)郵件會(huì)話
?? ??? ?//session.setDebug(true); // 是否打印調(diào)試信息
?? ??? ?toMailArr = toMails.split(",");
?? ??? ?for (String to : toMailArr) {
?? ??? ??? ?MimeMessage message = new MimeMessage(session); // 由郵件會(huì)話新建一個(gè)消息對(duì)象
?? ??? ??? ?message.setFrom(new InternetAddress(fromMail));// 設(shè)置發(fā)件人的地址
?? ??? ??? ?message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, fromName));// 設(shè)置收件人,并設(shè)置其接收類(lèi)型為T(mén)O
?? ??? ??? ?message.setSubject(mailTitle);// 設(shè)置標(biāo)題
?? ??? ??? ?message.setContent(mailContent, "text/html;charset=UTF-8"); // 設(shè)置郵件內(nèi)容類(lèi)型為html
?? ??? ??? ?message.setSentDate(new Date());// 設(shè)置發(fā)信時(shí)間
?? ??? ??? ?message.saveChanges();// 存儲(chǔ)郵件信息
?? ??? ??? ?// 發(fā)送郵件
?? ??? ??? ?Transport transport = session.getTransport();
?? ??? ??? ?transport.connect(fromMail, fromMailPwd);
?? ??? ??? ?transport.sendMessage(message, message.getAllRecipients());
?? ??? ??? ?transport.close();
?? ??? ?}
?? ?}
?? ?/**
?? ? * 郵件發(fā)送(群發(fā))
?? ? * @param mailHost 郵件服務(wù)地址
?? ? * @param fromMail 發(fā)件人
?? ? * @param fromName 發(fā)件人名
?? ? * @param fromMailPwd 發(fā)件人密碼
?? ? * @param toMails 收件人,多個(gè)用英文逗號(hào)分隔
?? ? * @param mailTitle 郵件標(biāo)題
?? ? * @param mailContent 郵件內(nèi)容
?? ? * @throws Exception
?? ? */
?? ?public static void sendGroupMail(String mailHost, String fromMail, String fromName, String fromMailPwd,
?? ??? ??? ?String toMails, String mailTitle, String mailContent) throws Exception {
?? ??? ?String[] toMailArr = null;
?? ??? ?if (toMails != null && !toMails.equals("")) {
?? ??? ??? ?toMailArr = toMails.split(",");
?? ??? ?} else {
?? ??? ??? ?throw new Exception("郵件發(fā)送人不能為空");
?? ??? ?}
?? ??? ?
?? ??? ?// 郵件屬性信息
?? ??? ?Properties props = new Properties();
?? ??? ?props.put("mail.host", mailHost);
?? ??? ?props.put("mail.transport.protocol", "smtp");
?? ??? ?props.put("mail.smtp.auth", "true");
?? ??? ?
?? ??? ?Session session = Session.getInstance(props); // 根據(jù)屬性新建一個(gè)郵件會(huì)話
?? ??? ?//session.setDebug(true); // 是否打印調(diào)試信息
?? ??? ?MimeMessage message = new MimeMessage(session); // 由郵件會(huì)話新建一個(gè)消息對(duì)象
?? ??? ?message.setFrom(new InternetAddress(fromMail)); // 設(shè)置發(fā)件人的地址
?? ??? ?InternetAddress[] sendTo = new InternetAddress[toMailArr.length];
?? ??? ?for (int i = 0; i < toMailArr.length; i++) {
?? ??? ??? ?sendTo[i] = new InternetAddress(toMailArr[i], fromName);
?? ??? ?}
?? ??? ?message.setRecipients(Message.RecipientType.TO, sendTo); // 設(shè)置收件人,并設(shè)置其接收類(lèi)型為T(mén)O
?? ??? ?message.setSubject(mailTitle); // 設(shè)置標(biāo)題
?? ??? ?message.setContent(mailContent, "text/html;charset=UTF-8"); // 設(shè)置郵件內(nèi)容類(lèi)型為html
?? ??? ?message.setSentDate(new Date()); // 設(shè)置發(fā)信時(shí)間
?? ??? ?message.saveChanges(); // 存儲(chǔ)郵件信息
?? ??? ?// 發(fā)送郵件
?? ??? ?Transport transport = session.getTransport();
?? ??? ?transport.connect(fromMail, fromMailPwd);
?? ??? ?transport.sendMessage(message, message.getAllRecipients());
?? ??? ?transport.close();
?? ?}
?? ?/**
?? ? * 讀取html文件為String
?? ? * @param htmlFileName
?? ? * @return
?? ? * @throws Exception
?? ? */
?? ?public static String readHtmlToString(String htmlFileName) throws Exception{
?? ??? ?InputStream is = null;
?? ??? ?Reader reader = null;
?? ??? ?try {
?? ??? ??? ?is = MailUtil.class.getClassLoader().getResourceAsStream(htmlFileName);
?? ??? ??? ?if (is == ?null) {
?? ??? ??? ??? ?throw new Exception("未找到模板文件");
?? ??? ??? ?}
?? ??? ??? ?reader = new InputStreamReader(is, "UTF-8"); ?
?? ??? ??? ?StringBuilder sb = new StringBuilder();
?? ??? ??? ?int bufferSize = 1024;
?? ??? ??? ?char[] buffer = new char[bufferSize];
?? ??? ??? ?int length = 0;
?? ??? ??? ?while ((length = reader.read(buffer, 0, bufferSize)) != -1){
?? ??? ??? ??? ?sb.append(buffer, 0, length);
?? ??? ??? ?}
?? ??? ??? ?return sb.toString();
?? ??? ?} finally {
?? ??? ??? ?try {
?? ??? ??? ??? ?if (is != null) {
?? ??? ??? ??? ??? ?is.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?logger.error("關(guān)閉io流異常", e);
?? ??? ??? ?}
?? ??? ??? ?try {
?? ??? ??? ??? ?if (reader != null) {
?? ??? ??? ??? ??? ?reader.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch ( IOException e) {
?? ??? ??? ??? ?logger.error("關(guān)閉io流異常", e);
?? ??? ??? ?}
?? ??? ?}
?? ?}
}HTML模板
<!DOCTYPE html> <html> ?? ?<head> ?? ??? ?<meta charset="utf-8"/> ?? ??? ?<title>java 郵件發(fā)送</title> ?? ?</head> ?? ?<body> ?? ??? ?<h1 id="title"></h1> ?? ??? ?<div id="content"></div> ?? ?</body> </html>
測(cè)試
package test.email;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class SendHtmlTemplateMail {
?? ?
?? ?public static void main(String[] args) throws Exception {
?? ??? ?// 讀取html模板
?? ??? ?String html = MailUtil.readHtmlToString("mailTemplate.html");
?? ??? ?
?? ??? ?// 寫(xiě)入模板內(nèi)容
?? ??? ?Document doc = Jsoup.parse(html);
?? ??? ?doc.getElementById("title").html("java 郵件發(fā)送測(cè)試");
?? ??? ?doc.getElementById("content").html("么么噠");
?? ??? ?String result = doc.toString();
?? ??? ?
?? ??? ?String mailHost = "smtp.qq.com";
?? ??? ?String fromMail = "";
?? ??? ?String fromName = "小灰";
?? ??? ?String fromMailPwd = "";
?? ??? ?String toMails = "";
?? ??? ?String mailTitle = "hello javamail";
?? ??? ?String mailContent = result;
? ? ? ??
?? ??? ?// 發(fā)送郵件
?? ??? ?MailUtil.sendMail(mailHost, fromMail, fromName, fromMailPwd, toMails, mailTitle, mailContent);
?? ?}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)管理的操作方法
Flyway是一個(gè)開(kāi)源的數(shù)據(jù)庫(kù)版本管理工具,并且極力主張“約定大于配置”,簡(jiǎn)單、專(zhuān)注、強(qiáng)大。接下來(lái)通過(guò)本文給大家介紹SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)管理的方法,感興趣的朋友一起看看吧2021-09-09
Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程
這篇文章主要介紹了Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
詳解Java對(duì)象序列化為什么要使用SerialversionUID
這篇文章主要介紹了詳解Java對(duì)象序列化為什么要使用SerialversionUID,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
SpringBoot實(shí)現(xiàn)HTTP調(diào)用的七種方式總結(jié)
小編在工作中,遇到一些需要調(diào)用三方接口的任務(wù),就需要用到 HTTP 調(diào)用工具,這里,我總結(jié)了一下 實(shí)現(xiàn) HTTP 調(diào)用的方式,共有 7 種(后續(xù)會(huì)繼續(xù)新增),需要的朋友可以參考下2023-09-09
java數(shù)據(jù)結(jié)構(gòu)基礎(chǔ):線性表
這篇文章主要介紹了Java的數(shù)據(jù)解構(gòu)基礎(chǔ),希望對(duì)廣大的程序愛(ài)好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-07-07

