【问题标题】:Add a mail template or without it sending mails in jhipster在 jhipster 中添加或不添加邮件模板发送邮件
【发布时间】:2018-04-09 20:12:18
【问题描述】:

我需要在 .../build/resources/main/mails/abc.html 中添加一个邮件模板(abc.html),例如默认的 activationEmail.html、CreationEmail.html、passwordResetEmail.html。当我手动创建该 abc.html 并运行 gradlew 执行应用程序后,新创建的 abc.html 文件被删除。那么我该如何创建它呢?我需要在其他地方注册该文件吗? 是否有任何特定命令可以创建 html 文件作为在 jhipster 中创建实体。我的最终目标是发送邮件。但是如果不创建模板,我不知道在 MailService.java 的 sendMail 方法中传递内容变量的值。为内容分配 null 也不起作用。那么有没有不使用模板发送邮件的内置方法呢?

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
    log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
        isMultipart, isHtml, to, subject, content);

    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
        message.setTo(to);
        message.setFrom(jHipsterProperties.getMail().getFrom());
        message.setSubject(subject);
        message.setText(content, isHtml);
        javaMailSender.send(mimeMessage);
        log.debug("Sent email to User '{}'", to);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.warn("Email could not be sent to user '{}'", to, e);
        } else {
            log.warn("Email could not be sent to user '{}': {}", to, e.getMessage());
        }
    }
}

@Async
public void sendSportMail(String email,String message) {
    log.debug("Sending sports email to '{}'", email," message");
    Locale locale = Locale.forLanguageTag("en");
    String subject = messageSource.getMessage("email.reset.title", null, locale);
    Context context = new Context(locale);
    context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
    String content = templateEngine.process("creationEmail", context);
    sendEmail(email, subject, content, false, true);
}

在 sendSportMail 方法而不是“creationEmail”中,我需要一个值(“abc”)来传递。或者,如果有任何方法可以在不创建更好的模板的情况下发送邮件。因为实际上我不需要邮件模板。我可以在 sendSportMail 方法中检索电子邮件和消息参数的值。所以我只需要发送邮件正文是消息而收件人的邮件是电子邮件的邮件。 gmail 的配置已正确完成,并且对于已内置的功能(如密码重置、创建用户等)运行良好。

【问题讨论】:

    标签: spring spring-boot smtp thymeleaf jhipster


    【解决方案1】:

    不要在 build/ 文件夹中创建任何东西,因为 gradle 使用它来编译和打包您的项目。类似于 maven 的 target/ 文件夹。

    改为在 src/main/resources/mails/ 中创建您的 html 模板

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2018-02-02
      • 2020-01-27
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      相关资源
      最近更新 更多