java邮件发送
2、邮件发送
温馨提示:
下面没使用框架发送邮件,有利于理解源码,以后用SpringBoot比这个简单多了,框架都帮你封装好了
发送邮件前必须要开启下图协议:这里拿QQ邮箱举例,其他的也都在设置里面
下面是封装的发送邮件工具类
在前端接收到邮件然后再后端发送邮件
注意:这里为了用户体验好要领开一个线程,这样就不会等到发送完邮件才跳转了!
servlet:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
String email = req.getParameter("email");
User user = new User(username, pwd, email);
EmailUtil emailUtil = new EmailUtil(user);
//开启这个线程
emailUtil.start();
req.setAttribute("message", user.getUsername()+":h欢迎你的到来!");
req.getRequestDispatcher("success.jsp").forward(req, resp);
}
package com.bing.utils;
import com.bing.pojo.User;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
/**
* 邮件授权码
*
* @author zhangbingbing
* @version 1.0
* @date 2020/5/13 18:54
*/
public class EmailUtil extends Thread {
//发送方
private String from = "1479461930@qq.com";
//发送方邮箱的用户名
private String username = "1479461930@qq.com";
//发送方邮箱的授权码或者密码
private String pwd = "邮件授权码";
//发送邮件的服务器地址
private String host = "smtp.qq.com";
private User user;
public EmailUtil(User user) {
this.user = user;
}
@Override
public void run() {
try {
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加密,这个有点像MySQL的安全连接,一般大厂才会有
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable","true");
properties.put("mail.smtp.ssl.socketFactory",sf);
//以上准备工作完成,下面是使用java发送文件的五个步骤
//1、创建整个应用程序才需要的session对象,QQ这种大厂才有
Session session = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
//发送邮件的地址和授权码
return new PasswordAuthentication(username, pwd);
}
});
//2、通过session获取transport对象
Transport transport = session.getTransport();
session.setDebug(true);
//3、使用邮件的用户名和授权码连接上服务器, 这三个参数可以根据邮箱的不同进行改变
transport.connect(host,user.getEmail(),pwd);
MimeMessage message = getMimeMessage2(session);
//5、发送邮件
transport.sendMessage(message,message.getAllRecipients());
//6、关闭连接
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//注册验证时的邮件,没有图片附件什么的
public MimeMessage getMimeMessage2(Session session) throws Exception {
//4、创建邮件写邮件,注意传递session
MimeMessage message = new MimeMessage(session);
//指明邮件的发送人
message.setFrom(new InternetAddress(from));
//邮件的接收人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
//邮件的标题
message.setSubject("注册验证邮件");
//邮件的内容
String info = "congratulations,you login successfully!, 您的用户名是:" + user.getUsername() + "密码是:" + user.getPwd();
//设置到消息中,并保存修改
message.setContent(info, "text/html;charset=utf-8");
message.saveChanges();
return message;
}
//发送复杂的邮件,用的少,真正开发邮箱才会用
public MimeMessage getMimeMessage(Session session) throws Exception {
//4、创建邮件写邮件,注意传递session
MimeMessage message = new MimeMessage(session);
//指明邮件的发送人
message.setFrom(new InternetAddress(from));
//邮件的接收人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
//邮件的标题
message.setSubject("我是邮件!");
//邮件的内容
//准备图片资源
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("图片目录"));
image.setDataHandler(dh);
image.setContentID("beautiful.jpg");
//准备附件资源
MimeBodyPart affix1 = new MimeBodyPart();
DataHandler dh1 = new DataHandler(new FileDataSource("附件目录"));
affix1.setDataHandler(dh1);
affix1.setFileName("dh1.md");
MimeBodyPart affix2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("附件目录"));
affix2.setDataHandler(dh2);
affix2.setFileName("dh2.md");
//准备正文数据
MimeBodyPart text = new MimeBodyPart();
text.setContent("这是一封正文加图片<img src='cid:beautiful.jpg'>邮件", "text/html;charset=utf-8");
//设置数据之间的关系
MimeMultipart mimeMultipart1 = new MimeMultipart();
mimeMultipart1.addBodyPart(image);
mimeMultipart1.addBodyPart(text);
mimeMultipart1.setSubType("related");
MimeBodyPart mimeBodyPart1 = new MimeBodyPart();
mimeBodyPart1.setContent(mimeMultipart1);
MimeMultipart allFile = new MimeMultipart();
allFile.addBodyPart(affix1);
allFile.addBodyPart(affix2);
allFile.addBodyPart(mimeBodyPart1);
//设置到消息中,并保存修改
message.setContent(allFile);
message.saveChanges();
return message;
}
}
以后使用框架慢慢完善!
有用的点个赞吧!!