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
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.
精彩评论