Sending Email using Dyalog and .Net

Note: This example uses the .Net namespace 'System.Web.Mail', which Microsoft's documentation now lists as deprecated. For an example of using the newer 'System.Mail' namespace, see this page. (The code listed is APLX-specific, but easily adapted to Dyalog).

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


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.

  1. SMTP credentials: some SMTP servers demand credentials before they will send your messages. If you have to present credentials to your server, you will need to dig further into the System.Web.Mail namespace.

  2. Some servers are wary of processing messages with many recipients. So this process sends messages to individual list members.
  3. Some servers 'throttle' outgoing messages or just reject them if they arrive too quickly from the same sender. So this process waits between sending messages.

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


mm←⎕NEW MailMan
mm.Body←'Here are the figures I promised you.' 'Good weekend!' 'Tony'
mm.From←'A.N. Other <>'
mm.Subject←'Figures you asked for'
mm.To←'M.E. Kick <>' 'R. Supwards<>'


  1. Body can be a flat character vector, a nested character vector, or a matrix.

  2. Attachments can be a simple or nested character vector.

  3. Messages are sent in a For loop. Although they could easily be dispatched using the each operator, suspending and restarting the expression would resend all the messages already sent.

  4. The vocabulary defines LF and CR using ⎕UCS. For versions of Dyalog earlier than 12, LF CR←⎕TC[3 2].

  5. Note the use of a vocabulary of simple functions and operators to keep the Send method clear.

  6. Note the use of the if operator to handle different forms of Body and Attachments

  7. .Net namespace System.Web.Mail is now deprecated. (Can anyone update this to work with System.Net.Mail instead?


: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 (0x0D) are replaced with CRLFs (0x0D 0x0A)
⍝ Stephen Taylor May 2008


    :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
      :ElseIf 0∊⍴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
          :For to :In ⊂if simple To
              SmtpMail.Send msg
              ⎕DL MessageDelay

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

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


Author: StephenTaylor

CategoryDotNet - CategoryDyalogDotNet - CategoryDyalogExamplesDotNet - CategoryEmail

SendingEmailDyalog (last edited 2015-04-05 01:28:03 by PierreGilbert)