Sending Emails via SMTP

The following code which was created in V11 of Dyalog creates an email and try to send it to several addresses, together with 2 attachments

 System∆Net∆Mail;Txt;Err;from;to;cc;message;bcc;filename;fileStream;contentType;disposition;attached;MySmtp
 ⎕IO←1 ⋄ ⎕ML←1 ⋄ ⎕WX←3
 ⎕USING←,⊂',System.dll'

 from←#.System.Net.Mail.MailAddress.New⊂'from@domain.com'
 to←#.System.Net.Mail.MailAddress.New'to@domain.com' 'Real name'
 cc←#.System.Net.Mail.MailAddress.New'cc@domain.com' 'Another real name'System.Text.Encoding.UTF8
 bcc←#.System.Net.Mail.MailAddress.New⊂'bcc@domain.com'

 message←#.System.Net.Mail.MailMessage.New from to
 message.Subject←'The subject of the test mail'
 message.Body←'The body of the test mail'
 message.CC.Add cc
 message.Bcc.Add bcc

 ⍝ Attach a file
 attached←#.System.Net.Mail.Attachment.New⊂'C:\APL-Libs\myLnag.txt'
 message.Attachments.Add attached

 ⍝ Attach a file as a stream
 filename←'C:\buffer\APL_In_20_Minutes.TXT'
 fileStream←#.System.IO.FileStream.New filename #.System.IO.FileMode.Open #.System.IO.FileAccess.Read
 fileStream.Close
 contentType←#.System.Net.Mime.ContentType.New⊂#.System.Net.Mime.MediaTypeNames.Text.Plain
 attached←#.System.Net.Mail.Attachment.New fileStream contentType
 disposition←attached.ContentDisposition
 disposition.FileName←'file',(⊃,/⍕¨⎕TS),'.txt' ⍝ change the filename suggested to receiver
 message.Attachments.Add attached

 MySmtp←#.System.Net.Mail.SmtpClient.New⊂'localhost'
 MySmtp.Credentials←#.System.Net.CredentialCache.DefaultNetworkCredentials
 MySmtp.Send(message)

Author: KaiJaeger


CategoryDotNet - CategoryDyalogDotNet - CategoryDyalogExamplesDotNet

EMailsWithDotNet (last edited 2015-04-05 01:19:23 by PierreGilbert)