Wednesday, August 21, 2013

Sending E-mails from an Automation Object - Part 1

Once in a while I hear people asking how to send e-mails from Application Server. As long as you have access, or rather your Galaxy has access to an SMTP (Simple Mail Transfer Protocol) server, you can configure an object to perform this task using the System.Net.Mail namespace of the Microsoft .NET Framework.

After the break, I'll walk you through a basic script that will send e-mails though Gmail SMTP server. Later, in Part 2, I will describe a better, optimized version of the object and script.

For basic e-mail delivery, we only need to implement three classes from the System.Net.Mail namespace:
  • SmtpClient: Allows you to send e-mails using SMTP.
  • MailAddress: E-mail address for the sender or recipient.
  • MailMessage: E-mail message to be sent by SmtpClient.

Let's get started. First of all, we'll need a way to trigger the script; so let's create a UDA for this purpose:
  • Name: SendEmailTrigger
  • Data type: Boolean
  • Category: User writeable (so we can trigger it manually; otherwise Object writeable will do)

We'll need an Execute script that will run based on the UDA create above:
  • Name: SendEmail
  • Execution type: Execute
  • Expression: Me.SendEmailTrigger
  • Trigger type: On True

The body of the script is pretty simple and straightforward, but I will dissect it below just the same:

' SMTP client.
dim smtp as System.Net.Mail.SmtpClient;
smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");

' FROM and TO addresses.
dim sender as System.Net.Mail.MailAddress;
sender = new System.Net.Mail.MailAddress("username@gmail.com", "displayname");
dim recipient as System.Net.Mail.MailAddress;
recipient = new System.Net.Mail.MailAddress("recipient@company.com");

' Email MESSAGE.
dim emailMsg as System.Net.Mail.MailMessage;
emailMsg = new System.Net.Mail.MailMessage(sender, recipient);
emailMsg.Subject = "Message from object";
emailMsg.Body = "This is a test message.";

' Send e-mail.
smtpClient.SendMailAsync(emailMsg);

' Reset trigger.
Me.SendEmailTrigger = false;
So what does it all means? Overall, the main workflow of the script:
  1. Create SMTP client object to send e-mail messages.
  2. Define the FROM and TO mail address objects.
  3. Define the actual e-mail MESSAGE.
  4. Send the e-mail.
Let's take a closer look at all the pieces.

dim smtp as System.Net.Mail.SmtpClient;
smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
We create and instantiate an SmtpClient object to use Gmail's SMTP server smtp.gmail.com on TCP port 587. The default port for SMTP is TCP port 25, but Gmail uses the new mail submission agent (MSA) which uses port 587 instead.

smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
Gmail requires a secure connection, so we need to enable Secure Sockets Layer (SSL) and specify the Gmail user account to be used to send the e-mail.

dim sender as System.Net.Mail.MailAddress;
sender = new System.Net.Mail.MailAddress("username@gmail.com", "displayname");
dim recipient as System.Net.Mail.MailAddress;
recipient = new System.Net.Mail.MailAddress("recipient@company.com")
To send an e-mail it is necessary to indicate the sender and recipient e-mail addresses. The actual e-mail address is mandatory for both mail addresses. For the sender, an optional display name can be added for easy-read by the recipient.

dim emailMsg as System.Net.Mail.MailMessage
emailMsg = new System.Net.Mail.MailMessage(sender, recipient);
Time to create and instantiate the e-mail message itself with the sender and recipient e-mail addresses. To send the e-mail to multiple recipients, add multiple MailAddress objects to the MailMessage.To collection. Let me know in the comments if you need a hand on how to do this.

emailMsg.Subject = "Message from object";
emailMsg.Body = "This is a test message.";
It wouldn't be an e-mail message without a subject and a body, right? And please, do include a subject; it's e-mail etiquette. I hate e-mails without subject. Yeah, I'm looking at you; you know who you are. The subject and body properties are strings, so any string value in the Galaxy, for example, a UDA, will do.

smtp.SendMailAsync(emailMsg);
Finally, time to send the e-mail. The SmtpClient class has three different Send methods; for use with Application Server, the SendMailAsync method is the more suitable. You could also use the SendAsync method, which requires a second string parameter and allows you to cancel the operation afterwards (not recall the e-mail, but abort the sending of the e-mail).

Important: Avoid the Send method; it sends the e-mail synchronously, holding the execution of the AppEngine until the method is done executing.

Me.SendEmailTrigger = false;
For good measure, let's reset the flag that triggered the script so it can be easily re-triggered again.

While this script works as-is, it has all the data hardcoded and re-creates all objects every time the script runs. On Part 2 I will show you a better structured version of this script and the use of UDAs to allow for customization of the recipient, subject, and body.

2 comments:

  1. Could you please tell me how to use SendSMTPMail()?

    ReplyDelete
    Replies
    1. Oscar, I don't know where that function is coming from. It doesn't seem to be a function from the .NET Framework.

      Delete