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

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

搞定。





 

转载于:https://www.cnblogs.com/MagicAsa/p/9235025.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值