Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring框架电子邮件确认链接错误_Java_Spring_Spring Mvc_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java Spring框架电子邮件确认链接错误

Java Spring框架电子邮件确认链接错误,java,spring,spring-mvc,spring-boot,spring-data-jpa,Java,Spring,Spring Mvc,Spring Boot,Spring Data Jpa,我已经在spring boot中创建了一个电子邮件确认,它工作得很好,而且我还创建了一个链接,当用户单击它时,它应该说“已确认”,但我不知道该怎么做 电子邮件发件人java类: @Component public class EmailSender { @Autowired JavaMailSender javaEmailSender; public void sendEmail(String to, String subject,

我已经在spring boot中创建了一个电子邮件确认,它工作得很好,而且我还创建了一个链接,当用户单击它时,它应该说“已确认”,但我不知道该怎么做

电子邮件发件人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 "";
            }
        }
@组件
公共类电子邮件发送者{
@自动连线
JavaMailSender javaEmailSender;
public void sendEmail(字符串收件人、字符串主题、字符串文本)引发MessaginException{
mimessage message=javaEmailSender.createmimessage();
MimeMessageHelper=新的MimeMessageHelper(消息,
MIMessageHelper.MULTIPART\u MODE\u MIXED\u相关,
StandardCharsets.UTF_8.name());
helper.seto(to);
helper.setSubject(subject);
setText(text,true);
addInline(“logo.jpg”,新类路径资源(“./images/logo.jpg”);
javaEmailSender.send(消息);
}
}
电子邮件模板加载器:
@组成部分
公共类EmailTemplateLoader{
@自动连线
私有配置freemakerConfiguration;
公共字符串getEmailBody(字符串名称、字符串确认链接)引发TemplateException{
试一试{
Template-Template=freemakerConfiguration.getTemplate(“EmailConfirmation.ftl”);
映射数据=新的HashMap();
数据输入(“名称”,名称);
data.put(“confirmationLink”,confirmationLink);
返回FreeMarkerTemplateUtils.ProcessTemplateToString(模板,数据);
}捕获(IOE异常){
e、 printStackTrace();
}捕获(模板异常e){
e、 printStackTrace();
}
返回“”;
}
}
我的注册资源:

@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);
        }
    }
@Autowired
私人注册服务注册服务;
@自动连线
私有HttpServletRequest HttpServletRequest;
@RequestMapping(value=“user/signup”,method=RequestMethod.POST,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
@事务性(rollboor=Exception.class)
公共响应注册(@RequestBody UserType user)引发注册异常{
URL requestUrl=null;
试一试{
requestUrl=新URL(httpServletRequest.getRequestURL().toString());
}捕获(格式错误){
debug(“格式错误的请求Url”);
}
debug(requestUrl.toString());
signupService.signUp(用户,requestUrl.getHost());
返回新的响应状态(HttpStatus.OK);
}
@RequestMapping(value=“user/confirmation”,method=RequestMethod.GET,products=MediaType.APPLICATION\u JSON\u value)
@事务性(rollboor=Exception.class)
公共响应身份确认注册(@RequestParam(“u”)字符串登录名,@RequestParam(“p”)字符串令牌){
试一试{
signupService.emailConfirmation(登录名、令牌);
返回新的响应状态(HttpStatus.OK);
}捕获(注册例外){
返回新的ResponseEntity(例如getMessage(),HttpStatus.BAD_请求);
}
}

因此,正如我所说,我可以成功发送电子邮件,但我无法排序如何才能成功编写确认信

您想发送确认信还是只想知道邮件已发送的确认状态?请具体说明

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

@自动连线 私有配置Freemarker配置

并在方法中添加类似的内容以获得验证

模板t=freemarkerConfiguration.getTemplate(“verification.ftl”); String text=FreeMarkerTemplateUtils.processTemplateIntoString(t,object)


或者,您可以在返回confirmSignup()方法时对ResponseEntity执行某些操作。您还可以添加一些数据以获得确认状态。

我在电子邮件上有链接,当我单击自己的链接时,它应该重定向我的模板,即验证。ftl好的确认状态基本上是我编辑了我的答案你可以在你的服务代码中执行类似的操作谢谢你的建议,你能给我一个代码示例吗