我们项目已经完成了手机验证码,但是还需要邮箱验证码,为此,
我们小组的张晨同学担负起了这个研究的重大任务。
拿出了几天研究了qq邮箱的验证码
张晨在腾讯邮箱注册了一个自己的账户,在qq邮箱里有一个功能选择开通,验证申请就行了。
然后在控制台执行以下代码即可。
// 1.创建连接对象javax.mail.Session
// 2.创建邮件对象 javax.mail.Message
// 3.发送一封激活邮件
String from = "17443934@qq.com";// 发件人电子邮箱
String host = "smtp.qq.com"; // 指定发送邮件的主机smtp.qq.com(QQ)|smtp.163.com(网易)
Properties properties = System.getProperties();// 获取系统属性
properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
properties.setProperty("mail.smtp.auth", "true");// 打开认证
try {
//QQ邮箱需要下面这段代码,163邮箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
// 1.获取默认session对象
Session session = Session.getInstance(properties, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("17443934@qq.com", "jxlewxxkrynucafe"); // 发件人邮箱账号、授权码
}
});
// 2.创建邮件对象
Message message = new MimeMessage(session);
// 2.1设置发件人
message.setFrom(new InternetAddress(from));
// 2.2设置接收人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
// 2.3设置邮件主题
message.setSubject("账号激活");
// 2.4设置邮件内容
String content = "<html><head></head><body><h1>您好!根据您提交找回密码请求,请点击下面的链接修改用户密码:</h1><h3><a href='http://localhost:8080/web/setPassword?code="
+ code +"&userEmail="+email+ "'>http://localhost:8080/web/setPassword?code=" + code+"&userEmail="+email
+ "</href></h3></body></html>";
message.setContent(content, "text/html;charset=UTF-8");
// 3.发送邮件
Transport.send(message);
System.out.println("邮件成功发送!");有需要的同学可以学习一下,很简单。

0条评论
点击登录参与评论