邮箱注册

article/2025/9/19 23:34:38

邮箱注册

  • 流程图

javaMail简介

  • Sun定义的一套接收、发送电子邮件的API
    • 支持各种邮件协议,开发人员无需考虑底层通信细节
    • 被作为Java EE的一部分,但没有被加入标准JDK中
  • 需要获取jar包
<dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version>
</dependency>
  • 常用API
    • Message:创建和解析邮件内容的核心API
    • Transport:发送邮件的API
  • 使用步骤
    1. 使用Properties对象封装连接所需的信息
    2. 获取Session对象
    3. 封装Message对象
    4. 使用Transport发送邮件
    5. 关闭连接
  • 通过原生javaMail发送邮件示例
public class SendEmail
{public static void main(String [] args){  // 收件人电子邮箱String to = "abcd@gmail.com";// 发件人电子邮箱String from = "web@gmail.com";// 指定发送邮件的主机为 localhostString host = "localhost";// 获取系统属性Properties properties = System.getProperties();// 设置邮件服务器properties.setProperty("mail.smtp.host", host);// 获取默认session对象Session session = Session.getDefaultInstance(properties);try{// 创建默认的 MimeMessage 对象MimeMessage message = new MimeMessage(session);// Set From: 头部头字段message.setFrom(new InternetAddress(from));// Set To: 头部头字段message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));// Set Subject: 头部头字段message.setSubject("This is the Subject Line!");// 设置消息体message.setText("This is actual message");// 发送消息Transport.send(message);System.out.println("Sent message successfully....");Transport.close();}catch (MessagingException mex) {mex.printStackTrace();}}
}

Spring Mail API

  • 对于JavaMail中邮件发送的相关功能,Spring提供了一个抽象层,简化了操作
  • 常用API
    • MailMessage:允许用户快速设置邮件内容的各种属性信息
    • MailSender:提供了发送简单邮件的策略
  • 使用步骤
    1. 导入所需jar文件
    2. 使用SimpleMailMessage实现简单的邮件消息
    3. 在Spring中配置JavaMailSenderImpl用以发送邮件

详细步骤

  • 邮箱注册
    1. dao完成用户表的增删改查
    2. userService中编写createByMail方法:添加用户,生成激活码,发送邮件,激活码存入redis
    3. controller:邮箱验证(验证邮箱格式合法性),调用createByMail
  • 邮箱验证
    1. dao完成用户激活状态的更新
    2. userService中编写activate方法:验证激活码,更新用户
    3. controller:调用activate
  • userService中添加方法

void itriptxCreateByMail(ItripUser user) throws Exception;
  • 编写mailService

void sendActivationMail(String mailTo, String activationCode);
  • 实现itriptxCreateByMail方法

@Override
public void itriptxCreateByMail(ItripUser user) throws Exception {// 添加用户信息itripUserMapper.insertItripUser(user);// 生成激活码String activationCode = MD5.getMd5(user.getUserCode(), 32);// 发送邮件mailService.sendActivationMail(user.getUserCode(), activationCode);// 激活码存入redisredisAPI.set("activation:" + user.getUserCode(), activationCode, 30 * 60);
}
  • 实现sendActivationMail方法,发送用户邮箱激活码

@Service
public class MailServiceImpl implements MailService {@Autowiredprivate MailSender mailSender;@Autowiredprivate SimpleMailMessage mailMessage;/*** 发送注册激活邮件*/public void sendActivationMail(String mailTo, String activationCode) {mailMessage.setTo(mailTo);mailMessage.setText("注册邮箱:" + mailTo + "  激活码:" + activationCode);mailSender.send(mailMessage);}}
  • 编写applicationContext-mail.xml配置mail相关内容,通过实例化这个bean进行注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"><property name="defaultEncoding" value="utf-8"/><property name="host" value="smtp.qq.com"/><property name="port" value="25"/><property name="username" value="***"/><property name="password" value="***"/></bean><bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"><property name="subject" value="请激活您的账户"/><property name="from" value="发送的邮箱号,必须到响应的邮箱开启服务"/></bean>
</beans>
  • 在applicationContext-mybatis.xml中导入mail的配置文件

<!--导入邮件配置-->
<import resource="applicationContext-mail.xml"/>
  • 测试

    • 注意,user表中的username为必填,所以需要设置

    • 邮件发送方必须开启第三方邮件服务

  • userService中编写activateByMail方法

boolean activateByMail(String email, String code) throws Exception;
  • 实现activateByMail方法,判断邮箱以及激活码是否正确,正确则修改用户的状态为已经激活

@Override
public boolean activateByMail(String email, String code) throws Exception {// 验证激活码String key = "activation:" + email;if (redisAPI.exists(key)) {if (redisAPI.get(key).equals(code)) {ItripUser itripUser = findByUsername(email);if (EmptyUtils.isNotEmpty(itripUser)) {itripUser.setActivated(1);//激活用户itripUser.setUserType(0);//自注册用户itripUser.setFlatID(itripUser.getId());itripUserMapper.updateItripUser(itripUser);return true;}}}return false;
}
  • 编写userController

