Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
C# Manual
Contact Us

GoDiagram for .NET from Northwoods Software

Printable Version

C# Email Utility
By Kara Hewett


This useful C# .NET email utility configures easily identifying the receiver, sender, message content and mail server requiring minimal time for implementation.

Creating the Workspace

To start the Email Utility project, open a Microsoft Visual Studio 2005, .NET Framework Version 2.0 development environment and create a Console Application. Add two C# classes, one called EmailHandler.cs and another called AppHandler.cs and an Application Configuration File called the default, App.config.


As with any Microsoft C# project, the application consists of the configuration properties and the application. By default the App.config file is XML with a primary element named .configuration.. To the default .configuration. element, set the properties required for email configuration:

. Recipient
. Sender
. Copy List
. Priority
. Body Encoding
. Subject
. Message Body
. SMTP Server

<?xml version="1.0" encoding="utf-8" ?>
    <add key="Email.Recipient" value="," />
    <add key="Email.Sender" value=" " />
    <add key="Email.Copy" value="" />
    <add key="Email.Priority" value="MailPriority.Normal "/>
    <add key="Email.Encoding" value="Encoding.ASCII"/>
    <add key="Email.Subject" value=".NET Email Utility Sample"/>
    <add key="Email.Message" value="This email provides an example of sending a .NET email using C#."/>
    <add key="Email.Smtp" value="" />
The Email Handler

The Email Handler provides a simple method for sending the email using the parameters defined in the application configuration file.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;

namespace EmailApp
public sealed class EmailHandler
private static char[] charSeparators = new char[] {  };
private static String[] result;

  public static void SendMailMessage(string smtpHost, MailAddress from, string to, string subject, string message)
                MailMessage mailMsg = new MailMessage();
                result = to.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
                for (int count = 0; count < result.Length; count++)
                { mailMsg.To.Add(new MailAddress(result[count])); }
                mailMsg.From = from;
                mailMsg.Subject = subject;
                mailMsg.Body = message;
                mailMsg.Priority = AppHandler.EmailPriority;
                mailMsg.BodyEncoding = AppHandler.EmailEncoding;
                SmtpClient smtpClient = new SmtpClient(smtpHost);

            catch (Exception exc)
The AppHandler class makes the application parameters accessible as static variables in the EmailHandler class.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace EmailApp
public static class AppHandler
private static MailAddress emailRecipient;
private static String emailSender;
private static String emailCopy;
private static String emailPriority;
private static String emailEncoding;
private static String emailSubject;
private static String emailMessage;
private static String emailSmtp;

public static MailAddress EmailRecipient
get { return emailRecipient; }
set { emailRecipient = value; }
public static String EmailSender
get { return emailSender; }
set { emailSender = value; }
public static String EmailCopy
get { return emailCopy; }
set { emailCopy = value; }
public static String EmailPriority
get { return emailPriority; }
set { emailPriority = value; }
public static String EmailEncoding
get { return emailEncoding; }
set { emailEncoding = value; }
public static String EmailSubject
get { return emailSubject; }
set { emailSubject = value; }
public static String EmailMessage
get { return emailMessage; }
set { emailMessage = value; }
The EmailHandler usage involves setting the configuration parameters and calling one line of code.
AppHandler.EmailRecipient = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["Email.Recipient"]);
AppHandler.EmailSender =   System.Configuration.ConfigurationManager.AppSettings["Email.Sender"];
AppHandler.EmailCopy = System.Configuration.ConfigurationManager.AppSettings["Email.Copy"];
AppHandler.EmailPriority = System.Configuration.ConfigurationManager.AppSettings["Email.Priority"];
AppHandler.EmailEncoding = System.Configuration.ConfigurationManager.AppSettings["Email.Encoding"];
AppHandler.EmailSubject = System.Configuration.ConfigurationManager.AppSettings["Email.Subject"];
AppHandler.EmailMessage = System.Configuration.ConfigurationManager.AppSettings["Email.Message"];
AppHandler.EmailSmtp = System.Configuration.ConfigurationManager.AppSettings["Email.Smtp"];
EmailHandler.SendEmail(AppHandler.EmailSmtp, AppHandler.EmailSender, AppHandler.EmailRecipient, AppHandler.EmailSubject, AppHandler.EmailMessage);
Kara Hewett is a software developer at RBS Greenwich Capital Markets. She earned a MBA in Information Systems, Finance and Accounting from New York University and a BS in Economics from the Wharton School of Business. Her past experience includes numerous consulting and development projects for SAC Capital, Lehman Brothers, J P Morgan Chase, American Stock Exchange, and Deutsche Bank.