0
votes

Using the code below, I am unable to send an email. No exception is thrown but no emails are being sent. The code is called from within a servlet running in Apache Tomcat.

The code is primarily derived from an online example at http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/. The debug output is below the code.

  public static boolean sendEmail(String to, final String from, String subject, String emailMessage) {
    final String username = from;
    final String password = MyUtilities.getSystemPWD(from);
    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", MyConfiguration.smtpServer);
    props.put("mail.smtp.port", MyConfiguration.smtpPort);
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        message.setText(emailMessage);
        Transport.send(message);

        System.out.println("Done");
    } catch (MessagingException me) {
        MyLogger.log("MyUtilities.sendEmail: Messaging error", me);
        Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", me);
        System.out.println("MyUtilities.sendEmail: Messaging error");
        return false;
    } catch (Exception ex) {
        MyLogger.log("MyUtilities.sendEmail: Messaging error", ex);
        Logger.getLogger(MyUtilities.class.getName()).log(Level.SEVERE, "MyUtilities.sendEmail: Messaging error", ex);
        return false;
    }
    return true;
}

Debug Output

DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.mysite.com", port 587, isSSL false
220 smtp.mysite.net InterWorx-CP SMTP Server ESMTP
DEBUG SMTP: connected to host "mail.mysite.com", port: 587

EHLO 192.168.1.109
250-smtp.mysite.net InterWorx-CP SMTP Server
250-STARTTLS
250-SIZE 20971520
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
STARTTLS
220 ready for tls
EHLO 192.168.1.109
250-smtp.mysite.net InterWorx-CP SMTP Server
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-SIZE 20971520
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 ok
RCPT TO:<[email protected]>
250 ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 go ahead
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: 
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit    
<html></html>
.
250 ok 1381941483 qp 29906
QUIT
221 smtp.mysite.net InterWorx-CP SMTP Server
Done 
2
If a moderator can see this, when I try changing the SSL tag to tls it changes back to ssl.... - Usman Mutawakil
Just tried to change the tag from ssl to tls. Doesn't change - Keerthivasan

2 Answers

2
votes

From the debug output, it's clear that your mail server is accepting the message from your application. Thus, there's nothing wrong with your program or configuration. If the mail server is never delivering the message, you'll need to look at the mail server log files. Before that, look in the recipient's spam folder.

1
votes

I faced (almost) same issue, which was due to some additional/unwanted jars, pls refer to Send mail not working in Spring/Maven/Java 6 environment