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 = "[email protected]";
    private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    private static final String[] sendTo = { "[email protected]" };


    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("[email protected]", "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
7
  • What port do you use? Commented May 2, 2019 at 10:09
  • It depends on ttl or ssl solution (465 or 587). Commented May 2, 2019 at 10:09
  • tried out stackoverflow.com/questions/3649014/send-email-using-java Commented May 2, 2019 at 10:10
  • Is there any chance that you set up proxy in your app? Commented May 2, 2019 at 10:12
  • My application is one file app. With only public static void main method, nothing sophisticated yet. I want simply send small email and improve upon that Commented May 2, 2019 at 10:15

2 Answers 2

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:


Sign up to request clarification or add additional context in comments.

10 Comments

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.
I've tried your provided example, and it produces the same error. Have you tried that by yourself?
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?
tried telnet, c# and java on the same machine, I'haven't tried wireshark yet
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=<null> DEBUG SMTP: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
|
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!

1 Comment

Ouch!! That is definitely an environment problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.