链接: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() ); } }