greenmail 服务器收不到邮件
greenmail server not receiving the mail
我的 class 通过 Greenmail 发送电子邮件的单元测试
public class GreenMailTest {
private GreenMail greenMail;
private EmailServiceImpl emailService = new EmailServiceImpl();
private MessageTemplateService messageTemplateService;
private EmailProperties emailProperties;
private Properties props;
private static final String USER_PASSWORD = "abcdef123";
private static final String USER_NAME = "hascode";
private static final String EMAIL_USER_ADDRESS = "hascode@localhost";
private static final String EMAIL_TO = "someone@localhost.com";
private static final String EMAIL_SUBJECT = "Test E-Mail";
private static final String EMAIL_TEXT = "This is a test e-mail.";
private static final String LOCALHOST = "localhost";
// private GreenMail mailServer;
@Before
public void testSmtpInit() {
//ServerSetup setup = new ServerSetup();
greenMail = new GreenMail(ServerSetupTest.SMTP);
greenMail.start();
messageTemplateService = mock(MessageTemplateService.class);
emailProperties = mock(EmailProperties.class);
emailService.setEmailProperties(emailProperties);
}
@Test
public void testEmail() throws InterruptedException, IOException {
greenMail.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD);
// create the javax.mail stack with session, message and transport ..
Properties props = System.getProperties();
props.put("mail.smtp.host", LOCALHOST);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", ServerSetupTest.SMTP.getPort());
Session session = Session.getInstance(props, null);
Message msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(EMAIL_TO));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(EMAIL_USER_ADDRESS, false));
msg.setSubject(EMAIL_SUBJECT);
msg.setText(EMAIL_TEXT);
msg.setSentDate(new Date());
Transport t = session.getTransport("smtp");
t.connect(EMAIL_USER_ADDRESS, USER_PASSWORD);
t.sendMessage(msg, msg.getAllRecipients());
// assertEquals("250 OK\n", t.getLastServerResponse());
t.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// fetch messages from server
MimeMessage[] messages = greenMail.getReceivedMessages();
我用这段代码在junit上测试邮件服务器。
但是服务器没有return任何消息
我做错了什么。
我改代码了请审核
GreenMail 在本地主机上 运行。相应地调整您的 smtp 主机:
props.put("mail.smtp.host", "localhost");
编辑
总结大量评论:另一个问题来自 mock-javamail
在类路径上。
我的 class 通过 Greenmail 发送电子邮件的单元测试
public class GreenMailTest {
private GreenMail greenMail; private EmailServiceImpl emailService = new EmailServiceImpl(); private MessageTemplateService messageTemplateService; private EmailProperties emailProperties; private Properties props; private static final String USER_PASSWORD = "abcdef123"; private static final String USER_NAME = "hascode"; private static final String EMAIL_USER_ADDRESS = "hascode@localhost"; private static final String EMAIL_TO = "someone@localhost.com"; private static final String EMAIL_SUBJECT = "Test E-Mail"; private static final String EMAIL_TEXT = "This is a test e-mail."; private static final String LOCALHOST = "localhost"; // private GreenMail mailServer; @Before public void testSmtpInit() { //ServerSetup setup = new ServerSetup(); greenMail = new GreenMail(ServerSetupTest.SMTP); greenMail.start(); messageTemplateService = mock(MessageTemplateService.class); emailProperties = mock(EmailProperties.class); emailService.setEmailProperties(emailProperties); } @Test public void testEmail() throws InterruptedException, IOException { greenMail.setUser(EMAIL_USER_ADDRESS, USER_NAME, USER_PASSWORD); // create the javax.mail stack with session, message and transport .. Properties props = System.getProperties(); props.put("mail.smtp.host", LOCALHOST); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", ServerSetupTest.SMTP.getPort()); Session session = Session.getInstance(props, null); Message msg = new MimeMessage(session); try { msg.setFrom(new InternetAddress(EMAIL_TO)); msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(EMAIL_USER_ADDRESS, false)); msg.setSubject(EMAIL_SUBJECT); msg.setText(EMAIL_TEXT); msg.setSentDate(new Date()); Transport t = session.getTransport("smtp"); t.connect(EMAIL_USER_ADDRESS, USER_PASSWORD); t.sendMessage(msg, msg.getAllRecipients()); // assertEquals("250 OK\n", t.getLastServerResponse()); t.close(); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // fetch messages from server MimeMessage[] messages = greenMail.getReceivedMessages();
我用这段代码在junit上测试邮件服务器。
但是服务器没有return任何消息
我做错了什么。
我改代码了请审核
GreenMail 在本地主机上 运行。相应地调整您的 smtp 主机:
props.put("mail.smtp.host", "localhost");
编辑
总结大量评论:另一个问题来自 mock-javamail
在类路径上。