Programming · Games · Life outdoors
Plain Text Email from Clojure
I recommend using the Apache Commons Email library when sending email from Clojure apps. Here’s a simple example.
First up, you need to add the dependency to ‘project.clj’
:dependencies [ [org.clojure/clojure "1.10.1"] [org.apache.commons/commons-email "1.5"] ]Next, your code would look something like the following. Caution, … This will send emails, so configure it to send the emails to a personal email address for testing purposes.
(ns email-example.core (:import (org.apache.commons.mail SimpleEmail)))
(defn -main "I send plaintext emails using the Apache Commons Email library." [& args] (println "Email the World ...")
;; for more methods, such as for authentication, look at the manual... ;; http://commons.apache.org/proper/commons-email/userguide.html
(let [smtp-address "smtp.example.com" from "[email protected]" to "[email protected]" subject "Test email" msg "This is your basic plain text email. Enjoy!"]
(doto (SimpleEmail.) (.setHostName smtp-address) (.setFrom from) (.setSubject subject) (.setMsg msg) (.addTo to) (.send)))
(println "... Email Sent!"))