怎样通过验证码来修改密码:
首先说怎样实现验证码的发送:一共是三部:配置QQ邮箱,导入依赖jar包 ,代码撰写
1:QQ邮箱设置
1:第一步 ,打开QQ邮箱(地址:https://mail.qq.com/);
2:第二步 ,登录后点击设置
3:第三步,点击账户
4:第四步:开启这里的服务:
第五步:生成随机授权码:
第六步,开启成功后会获得一个授权码,记录下来(代码中需要使用,这个授权码并不是一定的 );
导入jar包:activation.jar、commons-email-1.x.jar、mail.jar
我这里直接百度云免费分享:
链接:https://pan.baidu.com/s/1t34_QYeTJMEkZr5kUfmx2A
提取码:x4oy
--来自百度网盘超级会员V3的分享
最后就是代码了:
package 验证码学习;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author ${范涛之}
* @Description
* @create 2021-12-20 17:35
*/
public class Test {
public String achieveCode() { //由于数字 1 、 0 和字母 O 、l 有时分不清楚,所以,没有数字 1 、 0
String[] beforeShuffle= new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a",
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z" };
List list = Arrays.asList(beforeShuffle);//将数组转换为集合
Collections.shuffle(list); //打乱集合顺序
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i)); //将集合转化为字符串
}
return sb.toString().substring(3, 8); //截取字符串第4到8
}
//发送邮件代码
public static void sendAuthCodeEmail(String email, String authCode) {
try {
SimpleEmail mail = new SimpleEmail();
mail.setHostName("smtp.qq.com");//发送邮件的服务器
mail.setAuthentication("你的邮箱","你的授权码");//刚刚记录的授权码,是开启SMTP的密码
mail.setFrom("你的邮箱","发件人的昵称"); //发送邮件的邮箱和发件人
mail.setSSLOnConnect(true); //使用安全链接
mail.addTo("收信人邮箱");//接收的邮箱
//System.out.println("email"+email);
mail.setSubject("注册验证码");//设置邮件的主题
mail.setMsg("尊敬的用户:你好!\n 注册验证码为:" + authCode+"\n"+" (有效期为一分钟)");//设置邮件的内容
mail.send();//发送
} catch (EmailException e) {
e.printStackTrace();
}
}
}
这里说一下自己在 mail.setSSLOnConnect(false); //使用安全链接
这个地方只要设置成false就会成功,true就会报错
主方法中调用执行:
public static void main(String[] args) {
sendAuthCodeEmail("2831826106@qq.com",achieveCode() );
}
结果:成功:
全部代码展示:
package 验证码学习;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* @author ${范涛之}
* @Description
* @create 2021-12-20 17:35
*/
public class Test {
public static String achieveCode() { //由于数字 1 、 0 和字母 O 、l 有时分不清楚,所以,没有数字 1 、 0
String[] beforeShuffle= new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
"G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a",
"b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z" };
List list = Arrays.asList(beforeShuffle);//将数组转换为集合
Collections.shuffle(list); //打乱集合顺序
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i)); //将集合转化为字符串
}
return sb.toString().substring(3, 8); //截取字符串第4到8
}
//发送邮件代码
public static void sendAuthCodeEmail(String email, String authCode) {
try {
SimpleEmail mail = new SimpleEmail();
mail.setHostName("smtp.qq.com");//发送邮件的服务器
mail.setAuthentication("*******@qq.com","这里写自己的哦");//刚刚记录的授权码,是开启SMTP的密码
mail.setFrom("*******@qq.com","ftz"); //发送邮件的邮箱和发件人
mail.setSSLOnConnect(false); //使用安全链接
mail.addTo("*******@qq.com");//接收的邮箱
System.out.println("email是: "+email);
mail.setSubject("注册验证码");//设置邮件的主题
mail.setMsg("尊敬的用户:你好!\n 注册验证码为:" + authCode+"\n"+" (有效期为一分钟)");//设置邮件的内容
mail.send();//发送
} catch (EmailException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
sendAuthCodeEmail("******@qq.com",achieveCode() );
}
}