nithril / smtp-connection-pool

SMTP Connection Pool
Apache License 2.0
47 stars 19 forks source link

Messages not sent until pool shutdown #8

Closed tstavinoha closed 7 years ago

tstavinoha commented 7 years ago

I'm integrating this library to manage the SMTP connection pool in a multi-threaded environment. As far as I've seen, the library handles parallel connections well and reuses connections when possible.

However, I've noticed that the messages are not completely sent until I close or clear the pool. I've tried using both the local dummy smtp server, and our production mail server.

So, dummy mail logs show something like this:

250-8BITMIME
250-AUTH LOGIN
250 Ok
11:04:58 AM - Client: NOOP
11:04:58 AM - Server: 250 Ok
11:04:58 AM - Client: NOOP
11:04:58 AM - Server: 250 Ok
11:04:59 AM - Client: MAIL FROM:<me@domain.com>
11:04:59 AM - Server: 250 Ok
11:04:59 AM - Client: RCPT TO:<me@domain.com>
11:04:59 AM - Server: 250 Ok
11:04:59 AM - Client: RSET
11:04:59 AM - Server: 250 Ok
11:04:59 AM - Client: NOOP
11:04:59 AM - Server: 250 Ok

This is where it stops. Then, when I close the pool, the following logs are displayed:

11:05:13 AM - Client: RSET
11:05:13 AM - Server: 250 Ok
11:05:13 AM - Client: MAIL FROM:<me@domain.com>
11:05:13 AM - Server: 250 Ok
11:05:13 AM - Client: RCPT TO:<me@domain.com>
11:05:13 AM - Server: 250 Ok
11:05:13 AM - Client: DATA
11:05:13 AM - Server: 354 End data with <CR><LF>.<CR><LF>
11:05:13 AM - Server: 250 Ok
11:05:13 AM - Client: QUIT
11:05:13 AM - Server: 221 Bye

Everything works ok with vanilla, connection-per-message workflow. I've configured the pool as shown in README and send the messages using try with resources.

Any idea what could be wrong? Thanks

nithril commented 7 years ago

Hello,

Are you able to create and share a test case which reproduce your issue ?

tstavinoha commented 7 years ago

Ok, I created a small test to display this behavior. Unfortunately, this potential bug does not occur with dumbster or greenmail, but rather with some standalone dummy clients and Microsoft Exchange that are not easy/possible to start just for testing purposes. Therefore, I have used the 'free' Mailtrap service (credentials included in test, throwaway account) since it seems to be the easiest approach, and also suffers from the given problem - no mail in inbox until connection closed.

You can use breakpoints to pace the execution and make sure it waits for connection closing.

https://github.com/tstavinoha/smtp-connection-pool/blob/master/src/test/java/org/nlab/smtp/FlushOnlyAfterCloseTest.java

Br!

nithril commented 7 years ago

Thanks, I will look at your test.

nithril commented 7 years ago

Unfortunately I do not reproduce the issue using your test case.

I have even updated org.nlab.smtp.FlushOnlyAfterCloseTest#sendEmail to:

            Assert.assertEquals(0, getInboxCount());
            transport.sendMessage(mimeMessage);
            Assert.assertEquals(1, getInboxCount());

And the test is successful after removing the Assert.assertEquals(0, getInboxCount()); after the CompletableFuture.runAsync(this::sendEmail).join();.

Could you create the SmtpConnectionFactory with the debug enabled and paste the log?

        Properties props = new Properties();
        props.put("mail.debug", "true");

        SmtpConnectionFactory connectionFactory = SmtpConnectionFactoryBuilder
                .newSmtpBuilder()
                .host("mailtrap.io")
                .port(25)
                .protocol("smtp")
                .username("ca83413abb171c")
                .password("3ebe29d15ef77c")
                .session(props)
                .build();
nithril commented 7 years ago

Your initial logs are strange, especially the reset while you are sending an email: 11:04:59 AM - Client: RSET.

Maybe it relates to this serverfault question ?

tstavinoha commented 7 years ago

Interesting.. We do use Symantec at work. I will try it out tomorrow on a Linux environment and get back to you. Thanks for the link!

tstavinoha commented 7 years ago

I have disabled Symantec and everything started working! I don't think I would ever caught on to that without your help, Nicolas, so thank you very much! Closing this

nithril commented 7 years ago

You're welcome!