Solved

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

Posted on 2011-03-16
3
1,191 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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