Differences between revisions 7 and 8
Revision 7 as of 2008-08-20 18:57:19
Size: 1790
Editor: anonymous
Comment: converted to 1.6 markup
Revision 8 as of 2009-01-16 07:29:29
Size: 1820
Editor: KaiJaeger
Comment: Name wasn't clear enough
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from EMail

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


CategoryDyalogExamplesDotNet

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