<commit_before>(ns com.draines.postal.core
  (:use [clojure.contrib.test-is :only [is deftest run-tests]]
        [com.draines.postal.date :only [make-date]])
  (:import [java.util Date Properties]
           [javax.mail Session Transport Message$RecipientType]
           [javax.mail.internet MimeMessage InternetAddress]
           [com.sun.mail.smtp SMTPSSLTransport]))

;;  Properties props = new Properties();
;;  props.put("mail.smtp.host", "my-mail-server");
;;  props.put("mail.from", "<EMAIL>");
;;  Session session = Session.getInstance(props, null);
;;
;;  try {
;;      MimeMessage msg = new MimeMessage(session);
;;      msg.setFrom();
;;      msg.setRecipients(Message.RecipientType.TO,
;;                        "<EMAIL>");
;;      msg.setSubject("JavaMail hello world example");
;;      msg.setSentDate(new Date());
;;      msg.setText("Hello, world!\n");
;;      Transport.send(msg);
;;  } catch (MessagingException mex) {
;;      System.out.println("send failed, exception: " + mex);
;;  }

;; Session.getTransport() and Transport.connect()

(defn get-message [msg]
  (get msg :MimeMessage msg))

(defn get-recipients [msg]
  (map (comp str
             #(.getAddress %))
       (.getAllRecipients (get-message msg))))

(defn add-recipient [msg rtype addr]
  (doto (get-message msg) (.addRecipient rtype (InternetAddress. addr))))

(defn add-recipients [msg rtype addrs]
  (if (string? addrs)
    (add-recipient msg rtype addrs)
    (doseq [addr addrs]
      (add-recipient msg rtype addr)))
  msg)

(defn set-subject [msg subj]
  (.setSubject (get-message msg) subj))

(defn set-body [msg body]
  (.setText (get-message msg) body))

(defn set-date [msg date]
  (.setSentDate (get-message msg) date))

(defn get-subject [msg]
  (.getSubject (get-message msg)))

(defn get-body [msg]
  (.getContent (get-message msg)))

(defn get-date [msg]
  (.getSentDate (get-message msg)))

(defn make-message [attrs]
  (let [{:keys [to cc bcc date subject body]} attrs
        msg (merge attrs {:MimeMessage (MimeMessage. (Session/getInstance (java.util.Properties.)))})]
    (add-recipients msg Message$RecipientType/TO to)
    (add-recipients msg Message$RecipientType/CC cc)
    (add-recipients msg Message$RecipientType/BCC bcc)
    (set-subject msg subject)
    (set-body msg body)
    (set-date msg date)
    msg))

(deftest all
  (let [date (make-date "yyyy-MM-dd" "2009-01-01")
        msg (make-message {:to "Foo Bar <<EMAIL>>"
                           :cc ["<EMAIL>" "<EMAIL>"]
                           :date date
                           :subject "Test"
                           :body "Test!"})]
    (is (= '("<EMAIL>" "<EMAIL>" "<EMAIL>") (get-recipients msg)))
    (is (= "Test" (get-subject msg)))
    (is (= "Test!" (get-body msg)))
    (is (= date (get-date msg)))))

(run-tests)<commit_msg>Add From: and host info.<commit_after>(ns com.draines.postal.core
  (:use [clojure.contrib.test-is :only [is deftest run-tests]]
        [com.draines.postal.date :only [make-date]])
  (:import [java.util Date Properties]
           [javax.mail Session Transport Message$RecipientType]
           [javax.mail.internet MimeMessage InternetAddress]
           [com.sun.mail.smtp SMTPSSLTransport]))

;;  Properties props = new Properties();
;;  props.put("mail.smtp.host", "my-mail-server");
;;  props.put("mail.from", "<EMAIL>");
;;  Session session = Session.getInstance(props, null);
;;
;;  try {
;;      MimeMessage msg = new MimeMessage(session);
;;      msg.setFrom();
;;      msg.setRecipients(Message.RecipientType.TO,
;;                        "<EMAIL>");
;;      msg.setSubject("JavaMail hello world example");
;;      msg.setSentDate(new Date());
;;      msg.setText("Hello, world!\n");
;;      Transport.send(msg);
;;  } catch (MessagingException mex) {
;;      System.out.println("send failed, exception: " + mex);
;;  }

;; Session.getTransport() and Transport.connect()

(defn get-message [msg]
  (get msg :MimeMessage msg))

(defn get-recipients [msg]
  (map (comp str
             #(.getAddress %))
       (.getAllRecipients (get-message msg))))

(defn add-recipient [msg rtype addr]
  (doto (get-message msg) (.addRecipient rtype (InternetAddress. addr))))

(defn add-recipients [msg rtype addrs]
  (if (string? addrs)
    (add-recipient msg rtype addrs)
    (doseq [addr addrs]
      (add-recipient msg rtype addr)))
  msg)

(defn set-from [msg from]
  (.setFrom (get-message msg) (InternetAddress. from)))

(defn set-subject [msg subj]
  (.setSubject (get-message msg) subj))

(defn set-body [msg body]
  (.setText (get-message msg) body))

(defn set-date [msg date]
  (.setSentDate (get-message msg) date))

(defn get-from [msg]
  (.getFrom (get-message msg)))

(defn get-subject [msg]
  (.getSubject (get-message msg)))

(defn get-body [msg]
  (.getContent (get-message msg)))

(defn get-date [msg]
  (.getSentDate (get-message msg)))

(defn make-message [attrs]
  (let [{:keys [from to cc bcc date subject body host]} attrs
        props (doto (java.util.Properties.)
                (.put "mail.smtp.host" host)
                (.put "mail.from" from))
        msg (merge attrs {:Session (Session/getInstance props)})
        msg (merge msg {:MimeMessage (MimeMessage. (:Session msg))})]
    (add-recipients msg Message$RecipientType/TO to)
    (add-recipients msg Message$RecipientType/CC cc)
    (add-recipients msg Message$RecipientType/BCC bcc)
    (set-from msg from)
    (set-subject msg subject)
    (set-body msg body)
    (set-date msg date)
    msg))

(deftest all
  (let [date (make-date "yyyy-MM-dd" "2009-01-01")
        msg (make-message {:host "smtp.comcast.net"
                           :from "<EMAIL>"
                           :to "Foo Bar <<EMAIL>>"
                           :cc ["<EMAIL>" "<EMAIL>"]
                           :date date
                           :subject "Test"
                           :body "Test!"})]
    (is (= '("<EMAIL>" "<EMAIL>" "<EMAIL>") (get-recipients msg)))
    (is (= "Test" (get-subject msg)))
    (is (= "Test!" (get-body msg)))
    (is (= date (get-date msg)))))

(run-tests)
{'fraction_non_alphanumeric': 0.16565224543510446, 'fraction_numerical': 0.0026320118440532982, 'mean_word_length': 2.771712158808933, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2253061', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2031, 'n_words': 514}