473,498 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.ToCharArr ay());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "PASS " + _Pwd + CRLF;
szData =
System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
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.ToCharArr ay());
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.ToCharArr ay());
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.ToCharArr ay());
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
Oct 13 '06 #1
1 5668
Hi,

I use http://www.codeproject.com/cs/intern...pop3client.asp, which caters
for all my requirements, hope this helps you too.

Cheers,
Quentin

"Prasanta" <Pr******@discussions.microsoft.comwrote in message
news:23**********************************@microsof t.com...
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.ToCharArr ay());
NetStrm.Write(szData, 0, szData.Length);
Console.WriteLine(RdStrm.ReadLine());

Data = "PASS " + _Pwd + CRLF;
szData =
System.Text.Encoding.ASCII.GetBytes(Data.ToCharArr ay());
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.ToCharArr ay());
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.ToCharArr ay());
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.ToCharArr ay());
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

Oct 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2295
by: Programmer | last post by:
Hi all I wan't to know if i'm able to read mail from a mail server. My mail server is a pop3 server (UNIX) and i want to be able to get the mails from an aspx or an asmx. with out using external...
1
3245
by: oni | last post by:
hi all, the program should be able to log on to a pop mail account and need to just read its headers and display. any help in this regard highly apperecialted. code pref VB.NET thankz in...
3
1775
by: jamal | last post by:
HI guys I want to make a program that will read my external pop3 e-mail account and save them to an access/sql file on my workstation. what you guys think ? Can I make something like this with...
2
2621
by: Abhi | last post by:
i need to create a user in web mail using dotnet coad by using webmailserver api ex: xxx is my webmail then i want to create to a user in that webmail xxx ex: yyy@xxx.com how i should do in...
1
11291
by: Prasanta | last post by:
Hello, Please cnay one can tell me how to read mail as formatted.... i have made some code using that able to read but not able to serialize..... so am i need to parse the HTML, or is there any...
0
1005
by: aknigam1 | last post by:
Hello, I make a web application for receive mail from POP3 from the our domain by passing our pop3 server name and our user id and password . And i'm able to read (receive) only mail from Inbox....
4
6276
by: =?Utf-8?B?QWxwYW5h?= | last post by:
I am making a thin email client and want to get emails from a pop3 server...Is there any built in support in C# to get emails from a pop3 server and parse the email to show up on the UI ?
1
457
by: rodny.romero | last post by:
Somebody can help me with an example of like Reading post office with VB.Net using protocol POP3, I have obtained the commandos or instructions that protocol POP3 supports, but I have not been able...
3
11471
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I'm creating an application that will read emails from GMail, using the System.Net.Sockets.TcpClient and POP protocol. However, I am having a problem with my SslStream. ...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7203
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.