Java 使用Spring Boot和Thymeleaf发送HTML电子邮件
我正在查看如何使用Spring Boot发送电子邮件。 使用标准Spring引导模块发送电子邮件,并使用Thymeleaf模板引擎为消息准备HTML内容。 这就是我使用的依赖项Java 使用Spring Boot和Thymeleaf发送HTML电子邮件,java,spring,email,spring-boot,smtp,Java,Spring,Email,Spring Boot,Smtp,我正在查看如何使用Spring Boot发送电子邮件。 使用标准Spring引导模块发送电子邮件,并使用Thymeleaf模板引擎为消息准备HTML内容。 这就是我使用的依赖项 <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-st
<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>
这是我的测试课
@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();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
公共类MailClientTest{
@自动连线
私人邮件客户端;
私有绿信smtpServer;
@以前
public void setUp()引发异常{
smtpServer=new GreenMail(新服务器设置(25,空,“smtp”);
smtpServer.start();
}
@试验
public void shouldSendMail()引发异常{
//给定
String recipient=“amadeu。cabanilles@gmail.com";
String message=“测试消息内容”;
//什么时候
mailClient.prepareAndSend(收件人,邮件);
//然后
字符串内容=“消息+”;
assertReceivedMessageContains(内容);
}
私有void assertReceivedMessageContains(应为字符串)引发IOException、MessaginException{
MimeMessage[]receivedMessages=smtpServer.getReceivedMessages();
assertEquals(1,receivedMessages.length);
字符串内容=(字符串)receivedMessages[0]。getContent();
系统输出打印项次(内容);
assertTrue(content.contains(预期));
}
@之后
public void tearDown()引发异常{
smtpServer.stop();
}
}
在我的计算机上执行测试正常,我通过了测试,但没有收到任何电子邮件。您没有收到任何电子邮件,因为此集成测试使用本地测试SMTP服务器存根-绿色邮件。该测试不发送真实的电子邮件,只验证邮件是否准备好并正确发送,如果生产环境中有真实的SMTP服务器可用
为了从您的本地环境发送电子邮件,您需要设置一些SMTP服务器,但是,如果邮件确实发送,则自动验证是完全不同的情况。因此,无法查看Thymeleaf电子邮件模板在最后的样子?很遗憾,此测试不适用。测试邮件模板总是很痛苦,因为每个客户端都有自己的限制。但是,您可以在本地设置SMTP服务器,并修改此测试以使用它来代替GreenMail。将发送真正的电子邮件,以便您可以在所需的客户端中手动验证。谢谢:)虽然已经有一段时间了,但这段代码示例看起来很熟悉:)您应该寻找一些关于如何为您的目标邮件客户端编写HTML模板的指南。模板不是标准化的,但有一些好的做法值得遵循。@AmadeuCabanilles您可以随时编译模板,并检查生成的HTML是否是正确断言中所期望的,当您得到结果(即HTML)时,您可以在浏览器中打开它。总之,作为一个附带说明,我正在积极开发一个名为“用Spring Boot发送电子邮件”的插件。它也许可以在不浪费时间的情况下加快开发速度,重新实现基本功能。
@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();
}
}