public
Last active

Send email via gmail from Clojure

  • Download Gist
gmail.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
(ns foo
(:import (java.util Properties)
javax.mail.internet.MimeMessage
(javax.mail.internet MimeMessage InternetAddress)
(javax.mail Session Transport Authenticator
PasswordAuthentication Message$RecipientType)))
 
(defn send-gmail [{:keys [from to subject text user password]}]
(let [auth (proxy [Authenticator] []
(getPasswordAuthentication []
(PasswordAuthentication. user password)))
props (doto (Properties.)
(.putAll {"mail.smtp.user" user
"mail.smtp.host" "smtp.gmail.com"
"mail.smtp.starttls.enable" "true"
"mail.smtp.auth" "true"}))
session (Session/getInstance props auth)
msg (doto (MimeMessage. session)
(.setText text)
(.setSubject subject)
(.setFrom (InternetAddress. from)))]
(doseq [addr to]
(.addRecipient msg Message$RecipientType/TO (InternetAddress. addr)))
(Transport/send msg)))

I had to add another property, "mail.smtp.port" "587"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.