Solved

.net code to listen to a port and catch smtp messages

Posted on 2011-03-16
3
1,131 Views
Last Modified: 2012-05-11
i am using the below c# code as a console app to try to listen to port 8000 and parse smtp messages into parts like body, from, to...

when i run the service and the TcpListener class to get this done. but when i try to send mail to the listener it fails. in debugging i am using netstat -anb to show me stats and i dont see a listener on port 8000.

how can i trouble shoot this and/ or get a c# service or something to listen to a post for smtp messages?


 
TcpListener listener = new TcpListener(IPAddress.Loopback, 8000);
TcpClient client;
NetworkStream ns;

listener.Start();

Console.WriteLine("Awaiting connection...");
client = listener.AcceptTcpClient();
Console.WriteLine("Connection accepted!");

ns = client.GetStream();

using (StreamWriter writer = new StreamWriter(ns))
{
writer.WriteLine("220 localhost SMTP server ready.");
writer.Flush();

using (StreamReader reader = new StreamReader(ns))
{
    string response = reader.ReadLine();

    if (!response.StartsWith("HELO") && !response.StartsWith("EHLO"))
    {
    writer.WriteLine("500 UNKNOWN COMMAND");
    writer.Flush();
    ns.Close();
    return;
    }

    string remote = response.Replace("HELO", string.Empty).Replace("EHLO", string.Empty).Trim();

    writer.WriteLine("250 localhost Hello " + remote);
    writer.Flush();

    response = reader.ReadLine();

    if (!response.StartsWith("MAIL FROM:"))
    {
    writer.WriteLine("500 UNKNOWN COMMAND");
    writer.Flush();
    ns.Close();
    return;
    }

    remote = response.Replace("RCPT TO:", string.Empty).Trim();
    writer.WriteLine("250 " + remote + " I like that guy too!");
    writer.Flush();

    response = reader.ReadLine();

    if (!response.StartsWith("RCPT TO:"))
    {
    writer.WriteLine("500 UNKNOWN COMMAND");
    writer.Flush();
    ns.Close();
    return;
    }

    remote = response.Replace("MAIL FROM:", string.Empty).Trim();
    writer.WriteLine("250 " + remote + " I like that guy!");
    writer.Flush();

    response = reader.ReadLine();

    if (response.Trim() != "DATA")
    {
    writer.WriteLine("500 UNKNOWN COMMAND");
    writer.Flush();
    ns.Close();
    return;
    }

    writer.WriteLine("354 Enter message. When finished, enter \".\" on a line by itself");
    writer.Flush();

    int counter = 0;
    StringBuilder message = new StringBuilder();

    while ((response = reader.ReadLine().Trim()) != ".")
    {
    message.AppendLine(response);
    counter++;

    if (counter == 1000000)
    {
        ns.Close();
        return;  // Seriously? 1 million lines in a message?
    }
    }

    writer.WriteLine("250 OK");
    writer.Flush();
    ns.Close();
    // Insert "message" into DB
    Console.WriteLine("Received message:");
    Console.WriteLine(message.ToString());
}
}

Console.ReadKey();

Open in new window

0
Comment
Question by:realcoding
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
Comment Utility
Your listener is set to listen only on the loopback address, 127.0.0.1.  You probably want that first line to be:
TcpListener listener = new TcpListener(IPAddress.Any, 8000);

Assuming you want it to accept connections on any of the IP addresses configured on the system, otherwise you can specify one particular IP address.

Note that netstat will show the port in LISTENING mode, you just need to scroll far enough to see it (it's with the other entries for 127.0.0.1):
[avp.exe]
 TCP    127.0.0.1:1163         0.0.0.0:0              LISTENING
[htcUPCTLoader.exe]
 TCP    127.0.0.1:1201         127.0.0.1:1110         ESTABLISHED
[MOE.exe]
 TCP    127.0.0.1:5679         0.0.0.0:0              LISTENING
 WcesComm
[svchost.exe]
 TCP    127.0.0.1:5939         0.0.0.0:0              LISTENING
[TeamViewer_Service.exe]
 TCP    127.0.0.1:7438         0.0.0.0:0              LISTENING
 WcesComm
[svchost.exe]
 TCP    127.0.0.1:8000         0.0.0.0:0              LISTENING
0
 

Author Comment

by:realcoding
Comment Utility
i see. any idea how i can now get an smtp message sent to my ip address over port 8000 to test my listener?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
Comment Utility
Setup an e-mail client that points to your computer's IP address and port 8000, then send an e-mail.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now