前端
效果图
页面代码
<div class="loginbox row">
<h3>用户注册</h3>
<form class="form-login" id="register" method="post">
<ul>
<li class="form-group has-success has-feedback">
<span class="glyphicon glyphicon-earphone form-control-feedback"></span>
<input type="text" class="form-control" placeholder="请输入手机号" name="telephone" id="telephone" required />
</li>
<li class="form-group has-success has-feedback">
<span class="glyphicon glyphicon-user form-control-feedback"></span>
<input type="text" class="form-control" placeholder="请输入用户名" name="username" id="username" required />
</li>
<li class="form-group has-success has-feedback">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
<input type="password" class="form-control" placeholder="请输入密码" id="password"name="password" required />
</li>
<li class="form-group has-success has-feedback">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
<input type="text" class="form-control" placeholder="请输入您的qq邮箱" name="mail" id="mail" required />
</li>
<li class="form-group has-success has-feedback">
<span class="glyphicon glyphicon-check form-control-feedback"></span>
<input type="text" class="form-control code" placeholder="请输入6位验证码" id="mailCode" name="mailCode" />
<a id="send" href="javascript:void(0)" class="btn btn-primary btn-sm"
style="width: 100px;float: right; margin-top: -30px; margin-right: 10px">
发送邮件
</a>
</li>
<li>
<span>已有账号?请点击 <a href="/sys/login/login.page">登录账号</a></span>
</li>
<li>
<button id="submit" type="submit" class="btn btn-primary btn-lg btn-block">注册</button>
</li>
</ul>
</form>
</div>
js代码
$(function () {
var mail; // 邮箱
var mailCode ;// 验证码
$('#send').click(function () {
$(this).html("已发送");
$(this).attr("class","btn btn-default btn-sm");
mail = $('#mail').val();
$.ajax({
url: "/sys/mail/sendMail.json",
type: "POST",
data: {
mail : mail
},
success : function (result) {
bindSubmitClick(result);
}
});
});
function bindSubmitClick(result) {
$("#submit").click(function () {
// telephone = $('#telephone').val();
// username = $('#username').val();
// password = $('#password').val();
// mail = $('#mail').val();
mailCode = $('#mailCode').val();
if (result == mailCode){
alert("注册成功,请登录!")
$('#register').attr("action", "/sys/login/register.json");
// $.ajax({
// url : "/sys/login/register.json",
// type: "POST",
// data:{
// telephone : telephone,
// username : username,
// password : password,
// mail : mail
// }
// });
}else {
alert("验证码错误,请重新注册");
}
});
}
});
后台
springboot引入maven依赖
<!--配置邮件发送-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置
#配置qq邮箱
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
#邮箱号
spring.mail.username=xxxxxx@qq.com
#授权码
spring.mail.password=totgn*****njecie
#邮件发送安全配置
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
其中授权码在qq邮箱中,开启后获得授权码
controller
package com.lzy.controller;
import com.lzy.service.MailService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.Random;
@Controller
@RequestMapping("/sys/mail")
public class MailController {
@Resource
private MailService mailService;
// 发送邮件
@ResponseBody
@RequestMapping("/sendMail.json")
public String send(@RequestParam("mail") String mail){
String mailCode = String.valueOf(new Random().nextInt(899999)+ 100000);
String message = "您的注册验证码为:" + mailCode;
try{
mailService.sendMail(mail,"注册验证码",message);
}catch (Exception e){
e.printStackTrace();
}
return mailCode;
}
}
service
package com.lzy.service.impl;
import com.lzy.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class MailServiceImpl implements MailService {
@Value("${spring.mail.username}")
private String from;
@Resource
private JavaMailSender mailSender;
@Override
public void sendMail(String mail, String title, String message) {
System.out.println("---------------------------------"+from);
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 发件人
mailMessage.setFrom(from);
// 收件人
mailMessage.setTo(mail);
// 邮件名
mailMessage.setSubject(title);
// 邮件内容
mailMessage.setText(message);
// 发送邮件
mailSender.send(mailMessage);
}
}