SMTP Messages using C#.NET

Today I will go over how to build a simple SMTP Message Client, which can be used to send emails. This one is fun if your organization’s SMTP server does not require authentication (many don’t) and you would like to impersonate your boss. Note: Don’t impersonate your boss.

Lets get started!

What you’ll need:

  • The hostname of an SMTP server.
  • The port of the SMTP Service on the server. This is usually port 25.

You can wrap this in a Windows Forms GUI, a command prompt, have it run off a web service, etc. That part is up to you, but I will go over the functions involved.

Here is your namespace:

using System.Net.Mail;

And here is the bulk:

var smtpMessage = new MailMessage(fromEmail, toEmail)
{
    Subject = subjectText,
    Body = messageText
};
var smtpClient = new SmtpClient(smtpServer, (ushort)smtpPort);

try
{
    smtpClient.Send(smtpMessage);
    outcome = string.Format("Success!");
}
catch (Exception ex)
{
    outcome = string.Format("Error: {0}, {1}, {2}", ex, ex.Message, ex.StackTrace);
}

Pretty simple, but lets dissect it.

Note: This assumes several things: 1) You have defined fromEmail, toEmail, subjectText, messageText, smtpServer, smtpPort and 2) you have a variable named outcome to hold the results of sending the message.

Lets start from the top. Here is the message creation:

var smtpMessage = new MailMessage(fromEmail, toEmail)
{
    Subject = subjectText,
    Body = messageText
};

This creates a variable with the actual message, which includes the To Address, the From Address, the Subject, and the Message body. If you would like to do some fancy formatting in your message, make sure to include “” at the start of the message and “” at the end of the message. For more information on HTML and building a pretty message body, please read this extensive documentation on HTML.

var smtpClient = new SmtpClient(smtpServer, smtpPort);

This line defines the SMTP Server Name and Port, and is the basis for our connection to the server.

try
{
    smtpClient.Send(smtpMessage);
    outcome = string.Format("Success!");
}
catch (Exception ex)
{
    outcome = string.Format("Error: {0}, {1}, {2}", ex, ex.Message, ex.StackTrace);
}

Here we use some built in .NET magic to connect to the server and send the message. This is wrapped in a try-catch to mitigate crashes in your application, and should handle things like bad connections, offline servers, improper to/from emails, etc.

That is it! Pretty simple, really. But why has this been useful to me as a Software Tester? Some uses I have found for this small block of code:

  • Email alerts sent by automated tests for completions, fails, etc.
  • The basis for automating the testing of SMTP servers
  • Getting ‘approval’ for test approaches or vacation days

Please, let me know in the comments section any places you have been able to use this or similar code!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: