2014-10-10 64 views
3

我想用clojure解析IMAP收件箱中的郵件。如何使用clojure閱讀我的電子郵件?

我知道有clojure-mail庫,但我沒有gmail帳戶。謝謝!

+3

有沒有爲Java IMAP圖書館的不足。 – 2014-10-10 21:52:22

+0

@CharlesDuffy使用java庫的clojure答案也可能很酷。 – leontalbot 2014-10-11 03:38:16

+0

@LeonidBeschastny,OP已經知道,甚至在問題中提到,但不能用於gmail專用。 – 2014-10-11 19:02:15

回答

5

Apache Commons IMAP客戶端庫是一個合理的選擇。的their example program核心是平凡複製在遠低於20日線的Clojure的,如果不與故障處理麻煩:

(ns mail-client.core 
    (:import [org.apache.commons.net PrintCommandListener] 
      [org.apache.commons.net.imap IMAPClient])) 

(defn get-mail [server username password] 
    (doto (IMAPClient.) 
    (.setDefaultTimeout 60000) 
    (.addProtocolCommandListener (PrintCommandListener. System/out true)) 
    (.connect server) 
    (.login username password) 
    (.setSoTimeout 6000) 
    (.capability) 
    (.select "inbox") 
    (.examine "inbox") 
    (.status "inbox" (into-array String ["MESSAGES"])) 
    (.logout) 
    (.disconnect))) 
+0

謝謝!我用':import'替換了':include'。我怎麼能指定一個端口號?謝謝! – leontalbot 2014-10-18 03:33:20

+0

如果你看文檔(http://commons.apache.org/proper/commons-net/apidocs/ org/apache/commons/net/imap/IMAPClient.html),你會發現'connect'方法是從'org.apache.commons.net.SocketClient'繼承的,所以有一個2-argument形式接受端口號 – 2014-10-18 03:42:01

+0

...當然,如果您的端口號是通過SSL連接的端口號,則需要使用IMAPSClient而不是IMAPClient。上游文檔值得熟悉。 – 2014-10-18 03:43:07