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.验证逻辑

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

    搞定。





  • 相关阅读:
    子信息传递多个参数
    Oracle q' 简化单引号转义
    HTML的target属性中_blank、_self、_parent、_top含义
    数据库设计之“有时不得不违背的第三范式”
    Jquery下的Ajax调试方法
    JQuery.Ajax之错误调试帮助信息
    定时删除数据-用JOB
    数据分析
    机器学习数学基础
    机器学习数学基础
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/9235025.html
Copyright © 2011-2022 走看看