3

I'm trying to send emails through JavaMail API but I end up receiving the SocketException: Connection reset.

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class SendMailSSL {
 public static void main(String[] args) {

Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
    "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

     Authenticator auth = new Authenticator() {

             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication("userName@gmail.com","gmailPassword");
             }
         };

    Session session = Session.getDefaultInstance(props,auth);


    try {

    Message message = new MimeMessage(session);
    Address sender  = new InternetAddress("any@...");
    message.setFrom(sender);

    String recipients = "email1@...,email2@...,email2@...";
    String[] toList = recipients.split(",");
    System.out.println(toList.length);
    Address[] addressTo = new InternetAddress[toList.length];

    for(int i = 0; i < toList.length; i++){
    addressTo[i] = new InternetAddress(toList[i]);  
    }

    for( int i=0; i < addressTo.length; i++) { // changed from a while loop
            message.addRecipient(Message.RecipientType.TO, addressTo[i]);
        }


    message.setSubject("Testing Subject 5");
    message.setText("Dear Message ," +
    "\n\n HELLO, please! \n https://192.168.192.120:8181/centralWeb");
    System.out.println("SENDING MAIL......... " + new Date().toString());

    message.setHeader("Content-type", "text/html; charset=UTF-8");
    Transport.send(message);

    System.out.println("Done  " + new Date().toString());

    } catch (MessagingException e) {
    throw new RuntimeException(e);
    }

 }
}

NetBeans Output:

    1
SENDING MAIL......... Sun Apr 28 01:30:18 IST 2013
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    java.net.SocketException: Connection reset
    at sendmailssl.SendMailSSL.main(SendMailSSL.java:65)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    java.net.SocketException: Connection reset
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at sendmailssl.SendMailSSL.main(SendMailSSL.java:60)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:189)
    at java.net.SocketInputStream.read(SocketInputStream.java:121)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:312)
    at sun.security.ssl.InputRecord.read(InputRecord.java:350)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1328)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:503)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:234)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
    ... 7 more
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

I've tried disabling IPv6 and turning off my firewall too, but the problem persists. I am using Windows 7 x64, if that matters.

Thanks to anyone who can help me fix this issue.

user2327683
  • 41
  • 1
  • 2
  • 4
  • `Transport.send(..)` look suspicious. Where in the code did you connect to the server? See the code in http://stackoverflow.com/a/1990581/336802 – Christian Garbin Apr 28 '13 at 00:02
  • @chr Yeah you're right. Surprisingly, the code does work for some reason. (It was a stupid smtp outgoing filter in my antivirus that was causing the socket exception). Thanks for pointing it out, I'll add the code you pointed out. – user2327683 Apr 28 '13 at 15:37

1 Answers1

2

Try these debugging tips from the JavaMail FAQ:

Also, you might want to correct these common mistakes, although I don't think they're related to your problem.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • 2
    I actually have some of the mistakes mentioned, in the link, in my code. Thank you I'll revise my code. I got it work, by the way. It turns out it was a smtp outgoing filter in my antivirus that was blocking the connections. – user2327683 Apr 28 '13 at 15:40
  • 1
    ANTIVIRUS was the issue for me too...! – HaveAGuess May 30 '13 at 23:31