Praneel PIDIKITI
Praneel PIDIKITI

Reputation: 19517

Unable to connect to my mail Server (java)

public class ConnectGmail {

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


    PasswordAuthentication authentication;

    Properties props = System.getProperties();
    props.setProperty("mail.store.protocol", "imap");

    Session session = Session.getDefaultInstance(props);
    Store store = session.getStore("imap");

    try {

      store.connect(host , user , "password");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Got this ERROR .. I almost tried with all the protocol

javax.mail.MessagingException: Connection refused: connect;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571)
    at javax.mail.Service.connect(Service.java:288)
    at javax.mail.Service.connect(Service.java:169)
    at ConnectGmail.main(ConnectGmail.java:26)

any help ??

Upvotes: 0

Views: 6186

Answers (2)

Ralph
Ralph

Reputation: 120781

You need to enable the mail authentification before you can use it:

Set property mail.smtp.auth to true to enalbe authentification.

If true, attempt to authenticate the user using the AUTH command. Defaults to false.

@see Javadoc of Package com.sun.mail.smtp

Upvotes: 1

Olivier Grégoire
Olivier Grégoire

Reputation: 35427

Does your ISP allow you to connect to that port?

Do you have to go through a proxy?

Try opening a direct connection to the mail server with the right port using a simple telnet <host> <port> command. If you can't then it's not your connection that fails, it's your ISP that disallows it. If you can, check if you use specific proxy options and add them in your program.

Upvotes: 0

Related Questions