Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

javax.mail.MessagingException: Exception reading response; using java mail api 1.5.4 version

Saroja Kandepuneni-OracleJul 5 2016 — edited Jul 11 2016

Hi,

I am using the below code to connect to SMTP server which is SSL enabled and send a test email.

---------------------------------------------------------------------------------------------------------------------------------------------------------

import java.util.Date;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.Authenticator;

import javax.mail.Message;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

/*

  This class is a Standalone utility to check connection with SMTP server.

*/

public class SMTPTLSTest

{  

  public static void main(String args[] )   

  {

     Session session;    

     String smtpServer = args[0];

     String port = args[1];

     String userName = args[2];

     String password = args[3];

     String fromAddr = args[4];

     String toAddr = args[5];

     String truststore = args[6];

     Address[] addresses = null;

     MimeMessage mimeMessage = null;

     Properties smtpProperties = new Properties();

    

     smtpProperties.put("mail.transport.protocol", "smtp");

     smtpProperties.put("mail.smtp.host", smtpServer);

     smtpProperties.put("mail.smtp.port", port);

     smtpProperties.put("mail.smtp.auth", "true");   

     smtpProperties.put("mail.smtp.user", userName);

     smtpProperties.put("mail.smtp.password", password); 

     smtpProperties.put("mail.smtp.connectiontimeout", 60000); 

     smtpProperties.put("mail.smtp.timeout", 60000);

     System.getProperties().setProperty("java.security.debug", "ssl");

     System.getProperties().setProperty("javax.net.debug", "all");

     System.setProperty("javax.net.ssl.trustStore", truststore);

     System.getProperties().setProperty("java.security.debug","all");

     smtpProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

     smtpProperties.put("mail.smtp.socketFactory.fallback", "false");

     smtpProperties.put("mail.smtp.socketFactory.port", port);

     System.out.println("SMTPTLSTest. The given smtp host name, port, username and password values are:" + smtpServer

                       + "," + port + "," + userName + "," + password );

     System.out.println("SMTPTLSTest. The given From address and Recipient email addresses are:" + fromAddr

                       + "," + toAddr);

    

     try

     {

       Authenticator authenticator = new TestAuthenticator(userName, password);

    

       session = Session.getInstance(smtpProperties, authenticator);

       mimeMessage = new MimeMessage(session);

       System.out.println("SMTPTLSTest. Obtained the session object.");

       Address[] from = InternetAddress.parse(fromAddr, false);

       mimeMessage.addFrom(from);

       mimeMessage.setRecipients(Message.RecipientType.TO, toAddr);

       mimeMessage.setSubject("Test mail from outlook365");

       mimeMessage.setText("This is just a test mail to test SMTP connection\n");

       mimeMessage.setSentDate(new Date());

       addresses = InternetAddress.parse(toAddr, false);

       mimeMessage.setFrom(from[0]);

      

       System.out.println("SMTPTLSTest. Sending test email to " + toAddr + " ...");

       Transport.send(mimeMessage, addresses);

       System.out.println("SMTPTLSTest. Message has been sent");

    }

    catch(Exception ex)

    {

       System.out.println("SMTPTLSTest. Exception occured while sending message:"+ex);

       ex.printStackTrace();

    }

       

  } // end of main ()

 

} // end of class

class TestAuthenticator extends Authenticator

{

  String username;

  String password;

  TestAuthenticator(String name, String passwd)

  {

     username = name;

     password = passwd;

  }

  public PasswordAuthentication getPasswordAuthentication()

  {

     return new PasswordAuthentication(username, password);      

  }   

}

---------------------------------------------------------------------------------------------------------------------------------------------

It was throwing the below error using java mail API 1.5.4 version..

javax.mail.MessagingException: Exception reading response;

  nested exception is:

        java.net.SocketException: Connection reset

        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2305)

        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2032)

        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)

        at javax.mail.Service.connect(Service.java:386)

        at javax.mail.Service.connect(Service.java:245)

        at javax.mail.Service.connect(Service.java:194)

        at javax.mail.Transport.send0(Transport.java:253)

        at javax.mail.Transport.send(Transport.java:146)

        at SMTPTLSTest.main(SMTPTLSTest.java:96)

Caused by: java.net.SocketException: Connection reset

        at java.net.SocketInputStream.read(SocketInputStream.java:196)

        at java.net.SocketInputStream.read(SocketInputStream.java:122)

        at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)

        at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)

        at java.io.BufferedInputStream.read(BufferedInputStream.java:254)

        at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:92)

        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2285)

        ... 8 more

The same code is working fine with java mail API 1.4.3 version.

Is there any changes with the java mail API version 1.5.4?

What is the issue with this code? Please provide pointers to resolve this issue.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2016
Added on Jul 5 2016
1 comment
6,114 views