您可以使用测试 SMTP 服务器,如 Dumbster。请参阅下面的示例:
@Test
public void sendSimpleEmailWithCC() {
// Runs a Dumbster simple SMTP server - default config
SimpleSmtpServer server = SimpleSmtpServer.start();
String from = "whoever@from.com";
String to = "whoever@to.com";
String messageText = "Good message";
String title = "Test message";
String cc = "whoever@cc.com";
Assert.assertTrue(mailSender
.sendEmail(from, to, cc, title, messageText));
server.stop();
Assert.assertTrue(server.getReceivedEmailSize() == 1);
Iterator emailIter = server.getReceivedEmail();
SmtpMessage email = (SmtpMessage) emailIter.next();
Assert.assertTrue(email.getHeaderValue("From").equals(from));
Assert.assertTrue(email.getHeaderValue("To").equals(to));
Assert.assertTrue(email.getHeaderValue("Cc").equals(cc));
Assert.assertTrue(email.getHeaderValue("Subject")
.equals("Test message"));
Assert.assertTrue(email.getBody().equals(messageText));
}