Sending Emails using the EmailMessage class (Windows only)

The Consultingwerk.Utilities.Mail.EmailMessage class provides a simple way to send emails on Windows machines (OpenEdge 10.2B or OpenEdge 11 Client and OpenEdge 11 AppServer). It is a wrapper to the .NET System.Net.Mail.SmtpClient class.

The EmailMessage class requires to be instantiated using an instance of the Consultingwerk.Utilities.Mail.EmailConfiguration class. This class provides the SMTP server and user configuration:

  • Hostname
  • Username (for authentication)
  • Password (for authentication)
  • Sender Name (Display Name and Email Address)

The purpose of separating the EmailConfiguration from the EmailMessage is to allow the initialization of the EmailConfiguration instance during the application start. The instance may then be registered in the default ServiceContainer so that it can be used by multiple EmailMessage instances later.

In order to send an EmailMessage, you need to create an instance of the class and provide the EmailConfiguration instance to the constructor. Then the Subject and Body properties of the EmailMessage must be set followed by invoking the Send method with the Email recipients address.

A sample routine to send an email using the EmailMessage class looks like this:


EmailMessage Sample
USING Consultingwerk.Utilities.Mail.* FROM PROPATH.

DEFINE VARIABLE oMailConfig AS EmailConfiguration NO-UNDO . 
DEFINE VARIABLE oMessage    AS EmailMessage       NO-UNDO . 

/* Create an instance of the EmailConfiguration class */
oMailConfig = NEW EmailConfiguration (). 

/* Assign mail parameters */
oMailConfig:Host = "" .
oMailConfig:UserName = "" . 
oMailConfig:Password = "secret" .
oMailConfig:Sender = "Send by SmartComponent Library <>" .

/* Create EmailMessage instance based on EmailConfiguration instance */
oMessage = NEW EmailMessage (oMailConfig) .

/* Assign Subject and Body */
oMessage:Subject = "This is the subject" .
oMessage:Body = "This is the mail body" .
/* Send email */
oMessage:Send ("") .