Java Jsp Spring mvc2中的电子邮件验证
我一直在努力使电子邮件验证在春天。现在我只是被困在一个地方,不知道到底该修哪个部分。目前电子邮件功能不起作用。 我想做的是,当用户在那里键入电子邮件并按下按钮时,它会自动创建唯一的验证码,并将其发送到用户键入的电子邮件地址。当用户在输入中键入正确的代码时,此电子邮件地址验证成功,他们可以创建新帐户 这是我的控制器Java Jsp Spring mvc2中的电子邮件验证,java,spring,jsp,email,verification,Java,Spring,Jsp,Email,Verification,我一直在努力使电子邮件验证在春天。现在我只是被困在一个地方,不知道到底该修哪个部分。目前电子邮件功能不起作用。 我想做的是,当用户在那里键入电子邮件并按下按钮时,它会自动创建唯一的验证码,并将其发送到用户键入的电子邮件地址。当用户在输入中键入正确的代码时,此电子邮件地址验证成功,他们可以创建新帐户 这是我的控制器 //email verification mapping @RequestMapping(value = "/emailAutho.do", method = RequestMeth
//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();
}
}
这是jsp的验证代码
/*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();
}
}
<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>
<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">
键入您的7位代码。
这是jsp,用户在其中键入电子邮件地址并点击按钮,然后发送验证代码
/*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();
}
}
<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>
<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`到底什么不起作用?代码的哪一部分会导致问题?若你们也发布了堆栈跟踪信息,那个将很有帮助。@Piotr Podraza您好,我添加了不起作用的内容。当我按下按钮时,它应该发送带有验证码的电子邮件,但它却给我错误消息500
嵌套异常为java.lang.NumberFormatException:null
。