版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengtanyong4777/article/details/79035751
1.配置环境
1.1pom.xml中添加依赖
<!--javamail-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.3.2</version>
</dependency>
1.2实体类User
package com.gym.pojo;
import lombok.Data;
@Data
public class User {
private Integer userId;
private String userName;
private String userPassword;
private String userEmail;
private String userPhone;
private String userPhoto;
private Integer userState;
private String userCode;
}
1.3编写UUIDUtils随机生成激活码
package com.gym.util;
import java.util.UUID;
/**
* 生成随机字符串
*/
public class UUIDUtils {
public static String getUUID(){
return UUID.randomUUID().toString().replace("-","");
}
}
1.4编写MailUtils
package com.gym.util;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MailUtils {
/**
* 发送邮件的方法
* @param userEmail
* @param userCode
*/
public static void sendMail(String userEmail,String userCode) throws Exception {
//1.创建连接对象,连接到邮箱服务器
Properties properties = new Properties();
properties.put("mail.smtp.localhost", "163.com");
properties.put("mail.smtp.host","smtp.163.com");
properties.put("mail.smtp.auth","true");
MailAuthenticator mailAuthenticator = new MailAuthenticator("邮箱名【不要加邮箱后缀】","客户端授权密码");
Session session = Session.getInstance(properties,mailAuthenticator);
//开启session的调试模式,可以查看当前邮件发送状态
session.setDebug(true);
//2.创建邮件对象
Message message = new MimeMessage(session);
//2.1设置发件人
message.setFrom(new InternetAddress("发件人邮箱,需要后缀如[email protected]"));
//2.2设置收件人
message.setRecipient(Message.RecipientType.TO,new InternetAddress(userEmail));
//2.3设置邮件主题
message.setSubject("来自xxx的激活邮件");
//2.4设置邮件的征文
message.setContent("<h1>来自xxx的激活邮件,激活请点击以下链接:<h1><h3><a href='http://localhost:8080/user/activity.do?code="+userCode+"'>http://localhost:8080/user/activity.do?code="+userCode+"</a></h3>","text/html;charset=UTF-8");
//3.发送一封激活邮件
Transport.send(message);
}
}
class MailAuthenticator extends Authenticator {
/**
* 用户名
*/
private String username;
/**
* 密码(客户端授权密码)
*/
private String password;
/**
* 创建一个新的实例 MailAuthenticator.
*
* @param username
* @param password
*/
public MailAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
public String getPassword() {
return password;
} @
Override protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setUsername(String username) {
this.username = username;
}
}
1.5编写UserController.java
package com.gym.controller;
import com.gym.pojo.User;
import com.gym.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/user/")
public class UserController {
@Autowired
private UserService userSerivce;
@RequestMapping(value = "register.do",method = RequestMethod.POST)
@ResponseBody
private void register(User user) throws Exception {
userSerivce.register(user);
}
}
1.6编写UserService和UserServiceImpl
package com.gym.service;
import com.gym.pojo.User;
public interface UserService {
void register(User user) throws Exception;
}
package com.gym.service.Impl;
import com.gym.dao.UserMapper;
import com.gym.pojo.User;
import com.gym.service.UserService;
import com.gym.util.MailUtils;
import com.gym.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
/**
* 用户注册
* @param user
*/
public void register(User user) throws Exception {
//1.插入用户到数据库
user.setUserState(0);
user.setUserCode(UUIDUtils.getUUID()+ UUIDUtils.getUUID());
userMapper.insert(user);
//2.发送邮箱激活
MailUtils.sendMail(user.getUserEmail(),user.getUserCode());
}
}