我正在尝试使用JavaMail连接到邮件服务器。我收到了一个例外。
我也尝试过使用POP3,但没有运气。
错误:
javax.mail.MessagingException: Connection timed out: connect
这是由以下原因引起的:
java.net.ConnectException: Connection timed out: connect
我的代码:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getDefaultInstance(props, null);
try
{
Store store = session.getStore("imaps");
MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
socketFactory.setTrustAllHosts(true);
props.put("mail.imaps.ssl.socketFactory", socketFactory);
store.connect(host,user,password);
}
catch (MessagingException ex){
System.out.println(ex.toString());
}
Properties properties = new Properties();
properties.put("mail.pop3.host", host);
properties.put("mail.pop3.port", port);
// SSL setting
properties.setProperty("mail.pop3.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.pop3.socketFactory.fallback", "false");
properties.setProperty("mail.pop3.socketFactory.port", String.valueOf(port));
Session session = Session.getDefaultInstance(properties);
// connects to the message store
Store store = session.getStore("pop3");
store.connect(user, password);
System.out.println(store.isConnected());
System.out.println("connection succesful");
// opens the inbox folder
Folder folderInbox = store.getFolder("INBOX");
folderInbox.open(Folder.READ_ONLY);
答案 0 :(得分:0)
我能够使用下面建立连接。我的不好之前我曾经使用过不合适的主人。
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
Session session = Session.getInstance(props, null);
session.setDebug(true);
Store store = session.getStore("imaps");
MailSSLSocketFactory socketFactory = new MailSSLSocketFactory();
socketFactory.setTrustAllHosts(true);
props.put("mail.imaps.ssl.socketFactory", socketFactory);
System.out.println("store is "+store.isConnected());
store.connect("mail.abc.com","xyz@abc.com", "******");
System.out.println("Now the store is "+store.isConnected());