19

Running this Java service on my laptop (Windows 10) or on our development server (CentOS) everything works as expected. But when I run it on our live server (CentOS) I get the following error:

09/Sep/2016 08:31:07,005 [ERROR] [pool-2-thread-2] - EmailSender: A Messaging exception occurred in EmailSender
javax.mail.MessagingException: [EOF]
        at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363) ~[jar:rsrc:mail-1.4.jar!/:?]
        at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838) ~[jar:rsrc:mail-1.4.jar!/:?]
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375) ~[jar:rsrc:mail-1.4.jar!/:?]
        at javax.mail.Service.connect(Service.java:275) ~[jar:rsrc:mail-1.4.jar!/:?]
        at javax.mail.Service.connect(Service.java:156) ~[jar:rsrc:mail-1.4.jar!/:?]
        at com.awesomecompany.messaging.server.EmailSender.sendEmail(EmailSender.java:99) [rsrc:./:?]
        at com.awesomecompany.messaging.server.MonitorDevicesRunnable.run(MonitorDevicesRunnable.java:82) [rsrc:./:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_75]
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304) [?:1.7.0_75]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) [?:1.7.0_75]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:1.7.0_75]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [?:1.7.0_75]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [?:1.7.0_75]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_75]

My email code:

public void sendEmail(User user) {

        to[0] = user.getEmail();
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.debug", "true");
        props.put("mail.store.protocol", "imap");
        subject = user.getEmailBuilder().getEmailSubject();

        try {

            Session session = Session.getDefaultInstance(props,
                    new Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(from,
                                    password);
                        }
                    });

            MimeMessage message = new MimeMessage(session) {
                protected void updateMessageID() throws MessagingException {
                    if (getHeader("Message-ID") == null)
                        super.updateMessageID();
                }
            };
            message.setFrom(new InternetAddress(from));

            InternetAddress[] to_addresses = new InternetAddress[to.length];

            for (int i = 0; i < to.length; i++) {
                to_addresses[i] = new InternetAddress(to[i]);

            }

            message.addRecipients(javax.mail.Message.RecipientType.TO,
                    to_addresses);

            String messageHtml = user.getEmailMessage();
            message.setSubject(subject);
            message.setContent(messageHtml, "text/html;charset=UTF-8;");
            message.saveChanges();
            user.setEmailMessageId(message.getMessageID());
            log.info("Email message ID: " + message.getMessageID());
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            user.setEmailStatus(DistinctNotification.STATUS_SENT);

        } catch (AddressException e) {

            log.error("An AddressException occurred in EmailSender", e);
            user.setEmailStatus(DistinctNotification.STATUS_READY);
        } catch (MessagingException e) {

            log.error("A Messaging exception occurred in EmailSender", e);
            user.setEmailStatus(DistinctNotification.STATUS_READY);
        }

    }

The error occurs at this line:

transport.connect(host, from, password);

EDIT:

I reverted to an earlier build that I know for a fact worked, maybe a few months ago. It no longer works, I get the same error.
My other colleagues who have access to the server say they haven't made any changes to it.

I have absolutely no idea where to go with this. I have no problem contacting googles servers using telnet. I'm going to do more checking on various ports just to be sure, but I don't thing that's the issue. EDIT 2:

Here's the issue: 501-5.5.4 Empty HELO/EHLO argument not allowed, closing connection.
But why would this be happening only on the one server?

Additional logs from the console:

DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO
501-5.5.4 Empty HELO/EHLO argument not allowed, closing connection.
501 5.5.4  https://support.google.com/mail/?p=helo l10sm686448lfd.19 - gsmtp
HELO
DEBUG SMTP: EOF: [EOF]
09:07:00.277 [pool-2-thread-1] - A Messaging exception occurred in EmailSender
javax.mail.MessagingException: [EOF]
        at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363) ~[jar:rsrc:mail-1.4.jar!/:?]
        at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838) ~[jar:rsrc:mail-1.4.jar!/:?]
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375) ~[jar:rsrc:mail-1.4.jar!/:?]
        at javax.mail.Service.connect(Service.java:275) ~[jar:rsrc:mail-1.4.jar!/:?]
        at javax.mail.Service.connect(Service.java:156) ~[jar:rsrc:mail-1.4.jar!/:?]
        at com.watersprint.messaging.server.EmailSender.sendEmail(EmailSender.java:99) [rsrc:./:?]
        at com.watersprint.messaging.server.MonitorDevicesRunnable.run(MonitorDevicesRunnable.java:82) [rsrc:./:?]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_75]
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304) [?:1.7.0_75]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178) [?:1.7.0_75]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:1.7.0_75]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [?:1.7.0_75]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [?:1.7.0_75]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_75]
2016-09-15 09:07:00,760 ERROR Error occurred while sending e-mail notification. javax.mail.MessagingException: [EOF]
        at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363)
        at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:241)
        at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:150)
        at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:173)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:99)
        at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:430)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:409)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:367)
        at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:112)
        at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:727)
        at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:716)
        at org.apache.logging.log4j.spi.AbstractLogger.error(AbstractLogger.java:354)
        at com.watersprint.messaging.server.EmailSender.sendEmail(EmailSender.java:110)
        at com.watersprint.messaging.server.MonitorDevicesRunnable.run(MonitorDevicesRunnable.java:82)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

