JavaWeb邮件收发
邮件收发流程与架构
邮箱配置
要在网路上实现邮件功能,必须要有专门的邮件服务器STMP服务器地址: stmp.xxx.com 例如:smtp.163.com smtp.qq.com。 发送邮件的时候,需要对相应邮箱进行鉴权验证,验证采用授权码的方式,获取授权码的方法如下:
-
当通过网易163邮箱发送邮件的时候:
1.登录网易邮箱,打开设置
2.开启SMTP服务,这个时候系统会进行验证并给出一个授权码,在代码中授权码会代替邮箱密码起到鉴权作用
-
其他邮箱类似
代码架构
使用工具包
1.JavaMail API
2.JavaBeans™ Activation Framework
所有的Java邮件收发功能基本都通过上面两个工具包来实现,实现过程简单。
工具包使用架构
Java代码
邮件分为两类:
- 纯文本简单邮件
- 带有视频、图片等附件的复杂邮件
简单邮件发送实现(qq->163为例)
public class MailDemo01 {
//发送一个简单的邮件
public static void main(String[] args) throws Exception {
//为Session设置属性
Properties properties = new Properties();
properties.setProperty("mail.host", "smtp.qq.com");//设置QQ邮件服务器
properties.setProperty("mail.transport.protocol", "smtp"); //邮件发送协议
properties.setProperty("mail.smtp.auth", "true"); //需要验证用户名和密码
//QQ邮箱,还需要设置SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
//其他邮箱不需要上述代码
//使用JavaMail发送邮件的5个步骤
//1.创建定义整个应用程序所需的环境信息的Session对象
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发送人邮件用户名、授权码
return new PasswordAuthentication("qq邮箱@qq.com","授权码");
}
});
//开启Session的debug模式,可以看到程序发送Email的运行状态
session.setDebug(true);
//2.通过session得到transport对象,用来发送邮件
Transport transport = session.getTransport();
//3.使用邮箱的用户名和授权码连接上邮件服务器
transport.connect("smtp.qq.com","qq邮箱@qq.com","授权码");
//4.创建邮件 : 写邮件
//5.发送邮件
//传递session作为参数,告诉邮件相关信息
MimeMessage message = new MimeMessage(session);
//指明邮件的发件人
message.setFrom(new InternetAddress("qq邮箱@qq.com"));
//指明邮件的收件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress("网易邮箱@163.com"));
//邮件的标题
message.setSubject("这是一封简单邮件");
//邮件的内容 , 通过html格式发送,还可以加样式
message.setContent("<h1 style='color : red'>这是我发送的第一封邮件</h1>","text/html;charset=UTF-8");
//发送邮件,参数为邮件和发送的对象
transport.sendMessage(message,message.getAllRecipients());
//6.关闭连接
transport.close();
}
}
复杂邮件发送实现 MIME
MimeBodypart类 : 表示一个MIME信息
MimeMulitpart类 : 用来组合多个MIME消息,可以包含多个MimeBodypart对象
代码实例
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.setProperty("mail.host", "smtp.qq.com");//设置QQ邮件服务器
properties.setProperty("mail.transport.protocol", "smtp"); //邮件发送协议
properties.setProperty("mail.smtp.auth", "true"); //需要验证用户名和密码
//QQ邮箱,还需要设置SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
//使用JavaMail发送邮件的5个步骤
//1.创建定义整个应用程序所需的环境信息的Session对象
//只有QQ有,其他邮箱不用
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发送人邮件用户名、授权码
return new PasswordAuthentication("2661190790@qq.com","vmdjfvzgxuigebai");
}
});
//开启Session的debug模式,可以看到程序发送Email的运行状态
session.setDebug(true);
//2.通过session得到transport对象
Transport transport = session.getTransport();
//3.使用邮箱的用户名和授权码连接上邮件服务器
transport.connect("smtp.qq.com","...@qq.com","授权码");
//4.创建邮件 : 写邮件
//5.发送邮件
//注意需要传递Session
MimeMessage message = new MimeMessage(session); //传递session
//指明邮件的发件人
message.setFrom(new InternetAddress("...@qq.com"));
//指明邮件的收件人 自己给自己发
message.setRecipient(Message.RecipientType.TO,new InternetAddress("...@163.com"));
//邮件的标题
message.setSubject("这是一封简单邮件");
//准备图片数据
MimeBodyPart image = new MimeBodyPart();
//图片需要经过数据处理 , 处理器为DataHandler
DataHandler dh = new DataHandler(new FileDataSource("C:\\Users\\HP\\Desktop\\mail-Java\\src\\img.png"));
//在body主题中放入处理的图片数据
//给图片设置一个id
image.setDataHandler(dh);
image.setContentID("bz.jpg");
//准备正文数据
MimeBodyPart text = new MimeBodyPart();
//cid 为 ConetentID bz.jpg 为 img.png的cid
text.setContent("这是一封邮件正文带图片<img src='cid:bz.jpg'>的邮件","text/html;charset=UTF-8");
//准备附件数据
MimeBodyPart body3 = new MimeBodyPart();
body3.setDataHandler(new DataHandler(new FileDataSource("C:\\Users\\HP\\Desktop\\设计与开发文档.docx")));
body3.setFileName("设计与开发文档");//设置附件的名字
//描述数据关系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.addBodyPart(body3);
mm.setSubType("mixed");
//设置到消息当中,保存修改
message.setContent(mm);
message.saveChanges();
//发送邮件
transport.sendMessage(message,message.getAllRecipients());
//6.关闭连接
transport.close();
}
在Web工程中邮件发送的实现
** 邮件发送**
用户在前端输入自己的邮箱,Servlert中获取该属性,然后对该邮箱发送数据即可。
线程问题
为了优化用户的体验(不用等待耗时而可以继续浏览),邮件的发送应该采用多线程的方式
//多线程实现用户注册中的邮件发送 : 异步处理
public class SendMail extends Thread{
//用于给用户发送邮件的邮箱
private String from = "邮箱@qq.com";
//邮箱的用户名
private String username = "邮箱@qq.com";
//邮箱的密码
private String password = "授权码";
//发送邮件的服务器地址
private String host = "stmp.qq.com";
private User user;
public SendMail(User user)
{
this.user = user;
}
//重写run方法,发送邮件
@Override
public void run() {
Properties properties = new Properties();
properties.setProperty("mail.host", host);//设置QQ邮件服务器
properties.setProperty("mail.transport.protocol", "smtp"); //邮件发送协议
properties.setProperty("mail.smtp.auth", "true"); //需要验证用户名和密码
//QQ邮箱,还需要设置SSL加密
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
//使用JavaMail发送邮件的5个步骤
//1.创建定义整个应用程序所需的环境信息的Session对象
//只有QQ有,其他邮箱不用
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
//发送人邮件用户名、授权码
return new PasswordAuthentication(username,password);
}
});
//开启Session的debug模式,可以看到程序发送Email的运行状态
session.setDebug(true);
//2.通过session得到transport对象
Transport transport = session.getTransport();
//3.使用邮箱的用户名和授权码连接上邮件服务器
transport.connect(host,username,password);
//4.创建邮件 : 写邮件
//5.发送邮件
//注意需要传递Session
MimeMessage message = new MimeMessage(session); //传递session
//指明邮件的发件人
message.setFrom(new InternetAddress(from));
//指明邮件的收件人 自己给自己发
message.setRecipient(Message.RecipientType.TO,new InternetAddress(user.getEail())); //把目的邮箱传进来
//邮件的标题
message.setSubject("这是一封简单邮件");
//邮件的内容
message.setContent("<h1 style='color : red'>这是我发送的第一封邮件</h1>","text/html;charset=UTF-8");
//发送邮件
transport.sendMessage(message,message.getAllRecipients());
//6.关闭连接
transport.close();
}
}