SSH邮箱注册激活实现

首先jar包是mail.jar,百度找就ok了。
准备两个邮箱,建议阿里云邮箱接收,163邮箱发送,qq邮箱会出现接收很慢的情况。

效果图

在这里插入图片描述

工具类

MailUtils.java

package com.zsj.utils;

import java.util.Properties;

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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {

	//发邮箱,给谁发email,邮箱标题,以及email的内容
	//163邮箱发给阿里云邮箱
	public static void sendMail(String email, String title, String emailMsg)
			throws AddressException, MessagingException {
		

		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");
		props.setProperty("mail.host", "smtp.163.com");
		props.setProperty("mail.smtp.auth", "true");

		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				//账号 和 授权码
				return new PasswordAuthentication("17856007510", "OXJDIIMDAPUGXYCX");
			}
		};

		Session session = Session.getInstance(props, auth);

		Message message = new MimeMessage(session);
		//改
		message.setFrom(new InternetAddress("17856007510@163.com")); 

		message.setRecipient(RecipientType.TO, new InternetAddress(email)); 

		//设置标题
		message.setSubject(title);

		message.setContent(emailMsg, "text/html;charset=utf-8");

		

		Transport.send(message);
	}
}


工具类中的授权码,在163邮箱中设置可以找到
在这里插入图片描述
在这里插入图片描述
点击开启,按照提示就能得到授权码,如果已经是开启的,就关闭再开启。

代码

这里code是用户标识,把id和用户名传递相当于泄露,所以设置code码。
UserAction.java

public String register() throws Exception {
		
		//表单并没有把所有数据封装,这时需要我们手动去封装
		user.setState(0);
		user.setCode(UUID.randomUUID().toString());
		user.setIamge("0");
		user.setLevel(1);
		user.setCoin(1000);
		
		userService.addUser(user);
		
		//把id和用户名传递相当于泄露,所以设置code码
		MailUtils.sendMail(user.getEmail(), "用户激活", "恭喜你注册成功,请点击下面的链接进行激活吧!<a href='http://localhost:8080/SSH_Forum/UserAction_active?userCode="+user.getCode()+"'>点击这里</a>");
		
		return "toRegisterSuccess";
		
		
	}
	
	//用户邮箱激活
	public String active() throws Exception {
		//System.out.println("active");
		userService.activeUser(userCode);
		return "toLogin";
	}

UserService.java

//激活用户
	public void activeUser(String userCode) {
		userDao.activeUser(userCode);
	}

UserDao.java

public void activeUser(String userCode) {
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		String sql = "update user set state = 1 where code = ?";
		NativeQuery query = session.createSQLQuery(sql);
		query.setParameter(1, userCode);
		query.executeUpdate();
	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值