java实现注册邮箱激活验证
一般情况注册账户激活时有发送六位数字激活码和发送激活地址两种常见的方式,今天来总结一下
一.发送六位数字激活码的方式(用qq邮箱或者163邮箱发邮件,注意前提是要拿到授权码滴,下文有说到)
1.新建一个maven项目,结构如下
2.pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>war</packaging> <name>mail</name> <groupId>com.cyf</groupId> <artifactId>mail</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.7</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8888</port> <maxIdleTime>30000</maxIdleTime> </connector> </connectors> <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory> <contextPath>/</contextPath> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4</version> </dependency> </dependencies> </project>
3.发送邮件MailUtil.java
package com; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; public class MailUtil { /** * 使用网易邮箱发送邮件 * * @param to 给谁发 * @param text 发送内容 */ public static void send_mail(String to, String text) throws MessagingException { //创建连接对象 连接到邮件服务器 Properties properties = new Properties(); //设置发送邮件的基本参数 //发送邮件服务器 properties.put("mail.smtp.host", "smtp.163.com"); //发送端口 properties.put("mail.smtp.port", "25"); properties.put("mail.smtp.auth", "true"); //设置发送邮件的账号和密码 Session session = Session.getInstance(properties, new Authenticator() { @Override protected javax.mail.PasswordAuthentication getPasswordAuthentication() { // return new javax.mail.PasswordAuthentication("你的网易邮箱", "你的网易邮箱授权码"); return new javax.mail.PasswordAuthentication("17621831595@163.com", "cuiyanfei211"); } }); //创建邮件对象 Message message = new MimeMessage(session); //设置发件人 try { message.setFrom(new InternetAddress("17621831595@163.com")); //设置收件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //设置主题 message.setSubject("这是一份测试邮件"); //设置邮件正文 第二个参数是邮件发送的类型 message.setContent(text, "text/html;charset=UTF-8"); //发送一封邮件 Transport.send(message); } catch (javax.mail.MessagingException e) { e.printStackTrace(); } } /** * 使用qq邮箱发送邮件 * * @param to 给谁发 * @param text 发送内容 */ public static void send_mail2(String to, String text) throws MessagingException { //创建连接对象 连接到邮件服务器 Properties properties = new Properties(); //设置发送邮件的基本参数 //发送邮件服务器 properties.put("mail.smtp.host", "smtp.qq.com"); //发送端口 properties.put("mail.smtp.port", "587"); properties.put("mail.smtp.auth", "true"); //设置发送邮件的账号和密码 Session session = Session.getInstance(properties, new Authenticator() { @Override protected javax.mail.PasswordAuthentication getPasswordAuthentication() { // return new javax.mail.PasswordAuthentication("你的QQ邮箱", "你的qq邮箱授权码"); return new javax.mail.PasswordAuthentication("1741258558@qq.com", "sxmvraycsmkaddag"); } }); //创建邮件对象 Message message = new MimeMessage(session); //设置发件人 try { message.setFrom(new InternetAddress("1741258558@qq.com")); //设置收件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); //设置主题 message.setSubject("这是一份测试邮件"); //设置邮件正文 第二个参数是邮件发送的类型 message.setContent(text, "text/html;charset=UTF-8"); //发送一封邮件 Transport.send(message); } catch (javax.mail.MessagingException e) { e.printStackTrace(); } } }
4.测试类
package com; public class Test { public static void main(String[] args) { //发送太多可能会被拦截到垃圾箱 //用网易发邮件,qq邮箱收邮件 // MailUtil.send_mail("1741258558@qq.com", String.valueOf("激活码啊b:"+(int)((Math.random()*9+1)*100000))); // MailUtil.send_mail2("1741258558@qq.com", String.valueOf("激活码啊b:"+(int)((Math.random()*9+1)*100000))); //用qq发邮件,网易邮箱收邮件 try { MailUtil.send_mail("17621831595@163.com", String.valueOf("激活码啊a:" + (int) ((Math.random() * 9 + 1) * 100000))); MailUtil.send_mail2("17621831595@163.com", String.valueOf("激活码啊a:" + (int) ((Math.random() * 9 + 1) * 100000))); System.out.println("发送邮件成功"); } catch (javax.mail.MessagingException e) { e.printStackTrace(); } } }
二.介绍发送激活地址的方式
package com; public class Test2 { public static void main(String[] args) { try { //发送太多可能会被拦截到垃圾箱 MailUtil.send_mail("1741258558@qq.com", "<a href='http://localhost:8086/goods/RegistServlet.do?flag=3&loginname=abcdef'>点此激活</a>"); System.out.println("发送邮件成功"); } catch (javax.mail.MessagingException e) { e.printStackTrace(); } } }
注意:测试过程中可能出现的问题
用网易邮箱发送时
用qq邮箱发送时
这两个异常的原因是服务没有启用POP3/SMTP服务,请参考我的上一篇博客开启POP3/SMTP服务,
愿你遍历山河
仍觉人间值得