格式化从SpringBoot发送的电子邮件

我希望格式化从我的API发送的消息,使其看起来更合适。我想知道最佳做法是什么?

EmailController ,这是我发送邮件的地方。

    @PostMapping("/send")
    public void sendEmail(@RequestBody Contact contact) throws Exception {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setTo("test@gmail.com");
            mailMessage.setSubject(contact.getSubject());
            mailMessage.setText("Email: " + contact.getEmail() +
                                            contact.getMessage());
          try {
            emailSenderService.sendEmail(mailMessage);
        } catch (MailException ex) {
            System.err.println(ex.getMessage());
        }
    }

格式看起来很丑陋,有什么办法可以使文本变为粗体并添加换行符等? 任何建议都非常感谢。

gansinimabi 回答:格式化从SpringBoot发送的电子邮件

  

格式化我的邮件[...]看起来更合适

这听起来像您想要的格式文本,您可以在其中更改字体大小,加粗文本,更改颜色等。

为此,您希望电子邮件为 HTML ,而不是纯文本。

要生成HTML,通常最好使用模板引擎。 Spring Boot具有choice of multiple Template Engines来构建HTML网页,因此您最好使用相同的模板引擎来构建电子邮件文本,例如胸腺。

因此,找到有关如何调用Thymeleaf以及如何将呈现的HTML捕获为String的教程。然后将该字符串提供给mailMessage.setText(...)

本文链接:https://www.f2er.com/3166542.html

大家都在问