Click here to Skip to main content
Click here to Skip to main content

Create a simple SMTP server in C#

By , 20 Nov 2011
 
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)

About the Author

roosrj
Netherlands Netherlands
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
NewsSure would be handy to know...membercountrysideflair6hrs 40mins ago 
That Operations Manager 2007 R2 SDK is needed in order to get a hold of the microsoft.enterprisemanagement.operationsmanager.dll required for the SMTPServer class.
 
Not sure, but feels like it's not free to download from microsoft.com
 
So, sadly, this is no good except for those in that rare case. WTF | :WTF:
GeneralMy vote of 2memberperilbrain16 Feb '13 - 1:57 
No Source just nothing..
Suggestionthanks i am now going to design smtpserver.in powered relaymembersmtpserver.in7 Nov '12 - 20:47 
when the system is ready i come back and post the source code.
Questionplease some one help me how to test dkim with smtpserver relaymembersmtpserver.in22 Oct '12 - 4:12 
hi this is ranvir from smtpserver.in and would like to know how to generate dkim so we can use that with our mail relay
Questionsmtp server in cmemberIgede Hardisurya Budiana7 Oct '12 - 15:19 
does anyone have a code smtp server in c?
 
I am very needed it.
 
please help me...
thank you
QuestionOne more update to my alternate version - full sourcememberMark 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 sourcememberObiWan_MCC17 Sep '12 - 3:42 
Since we're at this, I posted my own critter here on CodeProject, you'll find the full code here (A C# SMTP server (receiver)[^]) and I hope it may fit your needs Wink | ;)
QuestionAnother alternate - full sourcememberMark Harmon11 Sep '12 - 8:07 
First off, thanks for sharing the code. You definitely got me over the hump. I have implemented my own version of this based on your code. The problem I was having is that some mail clients use the same connection for multiple messages. Also, not all mail clients send a QUIT message. So I changed the code to not automatically close the connection on QUIT and also to handle a connection timing out.
 
The class is called MailListener (not claiming most accurate name here Smile | :) ).
 
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.IO;
 
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;
 
        public bool IsThreadAlive
        {
            get { return thread.IsAlive; }
        }
 
        public MailListener(IPAddress localaddr, int port)
            : base(localaddr, port)
        {
        }
 
        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 = "";
                            line = reader.ReadLine();
 
                            if (line != null && line != ".")
                            {
                                const string SUBJECT = "Subject: ";
                                if (line.StartsWith(SUBJECT))
                                {
                                    subject = line.Substring(SUBJECT.Length);
                                }
                                else
                                {
                                    data.AppendLine(line);
                                }
 
                                for (line = reader.ReadLine(); line != null && line != "."; line = reader.ReadLine())
                                {
                                    data.AppendLine(line);
                                }
                            }
 
                            String message = data.ToString();
                            Console.Error.WriteLine("Received ­ email with subject: {0} and message: {1}", subject, message);
                            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();
            }
        }
    }
}
 
As you can see in the previous class, the thread management is now being handled by the MailListener class.
 
In a console application, the program.cs file would look like this.
 
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace FakeSmtp
{
    public class SMTPServer
    {
        static void Main(string[] args)
        {
            MailListener listener = null;
 
            do
            {
                Console.WriteLine("New MailListener started");
                listener = new MailListener(IPAddress.Loopback, 25);
                listener.Start();
                while (listener.IsThreadAlive)
                {
                    Thread.Sleep(500);
                }
            } while (listener != null);
        }
    }
}
 
So there you have it. This is not the best way to architect this thing, but I was trying to spend as little time as possible to get the functionality I needed.
SuggestionSome suggestion to improve your tipmemberObiWan_MVP3 Sep '12 - 6:13 
First of all, I think that you left out too much stuff from your example and this, in turn, makes it difficult to understand; it would have been a better idea adding some "full" code like the one shown here[^]; then, it would be useful to add some code to the "Run()" method to set the client socket "ReceiveTimeout" property (see here[^]) to a value different from "infinite" (aka zero); for example 5000, this way, a client connecting to the server and idling withouth sending in any command for some time (five seconds in my example) would be automatically disconnected.
 
just to be clear, here's the modified code snippet from the first URL above
 
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
 
namespace FakeSMTP
{
    public class SMTPServer
    {
        TcpClient client;
        NetworkStream stream;
        System.IO.StreamReader reader;
        System.IO.StreamWriter writer;
        
        public SMTPServer(TcpClient client)
        {
            this.client = client;
            this.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;
        }
 
        static void Main(string[] args)
        {
            TcpListener listener = new TcpListener(IPAddress.Loopback,25);
            listener.Start();
            while (true)
            {
                SMTPServer handler = new SMTPServer(listener.AcceptTcpClient());
                Thread thread = new System.Threading.Thread(new ThreadStart(handler.Run));
                thread.Start();
            }
        }
        
        public void Run()
        {
            writer.WriteLine("220 localhost -- Fake proxy server");
            for (string line = reader.ReadLine(); line != 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 = "";
                        line = reader.ReadLine();
                        if (line != null && line != ".")
                        {
                            const string SUBJECT = "Subject: ";
                            if (line.StartsWith(SUBJECT))
                                subject = line.Substring(SUBJECT.Length);
                            else data.AppendLine(line);
                            for (line = reader.ReadLine();
                                line != null && line != ".";
                                line = reader.ReadLine())
                            {
                                data.AppendLine(line);
                            }
                        }
                        String message = data.ToString();
                        Console.Error.WriteLine("Received ­ email with subject: {0} and message: {1}",
                            subject, message);
                        writer.WriteLine("250 OK");
                        client.Close();
                        return;
                    default:
                        writer.WriteLine("250 OK");
                        break;
                }
            }
        }
    }
}
 
I admit that the above code is simpler and probably less elegant than yours, but at least it helps understanding how the whole thing works and, the timeout setting avoids ending up with a bunch of sockets idling and doing nothing Smile | :)
 
HTH
GeneralReason for my vote of 5 Thanks man! It works but my firewall...memberfinkos24 Nov '11 - 8:31 
Reason for my vote of 5
Thanks man! It works but my firewall is barking at IPAddress.Any.
So I replaced it with
 
IPAddress address = null;
if (IPAddress.TryParse("127.0.0.1", out address))
{
//IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 25);
IPEndPoint endPoint = new IPEndPoint(address, 25);
listener = new TcpListener(endPoint);
}

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 21 Nov 2011
Article Copyright 2011 by roosrj
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid