javaemailsimple-java-mail

Mailer.sendMail(email); never returns after successfully both sending emails and receiving them


I'm trying to send emails from my app with following simplejavamail implementation:

Email email = EmailBuilder.startingBlank()
        .from("from_name", "my_email@yahoo.com")
        .to("from_name", "my_email@yahoo.com")          //to myself
        .to("recipient2_name", "mail_2@gmail.com")
        .withSubject("subject title")
        .withPlainText("Hello World!!")
        .buildEmail();

Mailer mailer = MailerBuilder
    .withSMTPServer("smtp.mail.yahoo.com", 465, "my_email@yahoo.com", "my_password")
    .withTransportStrategy(TransportStrategy.SMTPS)
    //.async()
    .buildMailer();
    //.sendMail(email);
    //.sendMail(email, true);

try {
    mailer.sendMail(email);
} catch (MailException e) {
    e.printStackTrace();
}

System.out.println("Exiting...");

And it successfully sends all my (two) emails: I know this because I'm receiving (both) emails on my usual email clients. However, it never returns from Mailer.sendMail(email); ,i.e., "Exiting..." is never displayed.

Following my app requirements, I really don't care if this is executed either sync or async. Still, I would expect my routine eventually to return and, somehow, proceed execution.

So, any idea how to make my routine return from Mailer.sendMail(email) and continue execution?

P.S. I'm also getting some warnings during runtime:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.mail.util.SocketFetcher (file:/F:/CW/workspace/Email_01/lib/jakarta.mail-1.6.5.jar) to method sun.security.util.HostnameChecker.getInstance(byte)
WARNING: Please consider reporting this to the maintainers of com.sun.mail.util.SocketFetcher
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

As these are just warnings, I'm not really worried, or should I?


Solution

  • After some additional digging, I think I found the issue:

    1. First, and perhaps most important, the execution is indeed displaying "Exiting...", meaning it is returning from Mailer.sendMail(email) and continue execution. Somehow, perhaps because of the various warnings and the fact that my VM never died, I missed this important piece of information and just assumed my app to hang. My bad :-(

    2. Anyway, I still think there's a (known) bug (simple-java-mail-6.4.4). Following a somewhat unrelated bug discussion thread (https://github.com/bbottema/simple-java-mail/issues/204) a comment from bbottema may explain the extent of my issue:

    "I found that when sending mails from static void main (and perhaps junit test), the java process wouldn't return after sending the email and simply hang. Closing the connection pool solved that issue at the time."

    In fact, as I was just testing the library concept, I was sending mails from static void main. Besides, "the java process wouldn't return after sending the email and simply hang". I thus assume this just to be a known bug.
    Also, as suggested, I finally solved the issue by calling: mailer.shutdownConnectionPool();

    Thank you for your time!