I don't know if anyone else will care, but here's a function for
sending email over SSL SMTP that I got working last night:
(defn sendmail [& optlist]
(let [props (new java.util.Properties)
opts (merge (apply hash-map optlist) {
:starttls.enable true
:auth true
:socketFactory.port (opts :port)
:socketFactory.class
"javax.net.ssl.SSLSocketFactory"
:socketFactory.fallback false})]
(doseq opt opts
(. props
(put (str "mail.smtp." (. (str (key opt)) (substring 1)))
(str (val opt)))))
(. javax.mail.Transport
(send
(doto (new javax.mail.internet.MimeMessage
(. javax.mail.Session
(getInstance
props
(new net.n01se.chouser.SimpleAuthenticator
(opts :user) (opts :password)))))
(setText (opts :text))
(setSubject (opts :subject))
(setFrom (new javax.mail.internet.InternetAddress
(opts :user)))
(addRecipient
(. javax.mail.Message$RecipientType TO)
(new javax.mail.internet.InternetAddress
(opts :to))))))))
Then use it like so:
(sendmail :user "
sen...@example.com" :password "nottellingyou"
:host "
smtp.gmail.com" :port 465
:to "
reci...@example.com" :subject "test message"
:text "This is the message")
Unfortunately I couldn't figure out how to get it to work without this
little Java class:
package net.n01se.chouser;
public class SimpleAuthenticator extends javax.mail.Authenticator
{
String email, password;
public SimpleAuthenticator( String e, String p ) {
email = e;
password = p;
}
public javax.mail.PasswordAuthentication getPasswordAuthentication()
{
return new javax.mail.PasswordAuthentication(email, password);
}
}
So much thanks to Rich (as usual). Anybody know how I could implement
SimpleAuthenticator in Clojure instead of Java?
--Chouser