Click here to Skip to main content
15,178,226 members
Articles / Programming Languages / C#
Posted 20 Nov 2011

Tagged as


29 bookmarked

Create a simple SMTP server in C#

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
20 Nov 2011CPOL
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);

while (true)
    TcpClient client = listener.AcceptTcpClient();
    SMTPServer handler = new SMTPServer();
    Thread thread = new System.Threading.Thread(new ThreadStart(handler.Run));

The protocol has the following messages in this example:
S: 250 Ok

S: 250 OK

S: 250 OK

S: 354 Start mail input; end with <crlf>.<crlf>


S: 250 OK
C : Quit

Basically the Run method contains this:
public void Run()
    Write("220 localhost -- Fake proxy server");
    string strMessage = String.Empty;
    while (true)
            strMessage = Read();
        catch(Exception e)
            //a socket error has occured

        if (strMessage.Length > 0)
            if (strMessage.StartsWith("QUIT"))
                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);

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!


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


About the Author

Netherlands Netherlands
No Biography provided

Comments and Discussions

Questionwhat about the mail queue? Pin
Pal Sayantan15-Mar-18 0:47
MemberPal Sayantan15-Mar-18 0:47 
GeneralMy vote of 1 Pin
Jason McBurney5-Mar-18 7:02
MemberJason McBurney5-Mar-18 7:02 
NewsSure would be handy to know... Pin
countrysideflair23-May-13 15:15
Membercountrysideflair23-May-13 15:15 
GeneralMy vote of 2 Pin
perilbrain16-Feb-13 2:57
Memberperilbrain16-Feb-13 2:57 
GeneralRe: My vote of 2 Pin
Jason McBurney5-Mar-18 7:01
MemberJason McBurney5-Mar-18 7:01 
Suggestionthanks i am now going to design powered relay Pin
smtpserver.in7-Nov-12 21:47
Membersmtpserver.in7-Nov-12 21:47 
Questionplease some one help me how to test dkim with smtpserver relay Pin
smtpserver.in22-Oct-12 5:12
Membersmtpserver.in22-Oct-12 5:12 
Questionsmtp server in c Pin
Igede Hardisurya Budiana7-Oct-12 16:19
MemberIgede Hardisurya Budiana7-Oct-12 16:19 
QuestionOne more update to my alternate version - full source Pin
Al Forno11-Sep-12 11:15
MemberAl Forno11-Sep-12 11:15 
AnswerRe: One more update to my alternate version - full source Pin
ObiWan_MCC17-Sep-12 4:42
MemberObiWan_MCC17-Sep-12 4:42 
QuestionAnother alternate - full source Pin
Al Forno11-Sep-12 9:07
MemberAl Forno11-Sep-12 9:07 
GeneralRe: Another alternate - full source Pin
partha chakraborti14-Jan-15 20:46
Memberpartha chakraborti14-Jan-15 20:46 
AnswerRe: Another alternate - full source Pin
Member 1185245119-Jul-16 7:05
MemberMember 1185245119-Jul-16 7:05 
SuggestionSome suggestion to improve your tip Pin
ObiWan_MCC3-Sep-12 7:13
MemberObiWan_MCC3-Sep-12 7:13 
GeneralReason for my vote of 5 Thanks man! It works but my firewall... Pin
finkos24-Nov-11 9:31
Memberfinkos24-Nov-11 9:31 
GeneralYou did not define the server variable in your code. What is... Pin
finkos22-Nov-11 15:51
Memberfinkos22-Nov-11 15:51 
GeneralReason for my vote of 5 good - i'll give it a try! Pin
johannesnestler22-Nov-11 8:13
Memberjohannesnestler22-Nov-11 8:13 

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

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