Monday, August 1, 2011

Apache Commons Exception -- Must issue a STARTTLS command first

While working on Apache Commons you may experience that email Client example provided at Apache's site may work in a simple java main method but denie to work on your web server (Tomcat).

Below is the example provided at http://commons.apache.org/email/userguide.html.


Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setTLS(true);
email.setFrom("user@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("foo@bar.com");
email.send();



You may experience the below error while executing this code in any web server like Tomcat.


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


RSET
250 2.1.5 Flushed e6sm6270355pbm.7
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. e6sm6270355pbm.7

        at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1668)
        at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1207)
        at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:735)
        at javax.mail.Transport.send(Transport.java:95)
        at javax.mail.Transport.send(Transport.java:48)
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
        at org.apache.commons.mail.Email.send(Email.java:1267)
        at org.artstor.adam.service.util.EmailClient.sendEmail(EmailClient.java:65)
        at org.artstor.adam.service.thread.ExportLaunchThread.sendEmails(ExportLaunchThread.java:248)
        at org.artstor.adam.service.thread.ExportLaunchThread.run(ExportLaunchThread.java:215)
        at org.artstor.adam.service.thread.ThreadPool$WorkerThread.run(ThreadPool.java:144)
QUIT
221 2.0.0 closing connection e6sm6270355pbm.7
DEBUG SMTP: useEhlo true, useAuth false


The main reason for this issue is that properties for port cannot be set properly for the client.
below change in code can save few minutes of your life.


            Email email = new SimpleEmail();
            email.setHostName("smtp.gmail.com");
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator("username", "password"));
            email.setTLS(true);
            email.setFrom("me@me.com");
            email.setSubject("some subject");
            email.setMsg("some message");
            email.setDebug(true);

            email.getMailSession().getProperties().put("mail.smtp.auth", "true");
            email.getMailSession().getProperties().put("mail.debug", "true");
            email.getMailSession().getProperties().put("mail.smtp.port""587");
            email.getMailSession().getProperties().put("mail.smtp.socketFactory.port""587");
            email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
            email.addTo("someone@mail.com");
            email.send();




I hope it helps.

Happy Coding



No comments: