Spring Framework电子邮件确认链接错误

时间:2018-08-10 09:59:53

标签: java spring spring-mvc spring-boot spring-data-jpa

我已经在春季启动中创建了一个电子邮件确认,它工作正常,并且当用户单击该链接时应该显示“已确认”的链接时,我也创建了一个链接,但是我不知道该怎么做?

电子邮件发件人java类:

 @Component
    public class EmailSender {

        @Autowired 
        JavaMailSender javaEmailSender;
        public void sendEmail(String to, String subject, String text) throws MessagingException {
             MimeMessage message = javaEmailSender.createMimeMessage();
             MimeMessageHelper helper = new MimeMessageHelper(message,
                        MimeMessageHelper.MULTIPART_MODE_MIXED_RELATED,
                        StandardCharsets.UTF_8.name());
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            helper.addInline("logo.jpg", new ClassPathResource("./images/logo.jpg"));
            javaEmailSender.send(message);
        }

}


 E mail template Loader:



     @Component
        public class EmailTemplateLoader {

            @Autowired
            private Configuration freemakerConfiguration;

            public String getEmailBody(String name, String confirmationLink) throws TemplateException {

                try {
                    Template template = freemakerConfiguration.getTemplate("EmailConfirmation.ftl");

                    Map<String, Object> data = new HashMap<String, Object>();
                    data.put("name", name);
                    data.put("confirmationLink", confirmationLink);

                    return FreeMarkerTemplateUtils.processTemplateIntoString(template, data);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TemplateException e) {
                    e.printStackTrace();
                }
                return "";
            }
        }

我的注册资源:

@Autowired
    private SignupService signupService;
    @Autowired
    private HttpServletRequest httpServletRequest;

    @RequestMapping(value = "user/signup", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<?> signup(@RequestBody UserType user) throws SignUpException {
        URL requestUrl = null;
        try {
            requestUrl = new URL(httpServletRequest.getRequestURL().toString());
        } catch (MalformedURLException e) {
            logger.debug("Malformed Request Url");
        }
        logger.debug(requestUrl.toString());
        signupService.signUp(user, requestUrl.getHost());
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "user/confirmation", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<?> confirmSignUp(@RequestParam("u") String loginName, @RequestParam("p") String token) {
        try {
            signupService.emailConfirmation(loginName, token);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (SignUpException e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
        }
    }

所以,正如我告诉我的那样,我可以成功发送电子邮件,但无法对如何成功发送确认邮件进行排序

1 个答案:

答案 0 :(得分:0)

您要发送确认邮件还是只想知道已发送邮件的确认状态?请指定。

您可以在已实现的SignUpService中添加一些代码  emailConfiguration()方法

@自动连线 私人配置freemarkerConfiguration;

并在方法中添加这样的内容以进行验证

模板t = freemarkerConfiguration.getTemplate(“ verification.ftl”); 字符串文本= FreeMarkerTemplateUtils.processTemplateIntoString(t,object);

或者您可以在返回ConfirmSignup()方法时对ResponseEntity进行一些操作。您还可以添加一些DTO来获取确认状态。