Created
March 24, 2012 18:18
-
-
Save Chouser/2185831 to your computer and use it in GitHub Desktop.
Send email via gmail from Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to add another property, "mail.smtp.port" "587"