0

I've seen all questions and answers about this problem, I've read the FAQ, tried all solutions which I've found, except writing my own one, I've turned off less secure app Gmail and so on. I've checked telnet connection, of course, tried to ping smtp.gmail.com and everything works fine. Even I've written (to be sure) small application on C# which took me 5 minutes (and this one works!). Please give me a clue how can I solve this issue. For every solution using ttl or ssl I get this stacktrace

    com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    at javax.mail.Service.connect(Service.java:366)
    at javax.mail.Service.connect(Service.java:246)
    at com.luga.culturalpickup.UsableSendMail.sendFromGMail(UsableSendMail.java:64)
    at com.luga.culturalpickup.UsableSendMail.main(UsableSendMail.java:84)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
    ... 5 more

I've disabled firewall completely.

Even I've used some wrappers around JavaMail Api to send letters and internally they were falling with this exception!

I want to send small email using java!


for exmaple, this code produce error specified above

public class GoogleTest {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "465";
    private static final String emailMsgTxt = "Test Message Contents";
    private static final String emailSubjectTxt = "A test from gmail";
    private static final String emailFromAddress = "mail4@gmail.com";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static final String[] sendTo = { "alex.95@mail.ru" };


    public static void main(String args[]) throws Exception {

        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

        new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt,
                emailMsgTxt, emailFromAddress);
        System.out.println("Sucessfully mail to All Users");
    }

    public void sendSSLMessage(String recipients[], String subject,
                               String message, String from) throws MessagingException {
        boolean debug = true;

        Properties props = new Properties();
        props.put("mail.smtp.host", SMTP_HOST_NAME);
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.port", SMTP_PORT);
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("mail4@gmail.com", "password");
                    }
                });

        session.setDebug(debug);

        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);

        InternetAddress[] addressTo = new InternetAddress[recipients.length];
        for (int i = 0; i < recipients.length; i++) {
            addressTo[i] = new InternetAddress(recipients[i]);
        }
        msg.setRecipients(Message.RecipientType.TO, addressTo);

        // Setting the Subject and Content Type
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        Transport.send(msg);
    }
}

I get following stacktrace

    DEBUG: JavaMail version 1.6.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
Exception in thread "main" com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    at javax.mail.Service.connect(Service.java:366)
    at javax.mail.Service.connect(Service.java:246)
    at javax.mail.Service.connect(Service.java:267)
    at javax.mail.Transport.send0(Transport.java:252)
    at javax.mail.Transport.send(Transport.java:174)
    at com.luga.culturalpickup.GoogleTest.sendSSLMessage(GoogleTest.java:72)
    at com.luga.culturalpickup.GoogleTest.main(GoogleTest.java:29)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.Socket.connect(Socket.java:538)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:359)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
    ... 8 more
Alex
  • 3,923
  • 3
  • 25
  • 43

2 Answers2

1

The configuration details look OK to me.

The big clue is that you are getting "Connection refused". In theory, there are a few possible explanations for this:

  • The smtp.gmail.com servers are all down. This is highly unlikely.
  • The smtp.gmail.com servers may have black-listed your IP address or a network range containing your IP address. (Perhaps the gmail.com servers have been getting too much SPAM via your ISP.)
  • Maybe there is a country-wide block on emails. (For example, it has been claimed that China does this sometimes.)
  • Maybe ISP is blocking the connection to smtp.gmail.com on those ports ... or maybe all outbound connections on this ports. This may be done to prevent spamming by your ISP's customers. Check your ISP's terms and conditions regarding sending emails, and check for information on how you should do it.
  • Maybe your organization is blocking the connection to smtp.gmail.com. This could be done so that they can monitor all out-going emails. It could also be done to prevent spamming or behavior that could be interpreted as spamming. Check with your local IT and network managers.
  • Maybe it is your machine's internal firewall. (Though you say that you disabled it ...)

There is one simple test you can do to validate this. Use the "telnet" command to try to connect to smtp.gmail.com on those two ports. If you get a "Connection refused", it is strong evidence of some kind of blocking somewhere.

Finally, if the ports appear to be open, check what you are doing against this tutorial page:


Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Well the next step would be to use something like Wireshark to spot the differences in the network traffic between the C# and Java versions of the code. – Stephen C May 02 '19 at 11:34
  • I've tried your provided example, and it produces the same error. Have you tried that by yourself? – Alex May 02 '19 at 11:39
  • I don't see why it would be wrong. If it was wrong, he would have corrected it. In fact, this points to this being a problem in your local environment. Have you tried WireShark yet? Have you tried the telnet test yet? Are you running the C# and Java examples on the same machine? – Stephen C May 02 '19 at 11:55
  • tried telnet, c# and java on the same machine, I'haven't tried wireshark yet – Alex May 02 '19 at 12:11
  • his example produces such log `DEBUG: JavaMail version 1.6.2 DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: need username and password for authentication DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=79217, password= DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false` – Alex May 02 '19 at 12:14
  • this part say about erroneous `DEBUG SMTP: need username and password for authentication DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=79217, password=` – Alex May 02 '19 at 12:15
  • That is different to what you are getting with your Java code. His code has **succeeded** in opening a TCP connection to gmail ... and gotten to the point where it need user credentials. – Stephen C May 02 '19 at 12:18
  • Nope, actually, it because of his code uses the send method of 1.6 version, where password and user should be specified as parameters, but not as auntificator instance. Stack trace are indentical, here I've shown only log messages. – Alex May 02 '19 at 12:23
  • In that case, it just reiterates my previous point that it is something in your environment. Use Wireshark – Stephen C May 02 '19 at 12:35
  • Stephen, see my answer! and thank you again for your help! – Alex May 02 '19 at 14:48
0

After one and a half of day I've found a problem. It was due to avg antivirus or to say it more correctly avg virus. It was turned off (as he has displayed me), but I've noticed some avg advertisment attached to messages sent with application written on C#, I've found a way to definitely turn it off. And it works now!

Alex
  • 3,923
  • 3
  • 25
  • 43