  • 先判断邮箱规范以及用户是否存在,成功才存进数据库,但是状态为未激活,通过接口发送激活码,并且存进Redis中

@Controller
@RequestMapping(value = "api")
public class UserController {@Autowiredprivate UserService userService;@RequestMapping(value = "/registerByMail", method = RequestMethod.POST, produces = "application/json")public @ResponseBodyDto registerByMail(@RequestBody ItripUserVO userVO) {if (!validEmail(userVO.getUserCode()))return DtoUtil.returnFail("请使用正确的邮箱地址注册", ErrorCode.AUTH_ILLEGAL_USERCODE);try {if (null == userService.findByUsername(userVO.getUserCode())) {ItripUser user = new ItripUser();user.setUserCode(userVO.getUserCode());user.setUserName(userVO.getUserName());user.setUserType(0);user.setUserPassword(MD5.getMd5(user.getUserPassword(), 32));userService.itriptxCreateByMail(user);return DtoUtil.returnSuccess();} else {return DtoUtil.returnFail("用户已存在,注册失败", ErrorCode.AUTH_USER_ALREADY_EXISTS);}} catch (Exception e) {e.printStackTrace();return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);}}


编写IrtripUserVo

  判断用户邮箱以及激活码是否正确,并且返回结果给用户

  @RequestMapping(value = "/activateByMail", method = RequestMethod.PUT, produces = "application/json")@ResponseBodypublic Dto activateByMail(@RequestParam String email, @RequestParam String code) {try {if (userService.activateByMail(email, code)) {return DtoUtil.returnSuccess("激活成功");} else {return DtoUtil.returnSuccess("激活失败");}} catch (Exception e) {e.printStackTrace();return DtoUtil.returnFail("激活失败", ErrorCode.AUTH_ACTIVATE_FAILED);}}


下面这部分可在前台页面用Jquery验证


    /**
     * 合法E-mail地址:
     * 1. 必须包含一个并且只有一个符号“@
     * 2. 第一个字符不得是“@”或者“.
     * 3. 不允许出现“@.”或者.@
     * 4. 结尾不得是字符“@”或者“.
     * 5. 允许“@”前的字符中出现“+”
     * 6. 不允许“+”在最前面,或者“+@”
     */

