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 ??
解决方案
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