Click here to Skip to main content
11,573,935 members (57,999 online)
Click here to Skip to main content

Tagged as

Create a simple SMTP server in C#

, 20 Nov 2011 CPOL 65.6K 25
Rate this:
Please Sign up or sign in to vote.
A simple basic SMTP server for testing purposes
I created for testing purposes a simple SMTP server.

This is the client call:

 System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
 smtp.Send(message);//Handles all messages in the protocol
 smtp.Dispose();//sends a Quit message

This is the basic server code:
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 25);
TcpListener listener = new TcpListener(endPoint);
listener.Start();
 
while (true)
{
    TcpClient client = listener.AcceptTcpClient();
    SMTPServer handler = new SMTPServer();
    servers.Add(handler);
    handler.Init(client);
    Thread thread = new System.Threading.Thread(new ThreadStart(handler.Run));
    thread.Start();
}
The protocol has the following messages in this example:
C : EHLO
S: 250 Ok
 
C : MAIL FROM
S: 250 OK
 
C : RCPT TO
S: 250 OK
 
C :DATA
S: 354 Start mail input; end with <crlf>.<crlf>
 
DATA....
 
S: 250 OK
C : Quit
</crlf></crlf>

Basically the Run method contains this:
public void Run()
{
    Write("220 localhost -- Fake proxy server");
    string strMessage = String.Empty;
    while (true)
    {
        try
        {
            strMessage = Read();
        }
        catch(Exception e)
        {
            //a socket error has occured
            break;
        }
 
        if (strMessage.Length > 0)
        {
            if (strMessage.StartsWith("QUIT"))
            {
                client.Close();
                break;//exit while
            }
            //message has successfully been received
            if (strMessage.StartsWith("EHLO"))
            {
                Write("250 OK");
            }
 
            if (strMessage.StartsWith("RCPT TO"))
            {
                Write("250 OK");
            }
 
            if (strMessage.StartsWith("MAIL FROM"))
            {
 
               Write("250 OK");
            }
 
            if (strMessage.StartsWith("DATA"))
            {
                Write("354 Start mail input; end with"); 
                strMessage = Read();
                Write("250 OK");
            }
        }  
    }
}
 
private void Write(String strMessage)
{
    NetworkStream clientStream = client.GetStream();
    ASCIIEncoding encoder = new ASCIIEncoding();
    byte[] buffer = encoder.GetBytes(strMessage + "\r\n");
    
    clientStream.Write(buffer, 0, buffer.Length);
    clientStream.Flush();
}
 
private String Read()
{
    byte[] messageBytes = new byte[8192];
    int bytesRead = 0;
    NetworkStream clientStream = client.GetStream();
    ASCIIEncoding encoder = new ASCIIEncoding();
    bytesRead = clientStream.Read(messageBytes, 0, 8192);
    string strMessage = encoder.GetString(messageBytes, 0, bytesRead);
    return strMessage;
}
 

Happy coding!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

roosrj
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

 
NewsSure would be handy to know... Pin
countrysideflair23-May-13 14:15
membercountrysideflair23-May-13 14:15 
GeneralMy vote of 2 Pin
perilbrain16-Feb-13 1:57
memberperilbrain16-Feb-13 1:57 
Suggestionthanks i am now going to design smtpserver.in powered relay Pin
smtpserver.in7-Nov-12 20:47
membersmtpserver.in7-Nov-12 20:47 
Questionplease some one help me how to test dkim with smtpserver relay Pin
smtpserver.in22-Oct-12 4:12
membersmtpserver.in22-Oct-12 4:12 
Questionsmtp server in c Pin
Igede Hardisurya Budiana7-Oct-12 15:19
memberIgede Hardisurya Budiana7-Oct-12 15:19 
QuestionOne more update to my alternate version - full source Pin
Mark Harmon11-Sep-12 10:15
memberMark Harmon11-Sep-12 10:15 
I've added a few things to make this super extra usable, at least for myself. These code changes will add the ability to write the emails to seperate html files, so that you can see the emails and test links and stuff like that. It also fixes some problems with decoding the text. Anyways, here is all the code.

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
 
