mardi 31 mars 2015

greenmail server not receiving the mail



  • My Unit Test for class for email through Greenmail


    @Before public void testSmtpInit() { 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 {



    String to = "abc@localhost.com";
    String from = "test@localhost.com";
    String subject = "subject";
    String contentType = "text/html";
    String user = "test@localhost.com";
    String login="test@localhost.com";
    String password = "12345";
    String body = "msgbody";
    greenMail.setUser(login,user, password);
    Properties props = System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    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(to));
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
    msg.setSubject(subject);
    msg.setText(body);
    msg.setSentDate(new Date());
    Transport t = session.getTransport("smtp");
    t.connect(user, password);
    t.sendMessage(msg, msg.getAllRecipients());
    t.close();
    } catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    MimeMessage[] messages = greenMail.getReceivedMessages();
    assertNotNull(messages);
    assertEquals(1, messages.length);
    MimeMessage m = messages[0];
    try {
    assertEquals(subject, m.getSubject());
    assertTrue(String.valueOf(m.getContent()).contains(body));
    assertEquals(to, m.getFrom()[0].toString());//
    } catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    }




  • I used this code to test the email server on junit.




  • But the server does not return any message




  • what i did wrong.




Aucun commentaire:

Enregistrer un commentaire