2012-04-27 86 views
3

我試圖將我的應用程序連接到Gmail以檢查電子郵件。我必須爲POP3使用SSL。Javamail和Gmail Pop3 SSL

這是我的代碼:

Properties props = new Properties(); 

    props.put("mail.host", "pop.gmail.com"); 
    props.put("mail.store.protocol", "pop3s"); 
    props.put("mail.pop3s.auth", "true"); 
    props.put("mail.pop3s.port", "993"); 

    Session session = Session.getDefaultInstance(props, null); 
    Store store=session.getStore(); 
    store.connect("[email protected]","mypass"); 

而且我得到這個錯誤:

Exception in thread "main" javax.mail.MessagingException: Connect failed; 
    nested exception is: 
    java.io.IOException: Unexpected response: * OK Gimap ready for requests from x.x.x.x.x z50if25691877wef.13 
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:210) 
    at javax.mail.Service.connect(Service.java:295) 
    at javax.mail.Service.connect(Service.java:176) 
    at javax.mail.Service.connect(Service.java:196) 

我認爲這具有良好的新:gmail的回答,但服務器...好像回答javamail的一個不好的方法。

+0

如果找到此上javax.mail源的Protocol.java: 如果(line.startsWith( 「+ OK」))r.ok = TRUE;而谷歌回答「*確定」... – Tobia 2012-04-27 15:43:18

+0

首先,將Session.getDefaultInstance更改爲Session.getInstance。然後,我想如果你看會話調試輸出,你會發現你實際上連接到imap.gmail.com。 POP3訪問的正確主機名是pop.gmail.com。另請注意,如果您使用默認值,則不需要設置mail.pop3s.port屬性,並且沒有mail.pop3s.auth屬性,因此無需設置它。查看JavaMail FAQ獲取完整示例。 – 2012-04-27 19:27:50

回答