Simple SMTP Mailer


This is a simple program which shows how to send mail via SMTP without using any framework support. While using the .NET SMTP classes may be a little easier, it is good to be able to go under the hood and get your hands dirty to understand what is going on at the socket level. For simple text messages, this implementation works fine. The TCPSocket class is used to establish the connection to the mail server. This class extends the basic Socket class to include everything necessary for the TCP protocol.

The SMTPMailer class is instantiated in Main like this:

SMTPMailer mlr = new SMTPMailer("server-name", "host");

You must replace "server-name" with the name of your mail server and "host" with your host name. A Message class is used as a simple wrapper for the basic parts of an email message. The class uses properties to get and set the message parts. The message is created in Main as well: Message msg = new Message("","", "Hello!", "This is a test..."); The first argument is the recipient, the second argument gets replaced with your email address (from), the third argument is the subject, and the fourth is a string with the body of the message. The SMTPMailer class has a single method - Send. An inspection of this method shows a simple walk thru the SMTP protocol. You can see the protocol is command/response based, meaning the client sends a text message and the SMTP server responds. When the program is run, all the responses from the server are echoed to the console. The next submission will cover the POP3 protocol to check messages. If you use this program, please leave my email as the recipient for a message, so I can see if anybody finds this useful! 


Requires .NET SDK

How To Compile?

csc /r:System.Net.dll /r:System.IO.dll mail.cs