码迷,mamicode.com
首页 > 编程语言 > 详细

java实现注册邮箱激活验证(一)

时间:2018-03-15 14:22:26      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:nal   情况   htm   protect   邮箱   测试过程   .com   eem   logs   

一般情况注册账户激活时有发送六位数字激活码和发送激活地址两种常见的方式,今天来总结一下

一.发送六位数字激活码的方式,下一篇博客介绍发送激活地址的方式(用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 com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
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("176********@163.com", "111111111111");
            }
        });

        //创建邮件对象
        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", "aaaaaaaaaaaa");
            }
        });

        //创建邮件对象
        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;

import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;

public class Test {
    public static void main(String[] args) {


        try {
            //发送太多可能会被拦截到垃圾箱
            //用网易发邮件,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发邮件,网易邮箱收邮件
            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 (MessagingException e) {
            e.printStackTrace();
        }

    }
}

 

注意:测试过程中可能出现的问题

用网易邮箱发送时

技术分享图片

 

用qq邮箱发送时

技术分享图片

 

这两个异常的原因是服务没有启用POP3/SMTP服务,请参考我的上一篇博客开启POP3/SMTP服务

启用服务后,问题就会解决啦,祝你编码愉快!

 

java实现注册邮箱激活验证(一)

标签:nal   情况   htm   protect   邮箱   测试过程   .com   eem   logs   

原文地址:https://www.cnblogs.com/feifeicui/p/8573050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!