ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

电子邮箱激活及应用

2020-12-05 12:01:07  阅读:272  来源: 互联网

标签:code return String 邮箱 user 应用 mail 激活 电子邮箱


电子邮箱的激活

1.导入jar包(mail.jar)
在这里插入图片描述
2.进入qq邮箱或者其他类型邮箱进入如下设置开启,发送短信校验,获得第一行的授权码粘下来保存好(下一步要用)
在这里插入图片描述
3.邮箱工具类

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

public final class MailUtils {
	//设置自己的邮箱号码(修改下方qq邮箱即可)
	private static final String USER = "123@qq.com";
	//这个密码就是上一步让记下来的
	private static final String PASSWORD = "zzyzaiivljscbbjg";

	public static boolean sendMail(String to, String text, String title) {
		try {
			final Properties props = new Properties();
			props.put("mail.smtp.auth", "true");
			// 发送协议qq(也可以设置其他类型)
			props.put("mail.smtp.host", "smtp.qq.com");

			// 发件人的账号
			props.put("mail.user", USER);
			// 发件人的密码
			props.put("mail.password", PASSWORD);

			//用于进行SMTP进行身份验证
			Authenticator authenticator = new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					// 用户名、密码
					String userName = props.getProperty("mail.user");
					String password = props.getProperty("mail.password");
					return new PasswordAuthentication(userName, password);
				}
			};
	
			Session mailSession = Session.getInstance(props, authenticator);
			//创建邮件消息
			MimeMessage message = new MimeMessage(mailSession);
			//设置发件人
			String username = props.getProperty("mail.user");
			InternetAddress form = new InternetAddress(username); // 发件人邮箱
			message.setFrom(form);
			// 设置收件人
			InternetAddress toAddress = new InternetAddress(to);
			message.setRecipient(Message.RecipientType.TO, toAddress);

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

			//设置邮件的内容体
			message.setContent(text, "text/html;charset=UTF-8");
			// 发送邮件
			Transport.send(message);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	// 测试
	public static void main(String[] args) {
		MailUtils.sendMail("电子邮箱", "正文", "标题");
		// 测试的为收件人qq邮箱自定义
		MailUtils.sendMail("123@qq.com", "你好,这是一封测试邮件,无需回复", "测试");
		//提示:已发送
		System.out.println("已发送");
	}
}

4.应用(部分注册)

DaoImpl层
//添加用户
	@Override
	public int add(User user) {
		try {
			String sql = "insert into user(uid,username,password,name,email,birthday,sex,state,code) values(?,?,?,?,?,?,?,?,?)" ;
			//更新操作
			int count = qr.update(sql, user.getUid(),
					user.getUsername(),
					user.getPassword(),
					user.getName(),
					user.getEmail(),
					user.getBirthday(),
					user.getSex(),
					user.getState(),
					user.getCode()) ;
			return count ;
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}	
	}	
//通过激活码查询用户(激活码是随机生成的)
	@Override
	public User selectUserByCode(String code) {
		//sql
		try {
			String sql = "select * from user where code = ?" ;
			User user = qr.query(sql, new BeanHandler<User>(User.class), code) ;
			return user ;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
//更新用户
@Override
	public void updateUser(User user) {
		try {
			String sql = "update user set state=? where uid = ?";
			qr.update(sql, user.getState(),user.getUid());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
ServiceImpl层
//执行注册服务
	@Override
	public boolean register(User user) {	
		if(ud.addUser(user)>0) {
			//注册的同时需要给注册的邮箱发送一个超链接
			String content = "<a href='http://localhost:8080/nov_27/user?method=active&code="+user.getCode()+"'>这是一封激活邮件,请您点击激活邮箱</a>";
			//发邮箱
			MailUtils.sendMail(user.getEmail(), content,"用户邮箱激活");		
			return true;
			}
		return false;
	}
	//注册成功之后,检测code是否有对应的用户
	@Override
	public User activerUser(String code) {
		User user = ud.finUserByCode(code);
		if(user==null) {
			return null;
		}
		user.setState(1);
		ud.updateUser(user);	
		return user;
	}

}
Servlet层
//用户激活邮件的业务
	public String active(HttpServletRequest request, HttpServletResponse response) {

		String code = request.getParameter("code");

		User user = us.activerUser(code);
		if (user != null) {
			//若满足条件的用户存在,则提示信息页面显示,邮箱激活成功,并可点击请您登陆跳转到登录页面
			request.setAttribute("msg", "邮件激活成功,<a href='http://localhost:8080/nov_27/user?method=loginUI'>请您登录</a>");
		} else {
			//若不存在,则提示邮箱激活失败
			request.setAttribute("msg", "您的邮件激活失败");
		}
		return "/jsp/msg.jsp";
	}

标签:code,return,String,邮箱,user,应用,mail,激活,电子邮箱
来源: https://blog.csdn.net/a17151314/article/details/110678467

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有