DotNet Example: Send an email
This example is an Xbasic script that sends an email using the .NET System::Net::Mail class. Copy this into a new Xbasic module or desktop script, fill in your parameters, and test it from your application.
In addition to built-in server-side and client-side actions for sending emails as part of a business process, the .NET System::Net::Mail class can be used to script the creation and sending of emails using Xbasic.
This example can be transformed into a customized Send_Email function for an application that accepts the recipient, subject, and body as parameters to the method. Other settings can be declared within the function. Messages can have multiple To, CC, and Bcc recipients specified as a collection of MailAddress objects. Properties and methods of the Mail.MailMessage class can be inspected directly from the Xbasic editor. Alpha Anywhere's auto-complete will display a list of properties an methods as code is written:
A list of properties and methods can also be listed in the interactive window using the ? operator:
dim msg as System::Net::Mail::MailMessage ? msg = PUBLIC Dispose AS System::Void () 'Dispose PUBLIC Equals AS L (obj AS A) 'Equals PUBLIC GetHashCode AS N () 'GetHashCode PUBLIC GetType AS System::Type () 'GetType PUBLIC MailMessage() 'Public Constructor PUBLIC STATIC ReferenceEquals AS L (objA AS A, objB AS A) 'ReferenceEquals PUBLIC ToString AS C () 'ToString +AlternateViews. +Attachments. +Bcc. Body = "" BodyEncoding = <No data returned> +BodyTransferEncoding. +CC. +DeliveryNotificationOptions. From = <No data returned> +Headers. HeadersEncoding = <No data returned> IsBodyHtml = .F. +Priority. ReplyTo = <No data returned> +ReplyToList. Sender = <No data returned> Subject = "" SubjectEncoding = <No data returned> +To.
If you want display names with your email addresses (e.g. "Mortimer Snerd <email@example.com>"), use one of the long MailAddress constructors described here.
msg.From = new System::Net::Mail::MailAddress("firstname.lastname@example.org","Mortimer Snerd")
You can also use a long constructor for the MailMessage object to initialize it using two MailAddress objects corresponding to the From and To addresses, e.g.
dim FromAddr as System::Net::Mail::MailAddress = \ new System::Net::Mail::MailAddress("email@example.com","Charlie McCarthy") dim ToAddr as System::Net::Mail::MailAddress = \ new System::Net::Mail::MailAddress("firstname.lastname@example.org","Mortimer Snerd") dim Msg as System::Net::Mail::MailMessage = \ new System::Net::Mail::MailMessage(FromAddr ,ToAddr)
In our sample code below we use the simple MailAddress constructor, which does not allow for a display name.
If you want file attachments, uncomment the msg.Attachments.Add line. If you want them sometimes, add a test for a non-zero length for FileAttachment. If you want multiple attachments, add a FOR or FOR EACH loop on an array of attachment names. It would also be good to test for the existence of each file you want to send before attaching it.
This general method for sending email is a good alternative to the various email functions built into Xbasic.
'most commonly used parameters dim recipient as C = "(your recipient here, must be one valid email)" dim subj as C = "(your subject line here)" dim body as C = "(your email body here)" dim isHtml as L = .T. '.T. if body is HTML, otherwise .F. dim FileAttachment as C = "(your attachment file path here)" dim CC as C = "(your CC here, must be one valid email)" dim Bcc as C = "(your BCC here, must be one valid email)" 'Multiple recipients, CCs, or BCCs can be added using a 'FOR or FOR EACH loop. 'parameters that are often static for a web site dim sender as C = "(your sender here, must be one valid email)" dim EmailServer as C = "smtp.gmail.com" 'put your server here dim EmailPort as N = 587 'usually 25 (plain) or 465 (SSL/TLS). 587 for GMail TLS. dim isSSL as L = .T. '.T. if SSL/TLS, .F. if not dim EmailUser as C = "(your user ID here, often the same as the sender)" dim EmailPassword as C = "(your email password here)" dim msg as System::Net::Mail::MailMessage = new System::Net::Mail::MailMessage() msg.To.Add(recipient) msg.From = new System::Net::Mail::MailAddress(sender) msg.Subject = subj msg.Body = body msg.BodyEncoding = System::Text::Encoding::UTF8 msg.IsBodyHtml = isHtml msg.Priority = System::Net::Mail::MailPriority::Normal dim addrCC as System::Net::Mail::MailAddress = new System::Net::Mail::MailAddress(CC) msg.CC.Add(addrCC) dim addrBcc as System::Net::Mail::MailAddress = new System::Net::Mail::MailAddress(Bcc) msg.Bcc.Add(addrBcc) 'Uncomment the next line if you want to have a file attachment 'msg.Attachments.Add(new System::Net::Mail::Attachment(FileAttachment)) dim MailClient as System::Net::Mail::SmtpClient = \ new System::Net::Mail::SmtpClient(EmailServer,EmailPort) MailClient.EnableSsl = isSSL MailClient.UseDefaultCredentials = .F. MailClient.Credentials = new System::Net::NetworkCredential(EmailUser, EmailPassword) MailClient.Send(msg) 'this does the actual sending of email msg.Dispose() 'clean up