以下是使用SSL连接POP3的代码。
public void pop3 ()throws Exception{
// connect to my pop3 inbox
String a = "javax.net.ssl.SSLSocketFactory";
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.setProperty("mail.pop3.host" , "myhost");
props.setProperty("mail.pop3.port" , "myport");
props.setProperty("mail.pop3.user" , "username");
// Start SSL connection
props.setProperty("mail.pop3.ssl.enable", "true");
props.put("mail.pop3.socketFactory" , "myport");
props.setProperty("mail.pop3.socketFactory.class" , a );
Session session = Session.getInstance(props);
try {
Store emailStore = session.getStore("pop3s");
// getting error while connecting
emailStore.connect("myhost",port,"username", "password");
Folder inbox = emailStore.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
// get the list of inbox messages
Message[] messages = inbox.getMessages();
if (messages.length == 0) System.out.println("No messages found.");
for (int i = 0; i < messages.length; i++) {
// stop after listing ten messages
if (i > 10) {
System.exit(0);
inbox.close(true);
emailStore.close();
}
Log.e("Message " , String.valueOf((i + 1)));
Log.e("From : " , String.valueOf(messages[i].getFrom()[0]));
Log.e("Subject : " ,messages[i].getSubject());
Log.e("Sent Date : " , String.valueOf(messages[i].getSentDate()));
}
inbox.close(true);
emailStore.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
每次我收到以下错误:
W / System.err:javax.mail.MessagingException:Connect failed;
W / System.err:嵌套异常是:
W / System.err:java.net.ConnectException:拒绝连接
W / System.err:at
com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:161)
.....
答案 0 :(得分:0)
摆脱所有套接字工厂的东西,you don't need it。
您正在设置“pop3”协议的属性,但您正在使用“pop3s”协议。使用“pop3”协议并将mail.pop3.ssl.enable
属性设置为true
更简单。