邮箱验证,工具类

做~自己de王妃 提交于 2020-03-09 02:59:01

邮箱验证,工具类:

emailUtil工具类代码:

 

package com.microClass.util;

import com.sun.mail.util.MailSSLSocketFactory;

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

/**
 * Created by ause on 2017-08-25.
 */
public class MailUtil {
    //public final static int ERA = 0;
    // 设置邮件服务器主机名
    public final static String HOST = "smtp.qq.com";
    // 发送邮件协议名称
    public final static String PROTOCOL = "smtp";
    //发送人的邮箱地址
    public final static String SEND_ADDRESS = "1593139675@qq.com";
    //授权码Authorization code    // ndtqsiupcmxcbabf
    public final static String AUTHORIZATION_CODE = "ndtqsiupcmxcbabf";

    public static void send(String title, String body, String receiveAddress) throws MessagingException, GeneralSecurityException {
        Properties props = new Properties();
        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.host", HOST);
        props.setProperty("mail.transport.protocol", PROTOCOL);

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        msg.setSubject(title);
        msg.setText(body);
        msg.setFrom(new InternetAddress(SEND_ADDRESS));

        Transport transport = session.getTransport();
        transport.connect(HOST, SEND_ADDRESS, AUTHORIZATION_CODE);

        Address[] addresses = {new InternetAddress(receiveAddress)};

        transport.sendMessage(msg, addresses);
        transport.close();
    }

    public static void registerMailValidate(String receiveAddress,String code) throws GeneralSecurityException, MessagingException {
        String title="微辅导注册邮件认证";
        //String body="你正在注册为微辅导用户:请点击连接地址完成注册:http://127.0.0.1:8080/register/mailPage?code="+code;
        String str=" <a href=\"http://127.0.0.1:8080/register/mailPage\">认证</a>";
        send(title, str, receiveAddress);
    }

    public static void main(String[] args) {
        try {
            registerMailValidate("851298348@qq.com","9999");
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
View Code

 

 

 

1.依赖jar包:

 

2.工具类使用说明:

 

3.授权码的获取:

 

 

 

 

service层:

dao层:

sql:

ok啦!,快去试一试吧!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!