2016-02-29 46 views
1

我試圖連接到郵箱本地託管的電子郵件POP3郵箱和顯示電子郵件,但我不斷收到錯誤:連接到本地POP3郵箱的Java

Exception in thread "main" javax.mail.MessagingException: Connect failed; nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:209)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at com.kami.utils.MailClient.checkInbox(MailClient.java:33)
at com.kami.Main.main(Main.java:38)

我的類看起來是這樣的:

public class MailClient { 
    private String host; 
    private String username; 
    private String password; 
    private String provider; 
    protected Session session; 

    public MailClient() { 
     Properties props = new Properties(); 

     this.host = "localhost"; 
     this.username = "unix-user"; 
     this.password = "unix-password"; 
     this.provider = "pop3"; 

     this.session = Session.getDefaultInstance(props, null); 
    } 

    public void checkInbox() throws MessagingException, IOException { 
     Store store = session.getStore(provider); 
     store.connect(host, username, password); //This is line 33 
     Folder inbox = store.getFolder("inbox"); 
     inbox.open(Folder.READ_ONLY); 
     Message[] messages = inbox.getMessages(); 

     for(Message message : messages){ 
      System.out.println(message.getReceivedDate()); 
      System.out.println(message.getSubject()); 
     } 

     inbox.close(true); 
     store.close(); 
    } 
} 

據本地託管使用達夫科特IMAP/POP3服務器版本2.2.9和Postfix郵件服務器Postfix的版本2.11.0

+0

AFAIK java pop3實現用於連接的郵箱。您是否嘗試連接已下載的pop3郵箱? – Xvolks

+0

連接到一個本地託管的,就像我可以像這樣發送電子郵件:'props.put(「mail.smtp.host」,「localhost」);' – Shepard

+1

本地託管是讓我感到困惑的。你有一臺郵件服務器在你的linux系統上運行(pop3/pop3s服務正在運行)嗎? – Xvolks

回答

1

第一的telnet端口110的電子郵件服務器在您的計算機來檢查我服務在那裏運行。在我的筆記本電腦我沒有一個POP3服務器上運行,這就是結果:

[email protected]:~$ telnet localhost 110 
Trying 127.0.0.1... 
telnet: Unable to connect to remote host: Connection refused 

如果連接成功,遵循POP3的協議驗證與自己的數據:

[email protected]:~$ telnet mail.foo.com 110 
Trying X.X.X.X... 
Connected to mail.foo.com. 
Escape character is '^]'. 
+OK mail.foo.com POP3 server ready 
user fooUser 
+OK hello fooUser, please enter your password 
pass fooPassword 
+OK server ready 

在你的情況telnet localhost;還要注意你只應該發出命令:telnet,user和pass。其餘的是來自服務器的響應。

如果一切正常,問題出在你的java配置上,請檢查庫中的文檔和示例。

+0

就關於這個問題,它確實回答我,但它會馬上閉合連接: '遠程登錄本地主機110 試圖127.0.0.1 ... 連到本地主機。 轉義字符是'^]'。 外國主機關閉的連接。' 這是正常的嗎?我甚至無法檢查用戶。 – Shepard

+0

那麼如果你花太長時間,服務器會發出一個超時並關閉連接,你必須立即開始輸入,如果它保持關閉連接看看服務器日誌文件。在我的Linux機器上這工作正常,Windows終端不是那麼友好。 –

+0

感謝您的日誌檢查建議,顯然我得到'錯誤:服務(pop3登錄):命令啓動失敗,節流2秒鐘' – Shepard

0

以下方法將從pop郵箱獲取郵件(給定_Host = localhost,_User = unix-user,_Password = unix-password,_Protocol =「pop3」)。但是,您必須確定幾件事情: 1)「localhost」正在運行「pop3」服務器而不是「pop3s」(安全協議)服務器; 2)在「localhost」偵聽的默認端口上的「POP3」服務器 3)「UNIX用戶」擁有POP3郵箱

根據您的後續,好像你正在期待能夠從pop3帳戶發送郵件。這不是它的工作方式,因爲pop3只是一種檢索郵件的方式,而不是發送郵件。要發送郵件,您需要建立到SMTP服務器的單獨連接。

public Message[] getMessages(int maxCount) 
     throws MessagingException 
    { 
    // Get a Session object 
    Properties props = new Properties(); 
    Session session = Session.getInstance(props); 

    // Get a Store object 
    Store store = session.getStore(_protocol); 

    // Connect 
    store.connect(_host,_user,_password); 

    // Open a Folder 
    Folder folder = store.getFolder(_mailbox); 
    if (folder == null || !folder.exists()) 
     throw new ApplicationException("Invalid mailbox"); 

    //Gets up to maxCount messages from the pop box 
    folder.open(Folder.READ_WRITE); 
    Message[] messages = Monitor.EMPTY_MESSAGE_ARRAY; 
    int toMessageIndex=folder.getMessageCount(); 
    if (toMessageIndex > 0) { 
     if (toMessageIndex > maxCount) 
     toMessageIndex = maxCount; 
     messages = folder.getMessages(1,toMessageIndex); 
    } 

    // Go through all the new messages and make sure they are loaded. Use the outputStream 
    //to force all information to be downloaded. 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    for (int i = 0; i < messages.length && shouldRun(); i++) { 
     try { 
     //Force the download of all message information 
     bos.reset(); 
     messages[i].writeTo(bos); 
     getLog().enter(
      this, 
      "[readAndClearInBox] Read message to " + messages[i].getAllRecipients()[0].toString()); 

     } catch (Exception mex) { 
     getLog().error(this, mex, "[readAndClearInBox] Message exception"); 
     StringWriter sw = new StringWriter(); 
     PrintWriter pw = new PrintWriter(sw, true); 
     try { 
      Monitor.dumpEnvelope(getLog(), pw, messages[i]); 
     } catch (Exception ex) { 
      getLog().error(this, mex, "[readAndClearInBox] Could only display faulty message."); 
     } finally { 
      pw.flush(); 
      getLog().enter(this, "[readAndClearInBox]" + sw.toString()); 
     } 
     } finally { 
     //Mark the message for deletion 
     messages[i].setFlag(Flags.Flag.DELETED, true); 
     } 
    } 

    //Close folder and expunge all deleted messages, unless the read was aborted 
    if (shouldRun()) { 
     getLog().enter(this,"Found " + messages.length + " messages; closing inbox."); 
     folder.close(true); 
     store.close(); 
     return messages; 
    } else { 
     getLog().enter(this,"Found " + messages.length + " messages; closing inbox without expunging."); 
     folder.close(false); 
     store.close(); 
     _bShouldRun = true; 
     return Monitor.EMPTY_MESSAGE_ARRAY; 
    } 
    }