实现邮箱注册账号并验证邮箱

两种实现思路:

  1.注册完成后 该账号状态未激活,点击邮箱链接更改数据库状态激活账号。

  2.填写完邮箱,设定激活链接失效时间,激活后可提交该表单页,完成注册。

两种方法大同小异 以1为例:

采用163邮箱

package com.email.test;


import org.springframework.stereotype.Service;

@Service
public class RegisterValidateService {

    /**
     * 发送邮箱验证
     */
    public void processregister(String email) {

        ///邮件的内容
        StringBuffer sb = new StringBuffer("点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!
"); String url = "smtp" //当前链接使用的协议 + "://" + "smtp.163.com"//服务器地址 + ":" + "8080/" //端口号 + "dahai"; //应用名称,如果应用名称为 sb.append("http://localhost:8088/email/user/register?action=activate&email=");//调用验证接口的页面 sb.append(email); sb.append("&validateCode="); sb.append("123456");//自己生成需要的密文 sb.append(""); //发送邮件 new SendEmail().send(email, sb.toString()); System.out.println("发送邮件"); } /** * 处理激活具体方法省略。。。 * @param email 邮箱 * @param validateCode 验证码 */ public void processActivate(String email, String validateCode) { //通过email获取用户信息 //检查链接是否过期 //验证验证码是否正确,正确状态更改为1 } }
package com.email.test;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class SendEmail {

    public static final String HOST = "smtp.163.com";
    public static final String FROM = "[email protected]";//发件人的email
    public static final String PWD = "xxxxx";//发件人密码

    /**
     * 获取Session
     * @return
     */
    private static Session getSession() {
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);//设置服务器地址
        //props.put("mail.store.protocol" , PROTOCOL);//设置协议
        //props.put("mail.smtp.port", PORT);//设置端口
        props.put("mail.smtp.auth" , "true");

        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM, PWD);
            }
        };
        Session session = Session.getDefaultInstance(props , authenticator);

        return session;
    }

    public void send(String toEmail , String content) {
        Session session = getSession();
        try {
            System.out.println("--send--"+content);
            // Instantiate a message
            Message msg = new MimeMessage(session);

            //Set message attributes
            msg.setFrom(new InternetAddress(FROM));
            InternetAddress[] address = {new InternetAddress(toEmail)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("账号激活邮件");
            msg.setSentDate(new Date());
            msg.setContent(content , "text/html;charset=utf-8");
            Transport.send(msg);
        }
        catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

    /**
     * 测试入口
     * @param args
     */
    public static void main(String[] args) {
        RegisterValidateService register = new RegisterValidateService();
        register.processregister("[email protected]");//接收邮件的邮箱
    }
}

配置完邮件,粘来即用,是不是很简单!

你可能感兴趣的:(项目实用方法,java,邮箱注册,邮箱激活)