How to read mail from mail server (Pop3) using TCPClient

G

Guest

Hello,

How to read a mail from the mail server as formatted. I am able to read the
mail as row HTML, is there any class, or something method using that i can
easily get the all info of that mail, like as TO,From,Subject,Body. here I am
attaching my coe which i developed.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Web.Mail;

namespace testMail
{
class Program
{
static void Main(string[] args)
{
try
{
TcpClient Server;
NetworkStream NetStrm;
StreamReader RdStrm;
string Data;
byte[] szData;
string CRLF = "\r\n";

if (args.Length < 3)
return;

string _Server = args[0];
string _User = args[1];
string _Pwd = args[2];
string status;
// create server POP3 with port 110
Server = new TcpClient(_Server, 110);


NetStrm = Server.GetStream();
RdStrm = new StreamReader(Server.GetStream());
Console.WriteLine(RdStrm.ReadLine());

// Login Process
Data = "USER " + _User + CRLF;
szData =
System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "PASS " + _Pwd + CRLF;
szData =
System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

// Send STAT command to get information ie: number of mail
and size
Data = "STAT" + CRLF;
szData =
System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);
string messagesC = RdStrm.ReadLine();
Console.WriteLine(messagesC);

int messageCounter = int.Parse(messagesC.Split(' ')[1]);
string szTemp;
string message="";
// retrieve mail with number mail parameter
Data = "RETR " + messageCounter + CRLF;
szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData,0,szData.Length);

szTemp = RdStrm.ReadLine();
if(szTemp[0]!='-')
{

while(szTemp!=".")
{
message += szTemp+CRLF;
szTemp = RdStrm.ReadLine();
}
}
else
{
Console.WriteLine(szTemp);
}



Console.WriteLine(message);
// Send QUIT command to close session from POP server
Data = "QUIT" + CRLF;
szData =
System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

//close connection
NetStrm.Close();
RdStrm.Close();

}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Finished....");
Console.ReadLine();
}
}
}

waiting for the reply....

Thanks
Prasanta
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top