Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
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…

809 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