    private boolean validEmail(String email) {String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";return Pattern.compile(regex).matcher(email).find();}

 


http://chatgpt.dhexx.cn/article/3q5rUtQj.shtml

相关文章

【Android工具】神器来了,游戏安装加速器ourplay,重点:附赠强大免费gmail邮箱注册...

今年的“会员”快到期&#xff0c;然后就需要新的邮箱&#xff0c;恩&#xff0c;我承认我用的方法比较LOW&#xff0c;但是从简单粗暴的角度&#xff0c;我还是选择这个方法。那么问题就来了&#xff1a;手机收不到验证码怎么破&#xff1f; 今天推荐一个神器&#xff1a;ourp…

双系统下Ubuntu20.04使用Pavucontrol无法连接pulseaudio解决办法

网上有很多解决Ubuntu没有声音的办法&#xff0c;其中有一个是下载 pavucontrol &#xff0c;但是在我们在终端输入&#xff1a; pavucontrol 弹出来的音量控制显示的是&#xff1a; Establishing connection to PulseAudio. Please wait...我抓马的在网上百度了三个小时&…

【Nacos】Nacos MySQL 配置 启动报错 ould not create connection to database server. Attempted reconnect 3 time

文章目录 1.概述2. 解决方案1-未解决3. 解决方案2-未解决4. 解决方案3-未解决5. 解决方案4-未解决6. 解决方案6-未解决7. 解决方案7-解决1.概述 在章节:【SpringCloud】Spring cloud Alibaba Nacos 集群和持久化配置 中设置MySQL,但是没有启动成功。配置如下 [lcc@lcc ~/so…

用VC6.0实现上位机串口通信

串口是常用的计算机与外部串行设备之间的数据传输通道&#xff0c;由于串行通信方便易行&#xff0c;所以应用广泛。我们可以利用Windows API 提供的通信函数编写出高可移植性的串行通信程序。本实例介绍在Visual C6.0下如何利用Win32 API 实现串行通信程序。程序编译运行后的界…

数据库连接异常: HikariPool-1 - Connection is not available, request timed out after 30000ms.

记一次生产环境数据库连接数导致的报错问题:Failed to obtain JDBC Connection; nested exception is java.sql.SQLTransientConnectionException: HikariPool-1 - Connection is not available, request timed out after 30000ms. 1. 复现&#xff0c;定时任务失败会有错误邮件…

Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Nacos集群启动一直报错 查看Nacos启动日志 Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.【解决办法】 1、在Nacos目录下创建plugins/mysql…

解决Hbase连接hdfs失败java.net.ConnectException: Connection refused

昨天hbase安装好之后一直连接不到hdfs上&#xff0c;十分费解。 错误如下&#xff1a; 2021-04-15 07:04:32,844 WARN [master:16000.activeMasterManager] ipc.Client: Failed to connect to server: master/192.168.110.129:9000: try once and fail. java.net.ConnectExc…

hbase1.2.1配置kerberos

今天需要在hbase上配置kerberos认证&#xff0c;所以需要安装kerberos&#xff0c;安装配置过程如下&#xff1a; kerberos简介 kerberos简单来说就是一套完全控制机制&#xff0c;它有一个中心服务器&#xff08;KDC&#xff09;&#xff0c;KDC中有数据库&#xff0c;你可以往…

setup maven plugin connection

setup maven plugin connection discover and map eclipse plugins to maven plugin goal executions 今天在创建maven工程时遇到了一个问题,工程在创建后有如图的报错,而且工程在创建后的也有很大的不同,丢失了很多的文件,问了下度娘居然没有相关的解决方法,所以在自己解决后…

Android Q Data Setup for Short Connection

直接上流程图 可参考之前的博客&#xff1a;Android N Data Setup & Disconnect For Short Connection 转载请注明出处。

Android Q Data Setup For Long Connection

直接上流程图 可参考之前的博客&#xff1a;Android N Data Setup For Long Connection 转载请注明出处。

hadoop集群安装配置Kerberos(三):hadoop集群配置 kerberos 认证

目录 前言 一、配置 SASL 认证证书 二、修改集群配置文件 1.hdfs添加以下配置 2.yarn添加以下配置 3.hive添加以下配置 4.hbase添加以下配置 三、kerberos相关命令 四、快速测试 五、问题解决 1、Caused by: java.io.IOException: Failed on local exception: java.…

IDEA连接kerberos环境的HDFS集群报错整理

连接hdfs代码 public class HdfsTest {public static void main(String[] args) throws IOException {System.setProperty("java.security.krb5.conf", "hdfs-conf-kerberos\\krb5.conf");Configuration conf new Configuration();conf.set("fs.def…

Failed to create/setup connection: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。

情况描述&#xff1a;用hutool的DB工具测试连接SQL server数据库。结果返回异常。 尝试网上的一些方法&#xff0c;但均未成功。 如&#xff1a; 1、更换JDK&#xff0c;这是不可能的。现在用的是1.8 2、jre/lib/ext目录增加bcprov-ext-jdk15on-1.54.jar和bcprov-jdk15on-1…

当刷机工具遇到SetupConnection时的解决方法

当刷机工具遇到SetupConnection时&#xff0c;解决方法 首先&#xff0c;在此贴上原文地址&#xff0c;已表感激之情。 http://blog.sina.com.cn/s/blog_636fd7d90101drak.html http://bbs.gfan.com/android-4032375-1-1.html 据说程序员是比较挑剔的&#xff0c;骨子里难…

电子元件二极管封装SMA,SMB,SMC的区别

某个电路使用二极管&#xff08;典型的如肖特基二极管SS14 SS24 SS34&#xff09;&#xff0c;发现居然有三个规格&#xff0c;SMA, SMB, SMC&#xff0c; 找了一下其区别&#xff0c;记录如下&#xff0c; 从下面图片的来看&#xff0c;可以看出主要体积上不同&#xff1a;S…

Windows Server之浅谈SMB以及SMB小案例分享

Windows Server之浅谈SMB以及SMB小案例分享 gs_h关注4人评论89230人阅读2017-01-23 14:45:45 SMB由来 服务器消息区块&#xff08;英语&#xff1a;Server Message Block&#xff0c;缩写为SMB&#xff0c;服务器消息区块&#xff09;&#xff0c;又称网络文件共享系统&#x…

linux——SMB文件共享及应用实例

SMB文件共享 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件&#xff0c;由服务器及客户端程序构成。SMB(Server Messages Block&#xff0c;信息服务块)是一种在局域网上共享文件和打印机的一种通信协议&#xff0c;它为局域网内的不同计算机之间提供文件及打印机等资源…

Linux-smb服务器搭建

Linux-smb服务器搭建 wget安装 rpm源获取地址&#xff1a;https://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/ 阿里云Yum源配置 1.可以移除默认的yum仓库&#xff0c;也就是删除 /etc/yum.repos.d/底下所有的.repo文件&#xff08;踢出国外的yum源&#xff09; 2…

Windows搭建SMB服务

Windows搭建SMB服务 本文介绍在windows本地环境上搭建SMB服务实现文件共享 配置服务 在本地机上以Windows10举例 &#xff1a;在控制面板 -->程序–>程序和功能–>启用或关闭Windows功能–>SMB 1.0/cifs file sharing support勾选SMB 1.0/CIFS Client和SMB 1.0/CI…