Spring Boot Email 模板路径问题

问题描述 投票:0回答:1

我有一个 Spring Boot 应用程序,用户可以注册,应用程序会向用户发送验证电子邮件以进行注册。我按照网络上的教程编写了 AbstractEmailContext 。我还使用 AWS Elasticbeanstalk 来分发应用程序。问题是,在本地计算机数据库和电子邮件发送测试正常时。上传到 Elasticbeanstalk 后,电子邮件发送出现错误,因为 setTemplateLocation 参数找不到“confirm.html”模板。

这里是 AbstracEmailVerification 类,

public class AccountVerificationEmailContext extends AbstractEmailContext{

    private String token;

    @Override
    public <T> void init(T context){
        CCUser ccUser = (CCUser) context;
        put("name", ccUser.getName());
        setTemplateLocation("/confirm");
        setSubject("Complete your registration!");
        setFrom("[email protected]");
        setTo(ccUser.getEmail());
    }

    @Override
    public <T> void rinit(T context){
        CCUser ccUser = (CCUser) context;
        put("email", ccUser.getEmail());
        setTemplateLocation("/creset");
        setSubject("Change your Password!");
        setFrom("[email protected]");
        setTo(ccUser.getEmail());
    }

    public void setToken(String token){
        this.token = token;
        put("token", token);
    }

    public void buildVerificationUrl(final String baseUrl, final String token){
        final String url = UriComponentsBuilder.fromHttpUrl(baseUrl).path("/registration/verify").queryParam("token",token).toUriString();
        put("verificationURL",url);
    }

    public void buildResetUrl(final String baseUrl, final String token){
        final String url = UriComponentsBuilder.fromHttpUrl(baseUrl).path("/forgotpass/resetpass").queryParam("token",token).toUriString();
        put("verificationURL",url);
    }
}

以及发送电子邮件的方法,

@Override
public void sendRegistrationConfirmationEmail(CCUser user){
    ConfirmationToken secureToken = secureTokenService.createSecureToken();
    secureToken.setUserToken(user);
    secureTokenRepository.save(secureToken);
    AccountVerificationEmailContext emailContext = new AccountVerificationEmailContext();
    emailContext.init(user);
    emailContext.setToken(secureToken.getConfirmationToken());
    emailContext.buildVerificationUrl(baseUrl, secureToken.getConfirmationToken());
    try {
        emailService.sendMail(emailContext);
    } catch (MessagingException e) {
        e.printStackTrace();
    }

}

当应用程序在 AWS Elasticbeanstalk 上运行 setTemplateLocation 参数时,我找不到正确的路径。请就主题提出建议。

还有错误,

Feb  4 22:18:25 ip-10-0-10-120 web[517963]: 2024-02-04T22:18:25.691Z ERROR 517963 --- [nio-5000-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [src/resources/templates/confirm], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
Feb  4 22:18:25 ip-10-0-10-120 web[517963]: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [src/resources/templates/confirm], template might not exist or might not be accessible by any of the configured Template Resolvers
spring-boot amazon-elastic-beanstalk
1个回答
0
投票

“确认”参数前面多了一个“/”。我已将其删除,现在电子邮件方法可以使用。

© www.soinside.com 2019 - 2024. All rights reserved.