Sending Email using .Net

Here's a way to send email messages using Dyalog and .Net's System.Web.Mail namespace.

Ingredients

Obstacles to negotiate

Spam emails plague the Net and administrators place all kinds of obstacles in their way. Your messages need to avoid these obstacles.

Clearly, you can adjust some or all of this in the following not-exactly-difficult source code.

Usage

mm←⎕NEW MailMan
mm.Attachments←'C:\Temp\figures.xls'
mm.Body←'Here are the figures I promised you.' 'Good weekend!' 'Tony'
mm.From←'A.N. Other <otheran@somewhere.com>'
mm.SmtpServer←'smtp.somewhere.com'
mm.Subject←'Figures you asked for'
mm.To←'M.E. Kick <kickme@somewhere.com>' 'R. Supwards<supwards@domain.com>'
mm.Send

Notes

Source

:Class MailMan
⍝ Send an email message, with one or more attachments, to one or more addressees
⍝ If To is a nested vector, the message is sent individually to each element
⍝ Body may be flat, matrix or nested char vector;
⍝ if flat, CRs (x013) are replaced with CRLFs (x013 x010)
⍝ Stephen Taylor sjt@dyalog.com May 2008

    ⎕USING←'System.Web.Mail,System.Web.dll'

    :Field Public Attachments←''
    :Field Public Body←''
    :Field Public From←''
    :Field Public MessageDelay←1        ⍝ seconds delay between repeated sends
    :Field Public Subject←''
    :Field Public SmtpServer←''
    :Field Public To←''

    ∇ Send;msg;to
      :Access Public
      :If 0∊⊃∘⍴¨Body From Subject To
          'INCOMPLETE MESSAGE'⎕SIGNAL 11
      :ElseIf 0∊⍴SmtpServer
          'UNIDENTIFIED SMTP SERVER'⎕SIGNAL 11
      :Else
          SmtpMail.SmtpServer←SmtpServer
          msg←⎕NEW MailMessage
          msg.(From Subject)←From Subject
          msg.Body←LF CR join(CR∘part∘,if simple)∘(rtb¨∘↓if matrix)Body
          :If ×⍴Attachments
              {}msg.Attachments.Add¨{⎕NEW MailAttachment(⊂⍵)}¨⊂if simple Attachments
          :EndIf
          :For to :In ⊂if simple To
              msg.To←to
              SmtpMail.Send msg
              ⎕DL MessageDelay
          :EndFor
      :EndIf

   ⍝ vocabulary ________________________________________________________________
    LF CR←⎕UCS 10 13
    if←{(⍺⍺⍣(⍵⍵ ⍵))⍵}                           ⍝ eg ⊂if simple
    join←{⊃,∘(⍺∘,)/⍵}                           ⍝ join strings in ⍵ with ⍺
    matrix←{2=⊃⍴⍴⍵}
    part←{(⍴,⍺)↓¨⊂where(⍺∘⍷)⍺,⍵}                ⍝ partition ⍵ at occurrences of ⍺
    scalar←0∘=∘≡
    simple←0 1∘(∊⍨)∘≡
    where←{(⍵⍵ ⍵)⍺⍺ ⍵}                          ⍝ or {⍵ ⍺⍺⍨⍵⍵ ⍵} or {⍺⍺⍨∘⍵⍵⍨⍵}

    rtb←↓where{-+/∧\' '=⌽⍵}                     ⍝ remove trailing blanks

:EndClass