用户注册功能快做完了,还差激活这个步骤。
以前听同学说有免费测试用的短信API,本身想搞个高大上的短信验证码的,前后端界面接口都搞了,网上逛了下聚合数据,百度云阿里云...各种云,看了看价格。劝退了,咱们还是踏实点,用邮箱绑定激活吧(qiong)。
(存入数据库,标记未激活,记下时间,验证码)
注册发邮件(账号不可用)---->用户点击邮件链接---->跳转到激活页面---->后台标记激活(账号可用)----->正常登陆
(验证时间(30分钟过期),邮箱、验证码核对)
为了防止骚扰,准备记录邮箱发送次数,以免产生过多垃圾邮件。
邮箱收发协议主要有POP3、IMAP、SMTP等
导入封装好javaMail的maven坐标:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
设置邮箱,获取打开smtp协议获取授权密码,比如QQ邮箱:
配置yml:
spring:
mail:
# 邮件服务器地址 我的是网易126邮箱
host: smtp.126.com
# 登录用户名
username: icu996@126.com
# 密码是邮箱提供商的授权码
password: icu996
# 编码格式
default-encoding: UTF-8
Service方法:使用 JavaMailSender 发送邮件。
package com.lyj.mall.service.impl;
import com.lyj.mall.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class MailserviceImpl implements MailService {
//获取yml配置文件的值
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
/**
* 简单文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param content 邮件内容
*/
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* HTML 文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param content HTML内容
* @throws MessagingException
*/
public void sendHtmlMail(String to, String subject, String content) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
helper.setFrom(from);
mailSender.send(message);
}
/**
* 附件邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param content HTML内容
* @param filePath 附件路径
* @throws MessagingException
*/
public void sendAttachmentsMail(String to, String subject, String content,
String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
helper.setFrom(from);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
mailSender.send(message);
}
/**
* 图片邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param content HTML内容
* @param rscPath 图片路径
* @param rscId 图片ID
* @throws MessagingException
*/
public void sendInlinkResourceMail(String to, String subject, String content,
String rscPath, String rscId) {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
helper.setFrom(from);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
页面获取路径参数,ajax提交验证,返回成功!
为了安全起见,邮箱和验证码应该加密混淆下。这里就不具体讲了。