javaEE(17)_邮件原理与JavaMail开发
一、Java邮件开发介绍
二、邮件开发涉到的一些基本概念
三、手工演示电子邮件的发送
整个流程如下:
telnet localhost 25 //telnet 可连接互联网任意一台主机
ehlo flx
auth login
YWFh
MTIz
mail from: <aaa@flx.com>
rcpt to: <bbb@flx.com> data
from: <jjs@taiwan.com>
to: <bbb@flx.com>
subject: test 美令,咱们分手吧
中正
.
quit telnet localhost
user aaa
pass
stat
list
retr quit
四、编写Socket程序发送电子邮件
public class Demo1 { public static void main(String[] args) throws Exception { Socket socket = new Socket("localhost",);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
OutputStream out = socket.getOutputStream(); System.out.println(br.readLine());
out.write("ehlo flx\r\n".getBytes()); System.out.println(br.readLine());
System.out.println(br.readLine()); out.write("auth login\r\n".getBytes());
System.out.println(br.readLine()); out.write("YWFh\r\n".getBytes());
System.out.println(br.readLine()); out.write("MTIz\r\n".getBytes());
System.out.println(br.readLine()); out.write("mail from: <aaa@flx.com>\r\n".getBytes());
System.out.println(br.readLine()); out.write("rcpt to: <bbb@flx.com>\r\n".getBytes());
System.out.println(br.readLine()); out.write("data\r\n".getBytes());
System.out.println(br.readLine()); out.write("from:<aaa@flx.com>\r\nto:<bbb@flx.com>\r\nsubject:test\r\n".getBytes());
out.write(".\r\n".getBytes());
System.out.println(br.readLine()); out.write("quit\r\n".getBytes());
System.out.println(br.readLine()); br.close();
out.close();
socket.close();
}
}
五、创建邮件—— MIME协议
MIME协议常用头字段
Content-type:字段
Content-Disposition头字段
Content-Disposition: attachment; filename=".bmp"
上面的MIME头字段表示MIME消息体的内容为邮件附件,附件名"1.bmp"
Content-ID头字段
Content-ID: it315logo_gif
那么,在HTML正文中就需要使用如下HTML语句来引用该图片资源:
<img src="cid:it315logo_gif">
注意,在引用Content-ID头字段标识的内嵌资源时,要在资源的唯一标识号前面加上"cid:",以说明要采用唯一标识号对资源进行引用.
MIME协议例:
//创建简单邮件
public class SimpleMail {
public static void main(String[] args) throws Exception { Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("aaa@flx.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("bbb@flx.com"));
message.setSubject("test");
message.setContent("aaaaaaaaaaaa", "text/html");
message.saveChanges();
message.writeTo(new FileOutputStream("c:\\1.eml"));
}
}
//创建带图片邮件
public class ImageMail { public static void main(String[] args) throws Exception { Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("aaa@flx.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("bbb@flx.com"));
message.setSubject("test"); //创建邮件中的数据
//创建正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("aaaa<br/><img src='cid:1.jpg'><br/>aaaaaaaa", "text/html"); //创建图片,不需要再setContent,setDataHandler可以感知类型
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src/1.jpg"));//jaf技术,把文件作为流
image.setDataHandler(dh);
image.setContentID("1.jpg"); //描述数据之间的关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related"); message.setContent(mm);
message.saveChanges(); message.writeTo(new FileOutputStream("c:\\1.eml"));
}
}
//创建带附件的邮件
public class AttachMail { public static void main(String[] args) throws Exception { Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("aaa@flx.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("bbb@flx.com"));
message.setSubject("test"); //创建封装正文数据的bodypart
MimeBodyPart text = new MimeBodyPart();
text.setContent("aaaaaaaaaaaa", "text/html"); //创建封装附件数据的bodypart
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src/1.mp3"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); //设置附件名 //mixed
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(attach);
mm.setSubType("mixed"); message.setContent(mm);
message.saveChanges(); message.writeTo(new FileOutputStream("c:\\1.eml"));
}
}
//创建最复杂邮件
public class ComplexMail { public static void main(String[] args) throws Exception { //创建邮件
Session session = Session.getDefaultInstance(new Properties());
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("aaa@flx.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("bbb@flx.com"));
message.setSubject("测试"); //创建bodypart封装正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是中文邮件a<img src='cid:1.jpg'>", "text/html;charset=UTF-8"); //创建bodypart封装图片
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src/1.jpg")));
image.setContentID("1.jpg"); //创建bodypart封装附件,解决乱码
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src/光辉岁月.mp3"));
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeText(dh.getName()));//content-disposition //描述数据关系
MimeMultipart content = new MimeMultipart();
content.addBodyPart(text);
content.addBodyPart(image);
content.setSubType("related");
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content);
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(mbp);
mm.addBodyPart(attach);
mm.setSubType("mixed"); message.setContent(mm);
message.saveChanges(); message.writeTo(new FileOutputStream("c:\\1.eml"));
}
}
ps:MIME协议已经突破原来的邮件领域广泛用在数据描述协议中,简单的一堆数据,不同类型,传给对方.
六、发送邮件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAicAAACnCAIAAABFHWt7AAAgAElEQVR4nO2dTWgb2R3Ax96ypO2hSmnB20vt7Tar0NKO9iRfityWrgOFOqcml+JAS7SnKj0UZQ+VN5uunOAgNatW2qDiaTFYUBOZVqxdKjIUQQQrWEENq4MhczBEB8MOrGAHosbTw7DT2Xkf8+b7Q//fSXp6M/Mf2f7//D7mPU4FAAAAgKDgwg4AAAAAmCHAOgAAAE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ANtqab/v7+84mJpycnIBygJkFrAMADtE66GxNaJ5MJp1Op9lsgnKAmQWsAwDOmUwmrVZLW1OALhJJkvb397e2tjwZEwKA+ALWAQC3TCaTXq9XrVabzaY2MUE0IAjCxsaGIAjwUA4AqGAdAPCQk5MTbWKC0TqwEwEAGAHrAAAAAMHxP6lTDu1FAeIIAAAAAElFTkSuQmCC" alt="" />
使用JavaMail发送一封简单的邮件:
//发送邮件
public class SendMail { public static void main(String[] args) throws Exception { Properties prop = new Properties();
prop.setProperty("mail.smtp.host", "localhost");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true"); javax.mail.Session session = javax.mail.Session.getInstance(prop);
session.setDebug(true);//打印与服务器的交互过程 Message message = createMessage(session);
Transport ts = session.getTransport();
ts.connect("aaa", "");
ts.sendMessage(message,message.getAllRecipients());//获取收件人
ts.close();
} private static Message createMessage(Session session) throws Exception {
//创建邮件
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("aaa@flx.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("bbb@flx.com"));
message.setSubject("test"); message.setContent("aaaaaaaaaaaa", "text/html");
message.saveChanges(); return message;
}
}
七、WEB应用中集成邮件发送程序
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>注册页面</title></head> <body>
<form action="${pageContext.request.contextPath }/servlet/RegisterServlet" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
电子邮箱:<input type="text" name="email"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
//用户注册时,发送邮件通知
public class RegisterServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws Exception { String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email"); User user = new User();
user.setEmail(email);
user.setPassword(password);
user.setUsername(username); try{
//开启发送邮件线程,如果不开线程的话,如邮件有问题,后续代码无法执行
Thread t = new Thread(new SendMail(user));
t.start(); request.setAttribute("message", "注册成功!!");
}catch (Exception e) {
e.printStackTrace();
request.setAttribute("message", "注册失败!!");
}
request.getRequestDispatcher("/message.jsp").forward(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
} }
public class SendMail implements Runnable { private String host = "localhost";
private String email = "aaa@flx.com";
private String username = "aaa";
private String password = "123";
private User user; public SendMail(User user) {
this.user = user;
}
public void run() {
try{
Thread.sleep(1000*30);
send(user);
}catch (Exception e) {
throw new RuntimeException(e);
}
} public void send(User user) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", host);
prop.setProperty("mail.transport.protocol", "smtp");
Session session = Session.getInstance(prop);
Message message = createmessage(session, user);
Transport ts = session.getTransport();
ts.connect(username, password);
ts.sendMessage(message, message.getAllRecipients());
ts.close();
} public Message createmessage(Session session,User user) throws Exception{ MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(email));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
message.setSubject("xxx网站用户注册邮件"); String content = "恭喜您,注册成功,您注册的用户名:"+ user.getUsername()!!";
message.setContent(content, "text/html;charset=UTF-8");
message.saveChanges();
return message;
}
}
八、邮件群发
javaEE(17)_邮件原理与JavaMail开发的更多相关文章
- 基于JavaMail开发邮件发送器工具类
基于JavaMail开发邮件发送器工具类 在开发当中肯定会碰到利用Java调用邮件服务器的服务发送邮件的情况,比如账号激活.找回密码等功能.本人之前也碰到多次这样需求,为此特意将功能封装成一个简单易用 ...
- OAuth的机制原理讲解及开发流程
本想前段时间就把自己通过QQ OAuth1.0.OAuth2.0协议进行验证而实现QQ登录的心得及Demo实例分享给大家,可一直很忙,今天抽点时间说下OAuth1.0协议原理,及讲解下QQ对于Oaut ...
- JavaMail开发教程01开山篇
序 其实想写JavaMail这一系列的博客已经有一个月之久了,缘起是某次乱逛传智播客官网浏览到相关的视频教程,想起大学时代学过的计算机网络提到邮件相关的协议,但遗憾的是到目前为止还没有接触计算机网络编 ...
- Zookeeper原理和实战开发经典视频教程 百度云网盘下载
Zookeeper原理和实战开发 经典视频教程 百度云网盘下载 资源下载地址:http://pan.baidu.com/s/1o7ZjPeM 密码:r5yf
- OAuth的机制原理讲解及开发流程(转)
1.OAuth的简述 OAuth(Open Authorization,开放授权)是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是 ...
- Linux中Postfix邮件原理介绍(一)
邮件相关协议 SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议, 工作在TCP的25端口.它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式 ...
- javaEE(14)_文件上传下载
一.文件上传概述 1.实现web开发中的文件上传功能,需完成如下二步操作: •在web页面中添加上传输入项•在servlet中读取上传文件的数据,并保存到本地硬盘中. 2.如何在web页面中添加上传输 ...
- 【转载】OAuth的机制原理讲解及开发流程
1.OAuth的简述 OAuth(Open Authorization,开放授权)是为用户资源的授权定义了一个安全.开放及简单的标准,第三方无需知道用户的账号及密码,就可获取到用户的授权信息,并且这是 ...
- java-阿里邮件推送服务开发 -- 发送邮箱验证码
参考文档: 如何在 DNS 服务器上配置域名:https://help.aliyun.com/knowledge_detail/39397.html?spm=5176.2020520150.102.d ...
随机推荐
- Linux 一些问题
终端以root账号执行 su - root
- centos 7.3 安装vmtools,解决无法编译共享文件夹模块
环境说明: vmware 12.5.0 build-4352439 centos 7.3.1611 64位,内核版本:Linux version 3.10.0-514.16.1.el7.x86_6 ...
- C笔记列表
笔记列表 指针是一个变量,其值为另一个变量的地址,即,内存位置的直接地址.就像其他变量或常量一样,您必须在使用指针存储其他变量地址之前,对其进行声明. 要理解指针就要先理解计算机的内存.计算机内存会被 ...
- Android近场通信---NFC基础(一)(转)
转自 http://blog.csdn.net/think_soft/article/details/8169483 本文译自:http://developer.android.com/guide/t ...
- Hexo - Template render error unexpected token
问题与分析 今天发现在使用hexo g时报错如下: FATAL Something's wrong. Maybe you can find the solution here: http://hexo ...
- SVN有任何胜过git的地方吗?
SVN有任何胜过git的地方吗? 好的技术问题通常会引出技术专家们依据经验得出的深层次的观点.但对于这样的问题的答案也很容易演变成完全基于个人喜好的情绪倾泄,而不是根据事实.标准和具体的专业知识.就比 ...
- [題解](并查集)luogu_P2391 白雪皚皚
今天被老師留的作業搞死了,全是裸的水題,難題就那麼兩道我還沒寫......,狗屎 1.倒序處理,每個點至多會被更新一次 2.所以要做的就是快速找到下一個不同顏色的點, 3.然而不知道怎麼就 想到用并查 ...
- scrapy框架中Spiders用法
scrapy框架中Spiders用法 Spider类定义了如何爬去某个网站,包括爬取的动作以及如何从网页内容中提取结构化的数据 总的来说spider就是定义爬取的动作以及分析某个网页 工作流程分析 以 ...
- [ZPG TEST 114] 括号匹配【树分治 点分治 括号序列】
1. 括号匹配 有一棵树,每个节点上都有一个括号(左括号或者右括号).有多少个有序点对(u, v)从u到v的路径上的节点构成的字符串是一个合法的括号匹配?(我们称这样的点对是合法的) 输 ...
- Python开发 第01课 Python 简介
一.Python 介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...