'clj-imap-observer' is a Clojure library which lets you create a
com.sun.mail.imap.IdleManager
in a more functional way.
[com.markusaschl/clj-imap-observer "0.3.0"]
(ns com.example.your-application
(:require [com.markusaschl.imap-observer :as imap-observer]))
Write something like this into
<your-project>/ressources/mail-properties.edn
:
{:host "example.com",
:username "user@example.com",
:password "password",
:imap-protocol "imaps",
:session-properties
{"mail.event.scope" "application"}}
(def observer (imap-observer/make-observer
(->> (clojure.java.io/resource "mail-properties.edn")
slurp
clojure.edn/read-string)
[{:name "INBOX"
:mode (:read-write imap-observer/folder-modes)
:on-folder-opened
(fn [folder-obj] (def ^:dynamic *folder* folder-obj))
:on-message-added
(fn [messages]
(def ^:dynamic *messages* messages))}]))
The function imap-observer/make-observer
takes two arguments,
config-options
and folder-options.
'config-options' is a map, which uses these keys:
:imap-protocol
:host
:username
:password
:session-properties
':session-properties' is also a map, which is then converted into a
java.util.Property
object, which in turn is passed to
jakarta.mail.Session/getDefaultInstance
. That happens in imap-observer/session
For a list of all the properties you can use, see the com.sun.mail.imap documentation.
':folder-options' is an array consisting of a map made of:
:name
:mode
:on-message-added
:on-message-removed
:on-folder-opened
For example:
[{:name "INBOX"
:mode (:read-write com.markusaschl.imap-observer/folder-modes)
:on-message-added (fn [messages])}]
:mode(s)
are jakarta.mail.Folder/READ_ONLY
or
jakarta.mail.Folder/READ_WRITE
. For convenience reasons, those two are
exported as a map in imap-observer/folder-modes
:name
is the name of an existing mailbox folder:on-messsage-added
and :on-message-removed
are
called with an array of jakarta.mail.Message
. See also
jakarta.mail.Message:on-folder-openend
is called with the appropriate
jakarta.mail.Folder
object. See also
jakarta.mail.FolderAfter a certain period of inactivity, the server closes the connection to the
client. To fix a possible jakarta.mail.FolderClosedException
when reading
a folder or email, I offer this solution:
(let
[ten-minutes (->> 1000 (* 60) (* 10))
idle-agent (agent (->> (imap-observer/make-observer
(->> (clojure.java.io/resource "mail-properties.edn")
slurp
clojure.edn/read-string)
[{:name "INBOX"
:mode (:read-write imap-observer/folder-modes)
:on-message-added (fn [messages])}])))]
(prn "starting execution")
(while true
(send idle-agent component/start)
(Thread/sleep ten-minutes)
(send idle-agent component/stop)
(prn "restarting execution")))
GreenMail
:on-folder-opened
, :on-message-added
and
:on-message-removed
) with GreenMail
The MIT License (MIT)
Copyright © 2021 Markus Aschl
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close