JAVA实现使用QQ邮箱发送验证码功能

JAVA实现使用QQ邮箱发送验证码功能

QQ邮箱设置

  1. 第一步 ,打开QQ邮箱(地址:https://mail.qq.com/);

  2. 第二步 ,登录后点击设置
    设置位置

  3. 第三步,点击账户
    在这里插入图片描述

  4. 第四步,下拉找到POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务
    打开Pop3/SMPT服务 ,获取邮箱授权码(需要手机验证)
    在这里插入图片描述

  5. 第五步,开启成功后会获得一个授权码,记录下来(代码中需要使用,这个授权码并不是一定的 );
    在这里插入图片描述

导入依赖jar包

分别是:activation.jar、commons-email-1.x.jar、mail.jar
楼主已经整合好了,地址:https://download.csdn.net/download/qq_43166448/10743081

写代码

生成随机验证码

// 随机验证码
		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();
   		}  
   	}

谢谢观看还请多多点赞
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
首先需要引入JavaMail API和Java Activation Framework (JAF)的包。 以下是一个简单的Java代码示例,用于从QQ邮箱发送验证码邮件: ```java import java.util.Properties; import javax.mail.*; import javax.mail.internet.*; public class SendEmail { public static void main(String[] args) { final String username = "your_qq_email@qq.com"; final String password = "your_email_password"; Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.qq.com"); props.put("mail.smtp.port", "587"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("your_qq_email@qq.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient_email@example.com")); message.setSubject("Verification Code"); int code = 123456; // replace with actual code message.setText("Your verification code is: " + code); Transport.send(message); System.out.println("Code sent successfully!"); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 在上面的代码中: - 将`your_qq_email@qq.com`替换为您的QQ邮箱地址。 - 将`your_email_password`替换为您的QQ邮箱密码。 - 将`recipient_email@example.com`替换为收件人的电子邮件地址。 - 将`123456`替换为实际的验证码。 运行代码后,您应该能够在收件人的邮箱中收到一封包含验证码的电子邮件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值