我想发送邮件给用户我使用spring boot和angular成功了但我有一个问题是我的控制台总是向我显示我的邮件:成功发送的消息:当邮件发送给用户时:
这是我的角度js controller.js:
$scope.sentMail = function() { var data = new FormData();var mail= $scope.account.mail;
console.log('test 1 :', $scope.account.mail);
console.log("sent mail");
$http.post(_contextPath + '/sentEmail',mail).then(Failure)
.catch(Success);
function Success(success) {
// console.log(response.data);
console.log(' email is successfully sent:', $scope.account.mail);
}
function Failure(error) {
console.log('A problem occurred while sending an email.');
}
}
这是我的controller.java:
@RequestMapping(value = "/sentEmail", method = RequestMethod.POST, headers = "Accept=application/json")
public ModelAndView processForgotPasswordForm(ModelAndView modelAndView, HttpServletRequest request, @RequestBody String mail) throws MessagingException
{
System.out.println("print");
User optional= usersService.findByEmail(mail);
if((optional== null))
{
modelAndView.addObject("errorMessage", "We didn't find an account for that e-mail address.");
}
else{
optional.setResetToken(UUID.randomUUID().toString());
usersService.save(optional);
String appUrl = request.getScheme() + "://" + request.getServerName();
SimpleMailMessage passwordResetEmail = new SimpleMailMessage();
passwordResetEmail.setFrom("test@test.com");
passwordResetEmail.setTo(optional.getUserEmail());
passwordResetEmail.setSubject("Password Reset Request");
passwordResetEmail.setText("Hello "+ optional.getUserFirstname()+" "+ optional.getUserLastname()+", \n \n You recently request to reset your password for rotana platform. Please click the link below to reset it : \n \n"+ appUrl +":8080/neorchaWEBPlatform"+ "/reset?token=" + optional.getResetToken()
+"\n \n if you did not request a password reset, please ignore email or reply to let us know.\n \n Thanks, \n \n Rotana Group \n \n P.S. We also love hearing from you and helping you with any issues you have. Please reply to this email if you want to ask a question.");
modelAndView.addObject("ResetMessage", "An e-mail has been sent to " + optional.getUserEmail());
emailService.sendEmail(passwordResetEmail);
}
modelAndView.setViewName("forgotPassword");
return modelAndView;
}
任何帮助,提前谢谢。
答案 0 :(得分:0)
.then
将处理“成功”回复。 .catch
会处理success
或error
个函数中的任何错误。您没有注册error
功能。我相信你的代码应该是这样的:
$http.post(_contextPath + '/sentEmail',mail).then(Success, Failure)
.catch(UnexpectedError);
function Success(success) {
// console.log(response.data);
console.log(' email is successfully sent:', $scope.account.mail);
}
function Failure(error) {
console.log('A problem occurred while sending an email.');
}
function UnexpectedError(error) {
console.log('Something unexpected happened in Success() or Failure()');
}
成功和错误回调查看服务器返回的状态。来自Angular文档:
200到299之间的响应状态代码被视为成功 status并将导致调用成功回调。任何 超出该范围的响应状态代码被视为错误 status并将导致调用错误回调。也, 小于-1的状态代码归一化为零。 -1通常意味着 请求已中止,例如使用config.timeout。注意,如果 响应是重定向,XMLHttpRequest将透明地遵循它, 意味着结果(成功或错误)将由决定 最终答复状态代码。