1

次のコードを使用して、Clojure からメールを送信しようとしています。

メールを送信するヘルパー関数:

(defn- send [recipient from subject msg host content-type & attachments]
  (let [att-ds (map (fn [at] {:ds (ByteArrayDataSource. (:source at)
                                                        (:type at))
                              :name (:name at)
                              :description (:description at)})
                    attachments)
        mpmail (MultiPartEmail.)]
    (doto mpmail
      (.setHostName host)
      (.setFrom from)
      (.setSubject subject)
      (.setContent msg content-type)
      (.setCharset "utf-8"))
    (.addTo mpmail recipient)
    (doseq [ds att-ds]
      (.attach mpmail (:ds ds) (:name ds) (:description ds)))
    (.send mpmail)))

使用法:

(send "sender@my.domain" 
      "recipient@my.domain" 
      "SUBJECT"
      "MSG"
      "my.smtp.server"
      "text/plain"
      {:source (.getBytes "Attachment") 
      :type "text/plain"
      :name "test.txt"
      :description "test"})

REPL (または私のアプリ) から上記を実行すると、recipient@my.domain が件名「SUBJECT」、本文「MSG」の電子メールを受信しますが、添付ファイルの痕跡はありません。どこにも例外は発生しません。

2 つの異なる smtp サーバーでこれを試しました。

助けてくれてありがとう。

4

2 に答える 2

3

に置き換え(.setContent msg)てみてください(.setMsg msg)。呼び出しsetContentたときに、手動でコンテンツを設定したと見なされ、次のattachメソッドが無視される場合があります。

于 2012-07-30T12:06:05.733 に答える