SIGN UP MEMBER LOGIN:    
ARTICLE

Simple SMTP Mailer

Posted by Bill Farley Articles | Internet & Web March 26, 2001
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.
Reader Level:
Download Files:
 

Description:

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("bfarley@cfl.rr.com","bfarley@cfl.rr.com", "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! 

Requirement:

Requires .NET SDK

How To Compile?

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

Login to add your contents and source code to this article
share this article :
post comment
 

Hello I tried the script and, in my case, it send the subject, but I don't get any body text. Cheers

Posted by ruben nroot Mar 25, 2011
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor