java发送邮件javamail, freemarker读取html模板内容
https://www.cnblogs.com/xdp-gacl/p/4216311.html
一、RFC882文档简单说明
RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔。
邮件头包含的内容有:
- from字段 --用于指明发件人
- to字段 --用于指明收件人
- subject字段 --用于说明邮件主题
- cc字段 -- 抄送,将邮件发送给收件人的同时抄送给另一个收件人,收件人可以看到邮件抄送给了谁
- bcc字段 -- 密送,将邮件发送给收件人的同时将邮件秘密发送给另一个收件人,收件人无法看到邮件密送给了谁
邮件体指的就是邮件的具体内容。
二、MIME协议简单介绍
在我们的实际开发当中,一封邮件既可能包含图片,又可能包含有附件,在这样的情况下,RFC882文档规定的邮件格式就无法满足要求了。
MIME协议是对RFC822文档的升级和补充,它描述了如何生产一封复杂的邮件。通常我们把MIME协议描述的邮件称之为MIME邮件。MIME协议描述的数据称之为MIME消息。
对于一封复杂邮件,如果包含了多个不同的数据,MIME协议规定了要使用分隔线对多段数据进行分隔,并使用Content-Type头字段对数据的类型、以及多个数据之间的关系进行描述。
三、使用JavaMail创建邮件和发送邮件
JavaMail创建的邮件是基于MIME协议的。因此可以使用JavaMail创建出包含图片,包含附件的复杂邮件。
3.1、JavaMail API的简单介绍
3.2、创建邮件发送测试项目
3.3、发送一封只包含文本的简单邮件
package me.gacl.main; 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; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、使用邮箱的用户名和密码连上邮件服务器,发送邮件时,发件人需要提交邮箱的用户名和密码给smtp服务器,用户名和密码都通过验证之后才能够正常发送邮件给收件人。
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createSimpleMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createSimpleMail
* @Description: 创建一封只包含文本的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//创建邮件对象
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//指明邮件的收件人,现在发件人和收件人是一样的,那就是自己给自己发
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
//邮件的标题
message.setSubject("只包含文本的简单邮件");
//邮件的文本内容
message.setContent("你好啊!", "text/html;charset=UTF-8");
//返回创建好的邮件对象
return message;
}
}
3.4、发送包含内嵌图片的邮件
package me.gacl.main; import java.io.FileOutputStream;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器,需要发件人提供邮箱的用户名和密码进行验证
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createImageMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createImageMail
* @Description: 生成一封邮件正文带图片的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createImageMail(Session session) throws Exception {
//创建邮件
MimeMessage message = new MimeMessage(session);
// 设置邮件的基本信息
//发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//邮件标题
message.setSubject("带图片的邮件"); // 准备邮件数据
// 准备邮件正文数据
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是一封邮件正文带图片<img src='cid:xxx.jpg'>的邮件", "text/html;charset=UTF-8");
// 准备图片数据
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\1.jpg"));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
// 描述数据关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related"); message.setContent(mm);
message.saveChanges();
//将创建好的邮件写入到E盘以文件的形式进行保存
message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));
//返回创建好的邮件
return message;
}
}
3.5、发送包含附件的邮件
package me.gacl.main; import java.io.FileOutputStream;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createAttachMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createAttachMail
* @Description: 创建一封带附件的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(Session session) throws Exception{
MimeMessage message = new MimeMessage(session); //设置邮件的基本信息
//发件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//邮件标题
message.setSubject("JavaMail邮件发送测试"); //创建邮件正文,为了避免邮件正文中文乱码问题,需要使用charset=UTF-8指明字符编码
MimeBodyPart text = new MimeBodyPart();
text.setContent("使用JavaMail创建的带附件的邮件", "text/html;charset=UTF-8"); //创建邮件附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); // //创建容器描述数据关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed"); message.setContent(mp);
message.saveChanges();
//将创建的Email写入到E盘存储
message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
//返回生成的邮件
return message;
}
}
3.6、发送包含内嵌图片和附件的复杂邮件
package me.gacl.main; import java.io.FileOutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility; /**
* @ClassName: Sendmail
* @Description: 发送Email
* @author: 孤傲苍狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器
ts.connect("smtp.sohu.com", "gacl", "邮箱密码");
//4、创建邮件
Message message = createMixedMail(session);
//5、发送邮件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} /**
* @Method: createMixedMail
* @Description: 生成一封带附件和带图片的邮件
* @Anthor:孤傲苍狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createMixedMail(Session session) throws Exception {
//创建邮件
MimeMessage message = new MimeMessage(session); //设置邮件的基本信息
message.setFrom(new InternetAddress("gacl@sohu.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
message.setSubject("带附件和带图片的的邮件"); //正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("xxx这是女的xxxx<br/><img src='cid:aaa.jpg'>","text/html;charset=UTF-8"); //图片
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));
image.setContentID("aaa.jpg"); //附件1
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); //附件2
MimeBodyPart attach2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));
attach2.setDataHandler(dh2);
attach2.setFileName(MimeUtility.encodeText(dh2.getName())); //描述关系:正文和图片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related"); //描述关系:正文和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
mp2.addBodyPart(attach2); //代表正文的bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed"); message.setContent(mp2);
message.saveChanges(); message.writeTo(new FileOutputStream("E:\\MixedMail.eml"));
//返回创建好的的邮件
return message;
}
}
以上就是使用JavaMail的API创建邮件和发送邮件的全部内容。
如果想发送带html模板的内容,请结合Freemarker来使用。
https://www.cnblogs.com/achengmu/p/10790789.html
String content = htmlToPdfFreeMarkerServer.freeMarkerRender(null, "pdfmodel/flight_delay_content.html");
Properties prop = new Properties();
prop.setProperty("mail.host", "mail.xxxx.com.cn");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail发送邮件的5个步骤
//1、创建session
Session session = Session.getInstance(prop);
//开启Session的debug模式,这样就可以查看到程序发送Email的运行状态
session.setDebug(true);
//2、通过session得到transport对象
Transport ts = session.getTransport();
//3、连上邮件服务器
ts.connect("mail.xxxx.com.cn", "tvweb@xxxx.com.cn", "xxxxx");
//4、创建邮件
Message message = null;
String subject = "xxxxx证明"; //读取模板内容, null表示不需要替换任何变量
String content = htmlToPdfFreeMarkerServer.freeMarkerRender(null, "pdfmodel/flight_delay_content.html"); message = SendMail.createSimpleMail(
"tvweb@tibetairlines.com.cn",
toMail,
subject,
content,
session);
java发送邮件javamail, freemarker读取html模板内容的更多相关文章
- Java代码一行一行读取txt的内容
public static void main(String[] args) { // 文件夹路径 String path = "E:\\eclipse work\\ImageUtil\\s ...
- java 实现poi方式读取word文件内容
1.下载poi的jar包 下载地址:https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- poi读取excel模板,填充内容并导出,支持导出2007支持公式自动计算
/** * 版权所有(C) 2016 * @author www.xiongge.club * @date 2016-12-7 上午10:03:29 */ package xlsx; /** * @C ...
- poi读取Excel模板并修改模板内容与动态的增加行
有时候我们可能遇到相当复杂的excel,比如表头的合并等操作,一种简单的方式就是直接代码合并(浪费时间),另一种就是写好模板,动态的向模板中增加行和修改指定单元格数据. 1.一个简单的根据模板shee ...
- Java之利用Freemarker模板引擎实现代码生成器,提高效率
https://blog.csdn.net/huangwenyi1010/article/details/71249258 java模板引擎freemarker代码生成器 更多 个人分类: 一步一步 ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- java读取txt文件内容
package read; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public ...
随机推荐
- 使用GIT上传文件,VSCODE使用GIT上传项目
GIT使用方法: 1.安装git 2.设置用户名和邮箱: git config --global user.name="haokan1113" git config --globa ...
- 17.Python略有小成(包,logging模块)
Python(包,logging模块) 一.包 什么是包 官网解释 : 包是一种通过使用'.模块名'来组织python模块名称空间的方式 , 具体来讲 , 包就是一个包含有__ init __.py文 ...
- TensorFlow学习笔记(1)—— 基本概念与框架
入门框架时的常见问题 学习框架的原因? 方便.易用 学习框架的哪些知识点? 掌握一个项目的基本流程,就知道需要学习哪些知识点了 迅速学习框架的方法 根据项目每块流程的需要针对性的学 可以看官方的入门教 ...
- MediaPlayer的prepareAsync不回调onPrepared问题
一.问题排查 debug调试的framework日志信息: 根据日志,进入MediaPlayer的framework层看源码 STEP 1 先从 V/MediaPlayerNative: constr ...
- rsync 使用
rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件. rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分, ...
- [LOJ6433] [PKUSC2018] 最大前缀和
题目链接 LOJ:https://loj.ac/problem/6433 Solution 注意到最大前缀要满足什么性质,假设序列\(a[1..n]\)的最大前缀是\(s_x\),那么显然要满足所有\ ...
- Codeforces Round #570 Div. 3
A:暴力从小到大枚举判断. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 10 ...
- [高清] JavaEE开发的颠覆者 Spring Boot实战 完整版
------ 郑重声明 --------- 资源来自网络,纯粹共享交流, 如果喜欢,请您务必支持正版!! --------------------------------------------- 下 ...
- CacheManager.Core
GitHub地址:https://github.com/MichaCo/CacheManager CacheManager的优点: 让开发人员的生活更容易处理和配资缓存,即使是非常复杂的缓存方案. C ...
- Java冒泡排序与快速排序笔记
public class Sort { public static void sort() { Scanner input = new Scanner(System.in); int sort[] = ...