zoukankan      html  css  js  c++  java
  • idea maven环境下 java实现发送邮件验证

    1.开通smtp授权

    QQ邮箱-设置-账户-开启

    得到一个授权码

    2.下载javax.email包

    maven项目中

    pom文件加入:

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.5</version>
    </dependency>
    

    3.方法:

    import java.util.Properties;
    import javax.mail.Address;
    import javax.mail.Message;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    
    
    
    
                   //email 你要发给谁      //authcode  验证码
    public static void email(String email,Integer authcode)throws Exception {
    		Properties properties = new Properties();
    		properties.setProperty("mail.transport.protocol", "smtp");//发送邮件协议
    		properties.setProperty("mail.smtp.auth", "true");//需要验证
    		 //properties.setProperty("mail.debug", "true");//设置debug模式 后台输出邮件发送的过程
    		Session session = Session.getInstance(properties);
    		session.setDebug(true);//debug模式
    		//邮件信息
    		Message messgae = new MimeMessage(session);
    		messgae.setFrom(new InternetAddress("测试@sina.com"));//设置发送人
    		messgae.setText("你的验证码为:"+authcode+"。请注意,验证码有效时间为2分钟!!!");//设置邮件内容
    		messgae.setSubject("邮箱验证");//设置邮件主题
    		//发送邮件
    		Transport tran = session.getTransport();
    		 tran.connect("smtp.sina.com", 25, "邮箱账户", "邮箱授权码");//连接到新浪邮箱服务器
    		// tran.connect("smtp.qq.com",587, "Michael8@qq.vip.com", "xxxx");//连接到QQ邮箱服务器
    		tran.sendMessage(messgae, new Address[]{ new InternetAddress(email)});//设置邮件接收人
    		tran.close();
    	}
    

     4.验证逻辑

    点击发送时生成一串随机数字,存一份发一份,验证即可。

    搞定。





  • 相关阅读:
    关于gitlab怎样merge request的流程
    有访问权限的gitlab如何把上面的代码clone到本地
    macpro终端打开mysql
    Hbase实验:java创建和删除table
    齐次递推式拆数学式
    一些生成函数
    圆锥表面曲线方程
    扩展欧拉降幂
    scanf读入有空格字符串
    线性筛素数的一个用途
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/9235025.html
Copyright © 2011-2022 走看看