Custom SMTP in C#

Getting Started

And here goes. I started by writing a custom exception for my C# Smtp class. Whenever my C# class encounters faults, I'll throw an instance of this exception.

public class SmtpException : System.Exception
{
    private string message;
    public SmtpException( string str)
    {
        message = str;
    }
    public string What()
    {
        return message;
    }
};

Our class will be inherited from the System.Net.Socket.TcpClient class in the dotNET framework. This class provides all the base functionality that is required to do TCP/ IP programming. Our class declaration is presented below.

public class Smtp : System.Net.Sockets.TcpClient

Our class will have eight data members. The from data member is the sender of the email. The to, cc and bcc data members represent the recipients of the email. The subject data member is the subject of the email. Either bodyText or bodyHtml will represent the body of the email. The server data member represents the SMTP server where our client is sending the message. Here are the data member definitions.

public class Smtp : System. Net. Sockets. TcpClient
{
    public string from = null;
    public ArrayList to;
    public ArrayList cc;
    public ArrayList bcc;
    public string subject = null;
    public string bodyText = null;
    public string bodyHtml = null;
    public string server = null;

The to, cc and bcc data members are arrays, in order to allow multiple recipients of all three types. All three arrays must be created in the constructor of our class.

public Smtp()
{
    to = new ArrayList();
    cc = new ArrayList();
    bcc = new ArrayList();
}

You might also like...

Comments

About the author

Randy Charles Morin

Randy Charles Morin Canada

Randy's article are Copyright 1998-2003 Randy Charles Morin

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Memory is like an orgasm. It's a lot better if you don't have to fake it.” - Seymour Cray