Solved

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

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

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 25
Testing of the web services 1 34
Generate Unique ID in VB.NET 21 62
looking for an ASP.net report designer 4 11
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Network ports are the threads that hold network communication together. They are an essential part of networking that can be easily ignore or misunderstood, my goals is to show those who don't have a strong network foundation how network ports opera…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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