Solved

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

Posted on 2011-03-16
3
1,208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35149477
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
ID: 35153476
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
ID: 35156575
Setup an e-mail client that points to your computer's IP address and port 8000, then send an e-mail.
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

734 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