首先需要导入三个必需的jar包:
1.mail.jar
2.activation-1.1.1.jar
3.javax.mail-1.6.0.jar
第二。将三个jar包构建路径: 点击 Add to Aspectpath,三个jar包都需要。
Controller代码:`
import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender;//这两个确保导入成功,未报异常 @Controller public class SendEmailController { @Autowired private JavaMailSender javaMailSender; @RequestMapping("send") public @ResponseBody void senMsg(HttpSession httpSession, @RequestParam String email){ //生成六位数验证码 String Captcha = String.valueOf(new Random().nextInt(899999) + 100000); httpSession.setAttribute("Captcha",Captcha); SimpleMailMessage message = new SimpleMailMessage(); //发件人的邮箱地址 message.setFrom("XXXXXX@qq.com"); //收件人的邮箱地址 message.setTo(email); //邮件主题 message.setSubject("邮件的主题"); //邮件内容 message.setText("欢迎您注册地软平台:"+Captcha+"若想要了解相关消息,请访问官网:xxxxxxxxx"); //发送邮件 javaMailSender.send(message); }
注册-Controller:
@RequestMapping("Register-QAZXFSDFGER") @ResponseBody public String Register(String username,String nickname,String password,String phone,String email,String emailcode, HttpSession session,HttpServletRequest req) { String sessionemailcode =(String) session.getAttribute("Captcha"); if(emailcode.equals(sessionemailcode)&&emailcode!=null&&sessionemailcode!=null) { int userid = (int)(Math.random()*100000); `userservice.Register(userid,username,nickname,password,phone,email); //注册成功返回登录 return "OK"; } //注册失败返回注册页 return "No"; }``
注册jsp页面-ajax:
<script> function Register(){ var username = $("#username").val(); var nickname = $("#nickname").val(); var password = $("#password").val(); var phone = $("#phone").val(); var emailer = $("#emailer").val(); var emailcode=$("#emailboxcode").val(); $.ajax({ url:"Register-QAZXFSDFGER", //要处理的页面 data:{username:username,nickname:nickname,password:password,phone:phone,email:emailer,emailcode:emailcode}, //要传过去的数据 type:"POST", dataType:"TEXT", //返回的数据类型,TEXT字符串 JSON返回JSON XML返回XML;dataType中T要大写!! success: function(data){ if(data=="OK") { window.location.href ='Login-Success'; }else{ } }, error:function(){ alert("注册失败"); } }); } </script>` //注册表单代码: <div class="middle-box text-center loginscreen animated fadeInDown"> <div> <div> </div> <h3>欢迎注册 地质大数据平台</h3> <p>创建一个PFAS新账户</p> <div class="form-group"> <input type="text" class="form-control" id="username" placeholder="请输入真实姓名" required=""> </div> <div class="form-group"> <input type="text" class="form-control" id="nickname" placeholder="请输入昵称" required=""> </div> <div class="form-group"> <input type="password" class="form-control" id="password" placeholder="请输入用户密码" required=""> </div> <div class="form-group"> <input type="password" class="form-control" placeholder="请再次输入用户密码" required=""> </div> <div class="form-group"> <input type="text" class="form-control" id="phone" placeholder="请输入手机号" required=""> </div> <form action="send" method="post"> <button type="submit" style=" width:100px;height:30px;" >发送验证码</button> <input type="text" class="form-control" placeholder="请输入邮箱" style=" margin-bottom:10px;margin-top:10px;" name="email" id="emailer" > <input type="text" class="form-control" placeholder="请输入邮箱验证码" style=" margin-bottom:50px;" name="emailboxcode" id="emailboxcode" > </form> <button class="btn btn-primary block full-width m-b" onclick="Register()">注 册</button> <p class="text-muted text-center"><small>注册代表同意该协议-</small><a href="login.html">网站协议</a> </p> </div> </div> `