zoukankan      html  css  js  c++  java
  • 邮箱验证,工具类

    邮箱验证,工具类:

    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啦!,快去试一试吧!

  • 相关阅读:
    springboot之异步调用@Async
    springboot之约定大约配置
    springboot之定时任务@Scheduled
    百度地图API
    JS触发服务器控件的单击事件
    jquery复选框 选中事件 及其判断是否被选中
    NopCommerce源码架构详解--初识高性能的开源商城系统cms
    基于dapper的通用泛型分页
    基于JQuery 的消息提示框效果代码
    kindeditor支持flv视频播放方法
  • 原文地址:https://www.cnblogs.com/dw3306/p/9314446.html
Copyright © 2011-2022 走看看