Java网站注册发送邮件

本文介绍了如何使用Java Web技术实现网站注册时通过多线程发送邮件,以提升用户体验。涉及的步骤包括:准备注册页面、编写User实体类、创建多线程邮件发送类Sendmail以及在Servlet中调用邮件发送。邮件发送使用了QQ邮箱SMTP服务器,并配置了SSL加密。当用户提交注册信息后,后台启动新线程发送验证邮件,并将控制权交还给前端,显示注册成功的提示。
摘要由CSDN通过智能技术生成

网站注册发送邮件

由于邮件的发送需要一定的时间,页面难以及时的跳转,为了提高用户的体验,故采取多线程的形式

所需jar包

servlet-api;mail;activation

准备注册页面

<%@ page contentType="text/html; charset = utf-8" pageEncoding="UTF-8"
         language="java" %>
<html>
<head>
    <title>注册</title>
</head>

<body>
<form action="${pageContext.request.contextPath}/send.do"  method="post">
    <p>用户名:<input type="text" name="username"></p>
    <p>密码:<input type="password" name="pwd"></p>
    <p>邮箱:<input type="email" name="email"></p>
    <p><input type="submit"> | <input type="reset"></p>

</form>
</body>
</html>

编写User实体类

package com.feng.pojo;

import java.io.Serializable;

public class User implements Serializable {
    private String username;
    private String pwd;
    private String email;

    public User() {
    }

    public User(String username, String pwd, String email) {
        this.username = username;
        this.pwd = pwd;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "pojo{" +
                "username='" + username + '\'' +
                ", pwd='" + pwd + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}

编写多线程邮件发送

邮件发送的具体步骤可参照:https://blog.csdn.net/shuati2000/article/details/120757667

package com.feng.util;

import com.feng.pojo.User;
import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class Sendmail extends Thread{

    private User user;
    public Sendmail(User user){
        this.user = user;
    }
    @Override
    public void run() {
        try {
            Properties prop = new Properties();
            prop.setProperty("mail.host", "smtp.qq.com");    //这只QQ邮箱服务器
            prop.setProperty("mail.transport.protocol", "smtp");      //邮件发送协议
            prop.setProperty("mail.smtp.auth", "true");      //验证用户名密码

            //关于QQ邮箱需要设置SSL加密
            MailSSLSocketFactory sf = null;

            sf = new MailSSLSocketFactory();

            sf.setTrustAllHosts(true);
            prop.put("mail.smtp.ssl.enable", "true");
            prop.put("mail.smtp.socketFactory", sf);

            //1.创建定义整个应用程序所需的环境信息的Session对象
            Session session = Session.getDefaultInstance(prop, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("1002960106@qq.com", "授权码");
                }
            });
            //开启Session的Dubug模式,这样就可以查看程序发送Email的状态
            session.setDebug(true);

            //2.通过Session得到transport
            Transport ts = session.getTransport();

            //3.使用邮件的用户名和授权码连上邮件的服务器
            ts.connect("smtp.qq.com","1002960106@qq.com","授权码");

            //4.创建邮件
            MimeMessage message = new MimeMessage(session);
            //指明发件人
            message.setFrom(new InternetAddress("1002960106@qq.com"));
            //指明邮件的收件人
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(user.getEmail());
            //邮件的标题
            message.setSubject("Good Study Day Day Up");

            //邮件的文本内容
            message.setContent("欢迎您的注册:\n"+"您的用户名为:"+user.getUsername()+"密码为:"+user.getPwd(),"text/html;charset=UTF-8");
            //5.发送邮件
            ts.sendMessage(message,message.getAllRecipients());
            //6.关闭连接
            ts.close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

调用多线程进行邮件发送

package com.feng.servlet;

import com.feng.pojo.User;
import com.feng.util.Sendmail;
import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;

public class loginSend extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(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);

        Sendmail sendmail = new Sendmail(user);
        sendmail.start();

        req.setAttribute("msg","欢迎您的加入,注册成功,我们已经发送了一份带有注册信息的邮件,请注意查收!");
        req.getRequestDispatcher("sucess.jsp").forward(req,resp);

    }
}

成功跳转页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>注册成功</title>
</head>
<body>

${msg}

</body>
</html>

在web.xml对servlet进行组测

大功告成!!!赶紧来进行邮件发送吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值