利用JavaMail发送HTML模板邮件
本文实例为大家分享了用JavaMail发送HTML模板邮件的具体代码,供大家参考,具体内容如下
依赖
<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>
工具类
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; /** * 发送邮件工具类 */ public class MailUtil { private final static Logger logger = LoggerFactory.getLogger(MailUtil.class); /** * 邮件发送 * @param mailHost 邮件服务地址 * @param fromMail 发件人 * @param fromName 发件人名 * @param fromMailPwd 发件人密码 * @param toMails 收件人,多个用英文逗号分隔 * @param mailTitle 邮件标题 * @param mailContent 邮件内容 * @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("邮件发送人不能为空"); } // 邮件属性信息 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); // 根据属性新建一个邮件会话 //session.setDebug(true); // 是否打印调试信息 toMailArr = toMails.split(","); for (String to : toMailArr) { MimeMessage message = new MimeMessage(session); // 由邮件会话新建一个消息对象 message.setFrom(new InternetAddress(fromMail));// 设置发件人的地址 message.setRecipient(Message.RecipientType.TO, new InternetAddress(to, fromName));// 设置收件人,并设置其接收类型为TO message.setSubject(mailTitle);// 设置标题 message.setContent(mailContent, "text/html;charset=UTF-8"); // 设置邮件内容类型为html message.setSentDate(new Date());// 设置发信时间 message.saveChanges();// 存储邮件信息 // 发送邮件 Transport transport = session.getTransport(); transport.connect(fromMail, fromMailPwd); transport.sendMessage(message, message.getAllRecipients()); transport.close(); } } /** * 邮件发送(群发) * @param mailHost 邮件服务地址 * @param fromMail 发件人 * @param fromName 发件人名 * @param fromMailPwd 发件人密码 * @param toMails 收件人,多个用英文逗号分隔 * @param mailTitle 邮件标题 * @param mailContent 邮件内容 * @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("邮件发送人不能为空"); } // 邮件属性信息 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); // 根据属性新建一个邮件会话 //session.setDebug(true); // 是否打印调试信息 MimeMessage message = new MimeMessage(session); // 由邮件会话新建一个消息对象 message.setFrom(new InternetAddress(fromMail)); // 设置发件人的地址 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); // 设置收件人,并设置其接收类型为TO message.setSubject(mailTitle); // 设置标题 message.setContent(mailContent, "text/html;charset=UTF-8"); // 设置邮件内容类型为html message.setSentDate(new Date()); // 设置发信时间 message.saveChanges(); // 存储邮件信息 // 发送邮件 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("关闭io流异常", e); } try { if (reader != null) { reader.close(); } } catch ( IOException e) { logger.error("关闭io流异常", e); } } } }
HTML模板
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>java 邮件发送</title> </head> <body> <h1 id="title"></h1> <div id="content"></div> </body> </html>
测试
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"); // 写入模板内容 Document doc = Jsoup.parse(html); doc.getElementById("title").html("java 邮件发送测试"); 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; // 发送邮件 MailUtil.sendMail(mailHost, fromMail, fromName, fromMailPwd, toMails, mailTitle, mailContent); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)