【问题标题】:Sending HTML email with Spring Boot and Thymeleaf使用 Spring Boot 和 Thymeleaf 发送 HTML 电子邮件
【发布时间】:2017-08-03 06:02:12
【问题描述】:

我正在检查如何使用 Spring Boot 发送电子邮件。 使用标准 Spring Boot 模块发送电子邮件,并使用 Thymeleaf 模板引擎为消息准备 HTML 内容。 这是我使用的依赖项

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.icegreen</groupId>
            <artifactId>greenmail</artifactId>
            <version>1.5.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

这是我的邮件客户端

@Service
public class MailClient {

    private JavaMailSender mailSender;
    private MailContentBuilder mailContentBuilder;

    @Autowired
    public MailClient(JavaMailSender mailSender, MailContentBuilder mailContentBuilder) {
        this.mailSender = mailSender;
        this.mailContentBuilder = mailContentBuilder;
    }

    public void prepareAndSend(String recipient, String message) {
        MimeMessagePreparator messagePreparator = mimeMessage -> {
            MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
            messageHelper.setFrom("amadeu.cabanilles@gmail.com");
            messageHelper.setTo("amadeu.cabanilles@gmail.com");
            messageHelper.setSubject("Sample mail subject");
            String content = mailContentBuilder.build(message);
            messageHelper.setText(content, true);
        };
        try {
            mailSender.send(messagePreparator);
        } catch (MailException e) {
            e.printStackTrace();
        }
    }
}

这是我的测试课

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class MailClientTest {

    @Autowired
    private MailClient mailClient;

    private GreenMail smtpServer;

    @Before
    public void setUp() throws Exception {
        smtpServer = new GreenMail(new ServerSetup(25, null, "smtp"));
        smtpServer.start();
    }

    @Test
    public void shouldSendMail() throws Exception {
        //given
        String recipient = "amadeu.cabanilles@gmail.com";
        String message = "Test message content";
        //when
        mailClient.prepareAndSend(recipient, message);
        //then
        String content = "<span>" + message + "</span>";
        assertReceivedMessageContains(content);
    }

    private void assertReceivedMessageContains(String expected) throws IOException, MessagingException {
        MimeMessage[] receivedMessages = smtpServer.getReceivedMessages();
        assertEquals(1, receivedMessages.length);
        String content = (String) receivedMessages[0].getContent();
        System.out.println(content);
        assertTrue(content.contains(expected));
    }

    @After
    public void tearDown() throws Exception {
        smtpServer.stop();
    }
}

在我的电脑上执行测试没问题,我通过了测试但我没有收到任何电子邮件。

【问题讨论】:

  • 无论如何,作为旁注,我正在积极开发一个名为 Email Tools 的扩展,用于使用 Spring Boot 发送电子邮件。它也许可以在不浪费时间重新实现基本功能的情况下加快开发速度。

标签: java spring email spring-boot smtp


【解决方案1】:

您没有收到任何电子邮件,因为此集成测试使用本地测试 SMTP 服务器存根 - GreenMail。该测试不发送真正的电子邮件,仅在生产中可用真正的 SMTP 服务器时验证邮件是否准备好并正确发送。

为了从您的本地环境发送电子邮件,您需要设置一些 SMTP 服务器,但是,如果邮件实际发送,自动验证是完全不同的故事。

【讨论】:

  • 所以看不到 Thymeleaf 电子邮件模板最后的样子?
  • 不幸的是,没有用这个测试。测试邮件模板总是很痛苦,因为每个客户端都有自己的局限性。但是,您可以在本地设置 SMTP 服务器并修改此测试以使用它代替 GreenMail。真实的电子邮件将被发送,因此您可以在所需的客户端中手动验证它。
  • 谢谢 :) 虽然已经有一段时间了,但这段代码示例看起来很熟悉 :) 您应该寻找一些关于如何为您的目标邮件客户端编写 HTML 模板的指南。模板没有标准化,但有一些值得遵循的良好做法。
  • @AmadeuCabanilles 您始终可以编译模板并检查生成的 HTML 是否是正确断言中的预期结果,当您获得结果(即 HTML)时,您可以在浏览器中打开它。
猜你喜欢
  • 2021-07-10
  • 2018-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
相关资源
最近更新 更多