namespace FakeSmtp
{
    public class MailListener : TcpListener
    {
        private TcpClient client;
        private NetworkStream stream;
        private System.IO.StreamReader reader;
        private System.IO.StreamWriter writer;
        private Thread thread = null;
        private SMTPServer owner;
        const string SUBJECT = "Subject: ";
        const string FROM = "From: ";
        const string TO = "To: ";
        const string MIME_VERSION = "MIME-Version: ";
        const string DATE = "Date: ";
        const string CONTENT_TYPE = "Content-Type: ";
        const string CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding: ";        
 

        public MailListener(SMTPServer aOwner, IPAddress localaddr, int port)
            : base(localaddr, port)
        {
            owner = aOwner;
        }
 
        new public void Start()
        {
            base.Start();
 
            client = AcceptTcpClient();
            client.ReceiveTimeout = 5000;
            stream = client.GetStream();
            reader = new System.IO.StreamReader(stream);
            writer = new System.IO.StreamWriter(stream);
            writer.NewLine = "\r\n";
            writer.AutoFlush = true;
 
            thread = new System.Threading.Thread(new ThreadStart(RunThread));
            thread.Start();
        }
 
        protected void RunThread()
        {
            string line = null;
 
            writer.WriteLine("220 localhost -- Fake proxy server");
 
            try
            {
                while (reader != null)
                {
                    line = reader.ReadLine();
                    Console.Error.WriteLine("Read line {0}", line);
 
                    switch (line)
                    {
                        case "DATA":
                            writer.WriteLine("354 Start input, end data with <CRLF>.<CRLF>");
                            StringBuilder data = new StringBuilder();
                            String subject = "";
                            string from = "";
                            string to = "";
                            string mimeVersion = "";
                            string date = "";
                            string contentType = "";
                            string contentTransferEncoding = "";
 
                            line = reader.ReadLine();
 
                            while (line != null && line != ".")
                            {
                                if (line.StartsWith(SUBJECT))
                                {
                                    subject = line.Substring(SUBJECT.Length);
                                }
                                else if (line.StartsWith(FROM))
                                {
                                    from = line.Substring(FROM.Length);
                                }
                                else if (line.StartsWith(TO))
                                {
                                    to = line.Substring(TO.Length);
                                }
                                else if (line.StartsWith(MIME_VERSION))
                                {
                                    mimeVersion = line.Substring(MIME_VERSION.Length);
                                }
                                else if (line.StartsWith(DATE))
                                {
                                    date = line.Substring(DATE.Length);
                                }
                                else if (line.StartsWith(CONTENT_TYPE))
                                {
                                    contentType = line.Substring(CONTENT_TYPE.Length);
                                }
                                else if (line.StartsWith(CONTENT_TRANSFER_ENCODING))
                                {
                                    contentTransferEncoding = line.Substring(CONTENT_TRANSFER_ENCODING.Length);
                                }
                                else
                                {
                                    data.AppendLine(line);
                                }
 
                                line = reader.ReadLine();
                            }
 
                            String message = data.ToString();
 
                            WriteMessage(from, to, subject, message, contentType, contentTransferEncoding);
 
                            writer.WriteLine("250 OK");
                            break;
 
                        case "QUIT":
                            writer.WriteLine("250 OK");
                            reader = null;
                            break;
 
                        default:
                            writer.WriteLine("250 OK");
                            break;
                    }
                }
            }
            catch (IOException)
            {
                Console.WriteLine("Connection lost.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                client.Close();
                Stop();
            }
        }
 
        private string DecodeQuotedPrintable(string input)
        {
            var occurences = new Regex(@"(=[0-9A-Z][0-9A-Z])+", RegexOptions.Multiline);
            var matches = occurences.Matches(input);
            foreach (Match m in matches)
            {
                byte[] bytes = new byte[m.Value.Length / 3];
                for (int i = 0; i < bytes.Length; i++)
                {
                    string hex = m.Value.Substring(i * 3 + 1, 2);
                    int iHex = Convert.ToInt32(hex, 16);
                    bytes[i] = Convert.ToByte(iHex);
                }
                input = input.Replace(m.Value, Encoding.Default.GetString(bytes));
            }
            return input.Replace("=\r\n", "");
        }
 
        private void WriteMessage(string from, string to, string subject, string message, string contentType, string transferEncoding)
        {
            if (transferEncoding == "quoted-printable")
            {
                message = DecodeQuotedPrintable(message);
            }
 
            if (OutputToFile)
            {
                string header = string.Format("<strong>FROM: </strong>{0}<br/><strong>TO: </strong>{1}<br/><strong>SUBJECT: </strong>{2}<br/><br/>",
                    new object[] { from, to, subject });
                string docText = string.Format("<html><body>{0}{1}</body></html>", header, message);
 
                // Create a file to write to.
                string path = string.Format("mail_{0}.html", DateTime.Now.ToFileTimeUtc());
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.Write(docText);
                }
            }
 
            Console.Error.WriteLine("===============================================================================");
            Console.Error.WriteLine("Received ­email");
            Console.Error.WriteLine("Type: " + contentType);
            Console.Error.WriteLine("Encoding: " + transferEncoding);
            Console.Error.WriteLine("From: " + from);
            Console.Error.WriteLine("To: " + to);
            Console.Error.WriteLine("Subject: " + subject);
            Console.Error.WriteLine("-------------------------------------------------------------------------------");
            Console.Error.WriteLine(message);
            Console.Error.WriteLine("===============================================================================");
            Console.Error.WriteLine("");
        }
 
