Loading

Javaweb邮件发送强化用户体验

设置多线程让用户注册后立即得到反馈同时进行邮件发送功能

//邮件发送方法区设置为多线程,将方法体放入run方法中
//这里可以改为实现Runnable接口,避免单继承局限性,灵活方便,方便同一个对象被多个线程使用
public class SendMail extends Thread {

    private String from = "****@qq.com";
    private String userName = "****@qq.com";
    private String password = "授权码";
    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();
            //设置QQ邮件服务器
            properties.setProperty("mail.host", host);
            //邮件发送协议
            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(from, password);
                }
            });
            //--开启debug模式,这样就可以查看到程序发送email的运行状态
            session.setDebug(true);
            //2.通过session得到transport对象
            Transport ts = session.getTransport();
            //3.使用邮箱的用户名和授权码连上邮件服务器
            ts.connect(host, from, password);
            //4.创建邮件
            MimeMessage message = new MimeMessage(session);
            //--邮件的发件人
            message.setFrom(new InternetAddress(userName));
            //--邮件的收件人
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(userName));
            //--邮件的标题
            message.setSubject("hello");
            //--邮件的文本内容
            message.setContent("<h1 style='color: red'>你好啊</h1>", "text/html;charset=UTF-8");
            //5.发送邮件
            ts.sendMessage(message, message.getAllRecipients());
            //6.关闭连接
            ts.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

servlet请求处理

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受用户请求,封装成对象
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        String email = request.getParameter("email");

        User user = new User(userName, password, email);
        SendMail sendMail = new SendMail(user);
        sendMail.start();//开启线程

        request.setAttribute("message", "发送成功");
        request.getRequestDispatcher("info.jsp").forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

封装实体类对象

public class User implements Serializable {
    private String userName;
    private String password;
    private String email;

    public User() {
    }

    public User(String userName, String password, String email) {
        this.userName = userName;
        this.password = password;
        this.email = email;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}
posted @ 2022-03-15 21:17  Cn_FallTime  阅读(24)  评论(0编辑  收藏  举报