在我们的日常工作与生活中,邮件的发送必可不少。那么,用javaweb发送邮件的方式有哪几种呢?邮件发送的基础又是什么?让我们一起往下看。

首先,为大家概述下javaweb邮件发送的几种方式,并通过实例代码为大家展示:

1.User:package org.westos.mail;
public class User
{
private String name;
private String password;
private String mail;
public User()
{}
public User(String name, String password, String mail)
{
this.name = name;
this.password = password;
this.mail = mail;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getMail()
{
return mail;
}
public void setMail(String mail)
{
this.mail = mail;
}
@Override
public String toString()
{
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", mail='" + mail + '\'' +
'}';
}
}
2.Servlet:package org.westos.mail;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Servlet extends javax.servlet.http.HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
//处理前端请求
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
//将信息封装进user对象
User user = new User(username, password, email);
SendMail sendMail = new SendMail(user);
sendMail.start(); //开启线程
request.setAttribute("msg", "发送成功");
try
{
request.getRequestDispatcher("msg.jsp")
.forward(request, response);
}
catch (ServletException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
3.SengMail:package org.westos.mail;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.security.PublicKey;
import java.util.Properties;
public class SendMail extends Thread
{
//发件人信息
private String From = "619046217@qq.com";
//发件人邮箱
private String recipient = "619046217@qq.com";
//邮箱密码
private String password = "16位授权码";
//邮件发送的服务器
private String host = "smtp.qq.com";
//收件人信息
private User user;
public SendMail(User user)
{
this.user = user;
}
@Override
public void run()
{
try
{
Properties properties = new Properties();
properties.setProperty("mail.host", "smtp.qq.com");
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
//QQ存在一个特性设置SSL加密
MailSSLSocketFactory sf = null;
try
{
sf = new MailSSLSocketFactory();
}
catch (GeneralSecurityException e)
{
e.printStackTrace();
}
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
//创建一个session对象
Session session = Session.getDefaultInstance(properties, new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(recipient, password);
}
});
//开启debug模式
session.setDebug(true);
//获取连接对象
Transport transport = null;
try
{
transport = session.getTransport();
}
catch (NoSuchProviderException e)
{
e.printStackTrace();
}
//连接服务器
transport.connect(host, From, password);
//创建一个邮件发送对象
MimeMessage mimeMessage = new MimeMessage(session);
//邮件发送人
mimeMessage.setFrom(new InternetAddress(recipient));
//邮件接收人
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(user.getMail()));
//邮件标题
mimeMessage.setSubject("网站注册成功");
//邮件内容
mimeMessage.setContent("网站注册成功,密码为" + user.getPassword() + ",请妥善保管密码", "text/html;charset=UTF-8");
//发送邮件
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
4.register.jsp:
注册页面 
 
用户名:
密码:
邮箱:
5.msg.jsp:
Title 
 
${msg}
6.web.xml:<?xml  version="1.0" encoding="UTF-8"?>
Servlet
org.westos.mail.Servlet
Servlet
/a.do

接下来我们看一下邮件发送的基础是什么。在很多情况下,我们发送邮件的时候,都是通过手动配置邮件客户端让它们连接到邮件服务器或者直接登录到一些网站查看邮件。但是实际情况下,他们无非是对应两种连接邮件服务器的客户端而已,一种是单机版的,一种是web版的。他们的本质还是所有区别的。发送的邮件消息提交给邮件服务器,再由邮件服务器转发到目的地。

以上就是关于javaweb发送邮件的方式概述,以及为大家简述邮件发送基础。