java邮箱实现验证 工具类_邮箱验证,工具类

packagecom.microClass.util;importcom.sun.mail.util.MailSSLSocketFactory;import javax.mail.*;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjava.security.GeneralSecurityException;importjava.util.Properties;/*** Created by ause on 2017-08-25.*/

public classMailUtil {//public final static int ERA = 0;//设置邮件服务器主机名

public final static String HOST = "smtp.qq.com";//发送邮件协议名称

public final static String PROTOCOL = "smtp";//发送人的邮箱地址

public final static String SEND_ADDRESS = "1593139675@qq.com";//授权码Authorization code//ndtqsiupcmxcbabf

public final static String AUTHORIZATION_CODE = "ndtqsiupcmxcbabf";public static void send(String title, String body, String receiveAddress) throwsMessagingException, GeneralSecurityException {

Properties props= newProperties();//开启debug调试

props.setProperty("mail.debug", "true");//发送服务器需要身份验证

props.setProperty("mail.smtp.auth", "true");

props.setProperty("mail.host", HOST);

props.setProperty("mail.transport.protocol", PROTOCOL);

MailSSLSocketFactory sf= newMailSSLSocketFactory();

sf.setTrustAllHosts(true);

props.put("mail.smtp.ssl.enable", "true");

props.put("mail.smtp.ssl.socketFactory", sf);

Session session=Session.getInstance(props);

Message msg= newMimeMessage(session);

msg.setSubject(title);

msg.setText(body);

msg.setFrom(newInternetAddress(SEND_ADDRESS));

Transport transport=session.getTransport();

transport.connect(HOST, SEND_ADDRESS, AUTHORIZATION_CODE);

Address[] addresses= {newInternetAddress(receiveAddress)};

transport.sendMessage(msg, addresses);

transport.close();

}public static void registerMailValidate(String receiveAddress,String code) throwsGeneralSecurityException, MessagingException {

String title="微辅导注册邮件认证";//String body="你正在注册为微辅导用户:请点击连接地址完成注册:http://127.0.0.1:8080/register/mailPage?code="+code;

String str=" 认证";

send(title, str, receiveAddress);

}public static voidmain(String[] args) {try{

registerMailValidate("851298348@qq.com","9999");

}catch(GeneralSecurityException e) {

e.printStackTrace();

}catch(MessagingException e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值