java - greenmail 服务器收不到邮件

标签 java testing junit greenmail

  • 我的通过 Greenmail 发送电子邮件的类(class)单元测试

    公共(public)类 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 上测试电子邮件服务器。

  • 但是服务器没有返回任何消息

  • 我做错了什么。

  • 我改了代码请审核

最佳答案

GreenMail 在本地主机上运行。相应地调整您的 smtp 主机:

props.put("mail.smtp.host", "localhost");

编辑

总结大量评论:额外的问题来自于 mock-javamail 在类路径中。

关于java - greenmail 服务器收不到邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29368123/

相关文章:

java - 如何使用 spring-cloud-netflix 和 feign 编写集成测试

java - Set 集合中的重复值?

java - 如何为此类编写junit测试用例

java - JUnit assertTrue 异常

java - 为什么类称为对象的抽象?

testing - 开源 ISDN 协议(protocol)测试

testing - 在 angular 6 测试脚本中监听全局渲染器事件触发器

testing - 有没有办法在一个人的测试类之外使用 TestNG DataProvider?

java - JUnit:是否可以创建一个测试套件来执行共享命名约定的类的所有测试?

java - 在 Spring 中实现 thymeleaf 时无法访问对象的属性