Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
sending email (SMTP + SSL)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chouser  
View profile  
 More options Feb 29 2008, 10:27 am
From: Chouser <chou...@gmail.com>
Date: Fri, 29 Feb 2008 07:27:01 -0800 (PST)
Local: Fri, Feb 29 2008 10:27 am
Subject: sending email (SMTP + SSL)
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 "recipi...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chouser  
View profile  
 More options Feb 29 2008, 10:31 am
From: Chouser <chou...@gmail.com>
Date: Fri, 29 Feb 2008 07:31:31 -0800 (PST)
Local: Fri, Feb 29 2008 10:31 am
Subject: Re: sending email (SMTP + SSL)
Oops, sorry, had an incorrect untested change.

On Feb 29, 10:27 am, Chouser <chou...@gmail.com> wrote:

> (defn sendmail [& optlist]
>   (let [props (new java.util.Properties)
>         opts (merge (apply hash-map optlist) {
>                     :starttls.enable true

That should be:

(defn sendmail [& optlist]
  (let [props (new java.util.Properties)
        opts (apply hash-map optlist)
        opts (merge (apply hash-map optlist) {
                    :starttls.enable true

--Chouser


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chouser  
View profile  
 More options Mar 4 2008, 4:19 pm
From: Chouser <chou...@gmail.com>
Date: Tue, 4 Mar 2008 13:19:52 -0800 (PST)
Local: Tues, Mar 4 2008 4:19 pm
Subject: Re: sending email (SMTP + SSL)
This version uses the new dynamic proxies so it no longer requires a
Java helper class:

(defn sendmail [& optlist]
  (let [props (new java.util.Properties)
        opts  (apply hash-map optlist)
        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)))))
    (let [msg (new javax.mail.internet.MimeMessage
                   (. javax.mail.Session
                      (getInstance
                        props
                        (proxy [javax.mail.Authenticator] []
                               (getPasswordAuthentication []
                                  (new
javax.mail.PasswordAuthentication
                                       (opts :user)
                                       (opts :password)))))))]
      (cond (opts :text) (. msg (setText (opts :text)))
            :else        (. msg (setText (opts :html) "UTF-8"
"html")))
      (. msg (setSubject (opts :subject)))
      (. msg (setFrom (new javax.mail.internet.InternetAddress
(opts :user))))
      (doseq rcpt (opts :to)
             (. msg (addRecipient
                      (. javax.mail.Message$RecipientType TO)
                      (new javax.mail.internet.InternetAddress
rcpt))))
      (. javax.mail.Transport (send msg)))))

This version also supports multiple recipients and html message
bodies:

(sendmail :user "f...@example.com" :password "nottellingyou"
          :host "smtp.gmail.com" :port 465
          :to ["t...@example.com" "t...@example.com] :subject "test
message"
          :html "This is the <b>message</b>")


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »