2

I am using greenmail as development Mail server. My use case is to 1) send an email on the mail server and 2) another process will keep looking that mail server using IMAP and notify if there is any new email.

To achieve first step 1) send an email I set up the smtp server using below command of Greenmail

java -Dgreenmail.setup.test.all -Dgreenmail.users=test1:pwd1,test2:pwd2@example.com \
     -jar greenmail-standalone.jar

Now when I used normal API of java to send an email using SMTP. It says that the email send successfully but I am not receiving any email on the "to" address where I sent an email. Below is the source code to send email

public static void main(String [] args) {    
      // Recipient's email ID needs to be mentioned.
      String to = "abcd@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "web@gmail.com";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties=new Properties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      props.put("mail.smtp.port", "3025"); //TLS Port
       props.put("mail.smtp.auth", "true"); //enable authentication
       props.put("mail.smtp.starttls.enable", "true");

      // Get the default Session object.
      Authenticator auth = new Authenticator() {
        //override the getPasswordAuthentication method
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(fromEmail, password);
        }
    };
Session session = Session.getInstance(props, auth);

      try {
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }

Could you please help me in finding out what I am doing wrong? 1) Can we really send an email using greenmail?

This is my Debug output

DEBUG: JavaMail version 1.4.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "localhost", port 3025, isSSL false
220 /127.0.0.1 GreenMail SMTP Service v1.6.0-SNAPSHOT ready
DEBUG SMTP: connected to host "localhost", port: 3025

EHLO 127.0.0.1
250 /127.0.0.1
DEBUG SMTP: use8bit false
MAIL FROM:<test1@localhost>
250 OK
RCPT TO:<abcd@gmail.com>
250 OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   abcd@gmail.com
DATA
354 Start mail input; end with <CRLF>.<CRLF>
From: test1@localhost
To: abcd@gmail.com
Message-ID: <1296064247.0.1509389569017.JavaMail.s0065311@IRV-DU10507>
Subject: Email From my Greenmail
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Test Mail sent from My Greenmail!!
.
250 OK
QUIT
221 /127.0.0.1 Service closing transmission channel
Email sent successfully from greenmail

Though it says everything is OK and status is 250 OK, i didn't receive the email.

Regards Maulik

MMJ
  • 519
  • 1
  • 9
  • 26
  • What does the [JavaMail debug output](https://javaee.github.io/javamail/FAQ#debug) show? Does greenmail have any sort of log file or debug output or verbose mode? – Bill Shannon Oct 28 '17 at 05:33
  • Honestly, I dont know how to look at the logs. I tried to see if there is any configuraion to enable the log and see it but I couldn't find any. – MMJ Oct 28 '17 at 05:37
  • Where did you look? Really, it wasn't that hard to find. Scroll half way down [this page](http://www.icegreen.com/greenmail/) to see the option `-Dgreenmail.verbose`. Try that and the JavaMail debug output. – Bill Shannon Oct 28 '17 at 23:27
  • This is what I get in the log `DEBUG smtp.SmtpServer| Handling new client connection smtp:127.0.0.1:3025<-/127.0.0.1:41377 5003 INFO smtp.SmtpManager| Created user login abcd@gmail.com for address abcd@gmail.com with password maulik.abcd@gmail.com because it didn't exist before.` – MMJ Oct 30 '17 at 17:06
  • What I think, greenmail is not actually sending the email to the to Address, It may be just return success When we do `Transport.Send`. As it main purpose is for the JUnit test – MMJ Oct 30 '17 at 17:09
  • How are you looking at greenmail to see if the message has arrived at greenmail? (You understand that the message is not going to be delivered to Gmail, right?) – Bill Shannon Oct 30 '17 at 21:55
  • Also, you're using a very old version of JavaMail. You should [upgrade](https://javaee.github.io/javamail/) if possible. – Bill Shannon Oct 30 '17 at 21:56
  • Yes, I know that the emails are not going to delivered. But when I tried to read those email using IMAP after sending it, it's not receiving at other end.. – MMJ Oct 30 '17 at 22:43
  • I am doing this steps: 1) run green mail so that it creates mail server SMTP on 3025 and IMAP on 3143. 2) Run IMAP thread to watch any email came on localhost on above ports. 3) Send an email (as shown above) . But I didn't get any email on localhost. Hope you understand my test scenario! – MMJ Oct 30 '17 at 22:46
  • Sorry, this doesn't look like a JavaMail problem. It looks like you need help from the greenmail experts. Perhaps try their forums? – Bill Shannon Oct 31 '17 at 06:38

1 Answers1

2

I found out the issue with my code!

Issue was the toAddress in my IMAP reader code. I was using the wrong login id and password to access the localhost account.

Once I fixed it, I started receiving the emails on IMAP as well.

Below is working code example.

1) TestEmail - which send email on SMTP

public static void main(String[] args) throws Exception {

    Session session;
    String user = "test1";
    String password = "pwd1";

    String fromAddress = "test1@localhost"; // newlycreateduser@localhost
    String toAddress = "test1@localhost";

    // Create a mail session
    Properties properties = System.getProperties();
    properties.put("mail.transport.protocol", "smtp");
    properties.put("mail.transport.protocol.rfc822", "smtp");
    properties.put("mail.smtp.host", "localhost");
    properties.put("mail.smtp.port", "3025");
    properties.put("mail.debug", "true");
    properties.put("mail.smtp.localaddress", "127.0.0.1");
    session = Session.getInstance(properties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("test1", "pwd1");
        }
    });

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(fromAddress));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
        message.setSubject("Email From my Greenmail");
        message.setText("Test Mail sent from My Greenmail!!");
        message.addHeader("X-THALES-ID", "1");
        message.addHeader("X-ROUTE-TO", "thalestest");
        message.addHeader("X-GROUND-TYPE", "GROUND");
        message.addHeader("X-ORIGINAL-FROM", "ambatltesttool");
        message.addHeader("X-EMBATL-ERROR", "");
        Transport.send(message);


        System.out.println("Email sent successfully from greenmail");
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

2) TestIMAP - which reads the localhost account continuously to check new emails!

public static void main(String[] args) throws Exception {
        Session sessionIMAP;
        sessionIMAP = setupImap();
        while(true) {

            Store store = sessionIMAP.getStore("imap");
            store.connect("localhost", 3143, "test1@localhost", "test1@localhost");
            if (store.isConnected()) {
                System.out.println("IMAP is connected");
                Folder folder = store.getFolder("INBOX");
                if (folder != null) {
                    folder.open(Folder.READ_ONLY);
                    //folder.getMessage(1);
                    if(folder.getMessageCount() > 0) {
                        System.out.println("maulik - " + folder.getMessage(1).getSubject());
                    }
                    Message[] messages = folder.getMessages();
                    System.out.println("maulik messages.length---" + folder.getMessageCount());
                }
            } else {
                System.out.println("IMAP is not connected");
            }
            Thread.sleep(1000);
        }
    }

    private static Session setupImap() {
        System.out.println("in setupImap");
        Session session1;
        Properties props = new Properties();
        props.setProperty("mail.store.protocol", "imap");
        props.put("mail.imap.host", "localhost");
        props.put("mail.imap.port", 3143);
        props.put("mail.debug", "true");
        props.put("mail.imap.localaddress", "127.0.0.1");
        session1 = Session.getInstance(props, null);
        return session1;
    }

Regards

MMJ
  • 519
  • 1
  • 9
  • 26