java实现发送qq邮箱验证码

1、登录qq邮箱,开启POP3/SMTP服务

路径:qq邮箱--->设置---->选择账户----> 打开

java实现发送qq邮箱验证码_第1张图片

按照要求发送短信后得到授权码

java实现发送qq邮箱验证码_第2张图片

得到授权码后要记得点击确定,页面拉到最下方点击保存设置。

2、pom.xml加入依赖


	    org.apache.commons
	    commons-email
	    1.5

 加入后会自动加载其他两个相关依赖

3、测试类(两种发送方式都测试成功了)

package com.ymo;

import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.Random;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.junit.Test;

import com.sun.mail.util.MailSSLSocketFactory;

public class EmalTest {
	String yzm;
	
	@Test
	public void send() {
		yzm = random1();
		try {
			 //HtmlEmail方式
			//sendEmail();
			//javax.mail方式
			sendMail("[email protected]", "XXX", "[email protected]", "测试", "

我的验证码:"+yzm+"

"); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (GeneralSecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 方式1:发送QQ邮件 */ public void sendEmail() { HtmlEmail send = new HtmlEmail();//创建一个HtmlEmail实例对象 // 获取随机验证码 String resultCode = yzm; try { send.setHostName("smtp.qq.com"); send.setAuthentication("[email protected]", "XXX"); //第一个参数是发送者的QQEamil邮箱 第二个参数是刚刚获取的授权码 send.setFrom("[email protected]", "orison有限公司");//发送人的邮箱为自己的,用户名可以随便填 记得是自己的邮箱不是qq // send.setSmtpPort(465); //端口号 可以不开 send.setSSLOnConnect(true); //开启SSL加密 send.setCharset("utf-8"); send.addTo("[email protected]"); //设置收件人 email为你要发送给谁的邮箱账户 send.setSubject("测试测试"); //邮箱标题 send.setMsg("HelloWorld!您的验证码: " + resultCode + " ,五分钟后失效"); //Eamil发送的内容 send.send(); //发送 } catch (EmailException e) { e.printStackTrace(); } } /**方式2:发送QQ邮件 * @param sender 发送方的邮箱 * @param auth qq邮箱中申请的16位授权码 * @param to 接收人邮箱 * @param title 邮件标题 * @param content 邮件内容 * */ public static void sendMail(String sender,String auth,String to,String title,String content) throws MessagingException, GeneralSecurityException, javax.mail.MessagingException { //创建一个配置文件并保存 Properties properties = new Properties(); properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.socketFactory.fallback", "false"); properties.setProperty("mail.smtp.port", "465"); properties.setProperty("mail.smtp.socketFactory.port", "465"); properties.setProperty("mail.host","smtp.qq.com"); properties.setProperty("mail.transport.protocol","smtp"); properties.setProperty("mail.smtp.auth","true"); //QQ存在一个特性设置SSL加密 MailSSLSocketFactory sf = new MailSSLSocketFactory(); sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf); //创建一个session对象 Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(sender,auth); } }); //开启debug模式 session.setDebug(true); //获取连接对象 Transport transport = session.getTransport(); //连接服务器 transport.connect("smtp.qq.com",sender,auth); //创建邮件对象 MimeMessage mimeMessage = new MimeMessage(session); //邮件发送人 mimeMessage.setFrom(new InternetAddress(sender)); //邮件接收人 mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to)); //邮件标题 mimeMessage.setSubject(title); //邮件内容 mimeMessage.setContent(content,"text/html;charset=UTF-8"); //发送邮件 transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients()); //关闭连接 transport.close(); } //生成6位数 验证码 public static String random1(){ String code = ""; Random rd=new Random(); for (int i = 0; i < 6; i++) { int r = rd.nextInt(10); //每次随机出一个数字(0-9) code = code + r; //把每次随机出的数字拼在一起 } System.out.println(code); return code; } }

你可能感兴趣的:(java,java,javax,email)