2016-09-15 09:07:00,762 ERROR An exception occurred processing Appender SMTPAppender org.apache.logging.log4j.LoggingException: Error occurred while sending email
        at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:153)
        at org.apache.logging.log4j.core.appender.SmtpAppender.append(SmtpAppender.java:173)
        at org.apache.logging.log4j.core.config.AppenderControl.callAppender(AppenderControl.java:99)
        at org.apache.logging.log4j.core.config.LoggerConfig.callAppenders(LoggerConfig.java:430)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:409)
        at org.apache.logging.log4j.core.config.LoggerConfig.log(LoggerConfig.java:367)
        at org.apache.logging.log4j.core.Logger.logMessage(Logger.java:112)
        at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:727)
        at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:716)
        at org.apache.logging.log4j.spi.AbstractLogger.error(AbstractLogger.java:354)
        at com.watersprint.messaging.server.EmailSender.sendEmail(EmailSender.java:110)
        at com.watersprint.messaging.server.MonitorDevicesRunnable.run(MonitorDevicesRunnable.java:82)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)
Caused by: javax.mail.MessagingException: [EOF]
        at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1363)
        at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:838)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:375)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)
        at javax.mail.Transport.send0(Transport.java:168)
        at javax.mail.Transport.send(Transport.java:98)
        at org.apache.logging.log4j.core.net.SmtpManager.sendMultipartMessage(SmtpManager.java:241)
        at org.apache.logging.log4j.core.net.SmtpManager.sendEvents(SmtpManager.java:150)
        ... 18 more
10
  • 1
    What does the JavaMail debug output show? Is there a firewall on your live server? Try the connection debugging tips in the JavaMail FAQ. Commented Sep 9, 2016 at 22:03
  • 1
    The above error is the only exception in the logs. Is there another way to specifically see JavaMail debug output? I thought the above error was it. I'm able to connect to googles mail servers with telnet, so it doesn't seem like a firewall issue or anything. Also, older versions of this email service have worked, and I don't believe I have changed anything in that regard. My initial thought is it must be something on the server itself, since the code works on other servers. But I can't see where the issue is.
    – mal
    Commented Sep 12, 2016 at 6:10
  • 1
    You mean another way besides the way in the link above? Well, you can configure java.util.logging; the logger details are on the JavaMail javadocs pages for each package. If you're not getting any debug output, you need to figure out where System.out is going. You should also fix these common JavaMail mistakes, which may fix some of your problems. Commented Sep 12, 2016 at 6:52
  • 1
    I'm getting my logging output using Log4j2. To the best of my knowledge I am already getting all the console output, and the above error is the only JavaMail error I'm seeing. Thanks for the links, I'll go through them and double check everything.
    – mal
    Commented Sep 12, 2016 at 6:56
  • 1
    OK this has to be some sort of server issue I think; I just ran a build of the email service that I made earlier this year and I know for a fact that that build worked on the live server. Something in the configuration must have changed somewhere, or in the routers or something. dammit!
    – mal
    Commented Sep 12, 2016 at 9:22

2 Answers 2

12

Somehow the server $HOSTNAME was deleted from /etc/hosts. As a result of that googles servers received a null or an empty value as the hostname and rejected the connection.

1
  • 1
    You saved my day! Thanks you so much! Tried other email services as well, but they didn't work before changing host name.
    – MewX
    Commented Sep 29, 2017 at 7:36
0

In my case it didn't work even when the hostname value was set to some dummy name, example: (abc.abc.com didn't work).
It only worked when I set the hostname to a value that is resolvable by the DNS example: (google.com worked!)

Not the answer you're looking for? Browse other questions tagged or ask your own question.