Jsp Spring中的电子邮件验证mvc2

时间:2017-01-20 11:38:48

标签: java spring jsp email verification

我一直在努力在春季进行电子邮件验证。现在我只是陷入了一个地方,并且不知道我应该确切地确定哪个部分。现在电子邮件功能不起作用。 我想要做的是当用户输入电子邮件并点击按钮时,它会自动创建唯一的验证码并将其发送到用户输入的电子邮件地址。当用户在输入中键入正确的代码而不是成功验证此电子邮件地址时,他们可以创建新帐户。

这是我的控制器

//email verification mapping
@RequestMapping(value =  "/emailAutho.do", method = RequestMethod.GET)
public ModelAndView emailAuth(HttpServletResponse response,
        HttpServletRequest request) throws Exception{

    String email = request.getParameter("email");
    String authNum = "";

    authNum = RandomNum();

    sendEmail(email.toString(), authNum);

    ModelAndView mv = new ModelAndView();
    mv.setViewName("/member/emailAuth.jsp");
    mv.addObject("email",email);
    mv.addObject("authNum", authNum);

    return mv;


}

//creating verification code method
private String RandomNum() {
        StringBuffer buffer = new StringBuffer();
        for (int i=0; i<=6; i++){
            int n = (int) (Math.random() *10);
            buffer.append(n);
        }
        return buffer.toString();
    }

//sending email
private void sendEmail(String email, String authNum) {
    String host = "smtp.gmail.com";
    String subjet = "title";
    String fromName = "nomad";
    String from = "";
    String tol = email;

    String content = "your verification number is [" +authNum +"]";

    try{
        Properties props = new Properties();
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", host);
        props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.auth", "true");

        Session mailSession = Session.getInstance(props, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication("", "", "");

            }
        });

    Message msg = new MimeMessage(mailSession);

    msg.setFrom(new InternetAddress(from, MimeUtility.encodeText(fromName, "UTF-8", "B")));

    InternetAddress[] address1 = {new InternetAddress(tol)};
    msg.setRecipients(Message.RecipientType.TO, address1);
    msg.setSubject(subjet);
    msg.setSentDate(new java.util.Date());
    msg.setContent(content, "text/html;charset=euc-kr"); //set content
    Transport.send(msg);
    }catch(MessagingException e){

        e.printStackTrace();

    }catch(Exception e){

        e.printStackTrace();
    }
}

这是用于检查验证码的JavaScript。

    /*check email verification code  */
    function check(){
        var form = document.authenform;
        var authNum = ${authNum};

        if(!form.authnum.value){
            alert("type your verification code");
            return false;
            }
        if(form.authnum.value != authNum){
            alert("This is wrong verification code. Please re-enter the code again.");
            form.authnum.value="";
            return false;
        }
        if(form.authNum.value == authNum){
            alert("verification succeded");
            opener.document.userinput.mailCheck.value="verification succeded.";
            self.close();
        }
    }

这是验证码的jsp。

<form method="post" name="authenform" onSubmit="return check();">
            <label id="valid_email">Type your 7 digit code.</label>
            <input id="authnum" name="authnum" type="text"/>                        
            <input type="submit" value="Submit">
            </form>

这是用户输入电子邮件地址并按下按钮而不是发送验证码的jsp。

<form method="post" name="emailAutho" onSubmit="return check();">
            <label id="valid_email">email</label>
            <input id="email" name="email" type="email" />                      
            <input type="submit" class="form-style-join" value="submit">

问题是电子邮件未发送,只显示错误消息500  请求处理失败;嵌套异常是java.lang.NumberFormatException:null`

0 个答案:

没有答案