        public bool OutputToFile { get; set; }
 
        public bool IsThreadAlive
        {
            get { return thread.IsAlive; }
        }
    }
}

Use it in your console app like this.

using System;
using System.Net;
using System.Threading;
 
namespace FakeSmtp
{
    public class SMTPServer
    {
        [STAThread] 
        static void Main(string[] args)
        {
            SMTPServer server = new SMTPServer();
            server.RunServer();
        }
 
        public void RunServer()
        {
            MailListener listener = null;
 
            do
            {
                Console.WriteLine("New MailListener started");
                listener = new MailListener(this, IPAddress.Loopback, 25);
                listener.OutputToFile = true;
                listener.Start();
                while (listener.IsThreadAlive)
                {
                    Thread.Sleep(500);
                }
            } while (listener != null);
 
        }
    }
}

AnswerRe: One more update to my alternate version - full source Pin
ObiWan_MCC17-Sep-12 3:42
memberObiWan_MCC17-Sep-12 3:42 
QuestionAnother alternate - full source Pin
Mark Harmon11-Sep-12 8:07
memberMark Harmon11-Sep-12 8:07 
GeneralRe: Another alternate - full source Pin
partha chakraborti14-Jan-15 19:46
memberpartha chakraborti14-Jan-15 19:46 
SuggestionSome suggestion to improve your tip Pin
ObiWan_MVP3-Sep-12 6:13
memberObiWan_MVP3-Sep-12 6:13 
GeneralReason for my vote of 5 Thanks man! It works but my firewall... Pin
finkos24-Nov-11 8:31
memberfinkos24-Nov-11 8:31 
GeneralYou did not define the server variable in your code. What is... Pin
finkos22-Nov-11 14:51
memberfinkos22-Nov-11 14:51 
GeneralReason for my vote of 5 good - i'll give it a try! Pin
johannesnestler22-Nov-11 7:13
memberjohannesnestler22-Nov-11 7:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 21 Nov 2011
Article Copyright 2011 by roosrj
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid