// Hope that through this article, you can use C # to write your own email client program
This is a follow up to my SMTP example that shows how to access your POP3 server. This program connects and logs on to your POP3 server, and checks to see how many new messages you have.
The instantiation of the pop is in main () like this:
Pop = new POP ("pop-server", "loginname", "password"); you must replace "pop-server" with the name of your Pop server, "loginname" with your own log in, and "password" with your password. the class has two methods. the connect method takes care of actually logging in to the server. the tcpclient class is used to establish the connection. the "user" and "pass" commands are used to login. connect returns a networkstream object created during the connection process. the second method is getnumberofnewmessages, which returns the number of unread messages on the server. the response to the "stat" command is parsed to extract the number of new messages.
Requirement:
Requires. Net SDK
How to compile?
CSC/R: system. net. dll/R: system. Io. dll pop. CS
Source code
Using system. net. Sockets;
Using system. IO;
Using system. net;
Using system;
Class pop
{
String popserver;
String user;
String PWD;
Public POP (){}
Public POP (string server, string _ User, string _ PWD)
{
Popserver = server;
User = _ user;
Pwd = _ PWD;
}
Private networkstream connect ()
{
Tcpclient sender = new tcpclient (popserver, 110 );
Byte [] outbytes;
String input;
Networkstream NS = NULL;
Try {
NS = sender. getstream ();
Streamreader sr = new streamreader (NS );
Console. writeline (Sr. Readline ());
Input = "user" + User + "/R/N ";
Outbytes = system. Text. encoding. ASCII. getbytes (input. tochararray ());
NS. Write (outbytes, 0, outbytes. Length );
Console. writeline (Sr. Readline ());
Input = "pass" + PWD + "/R/N ";
Outbytes = system. Text. encoding. ASCII. getbytes (input. tochararray ());
NS. Write (outbytes, 0, outbytes. Length );
Console. writeline (Sr. Readline ());
Return ns;
}
Catch (invalidoperationexception IOE ){
Console. writeline ("cocould not connect to mail server ");
Return ns;
}
}
Public int getnumberofnewmessages ()
{
Byte [] outbytes;
String input;
Try {
Networkstream NS = connect ();
Streamreader sr = new streamreader (NS );
Input = "stat" + "/R/N ";
Outbytes = system. Text. encoding. ASCII. getbytes (input. tochararray ());
NS. Write (outbytes, 0, outbytes. Length );
String resp = Sr. Readline ();
Console. writeline (RESP );
String [] tokens = resp. Split (New char [] {''});
Input = "quit" + "/R/N ";
Outbytes = system. Text. encoding. ASCII. getbytes (input. tochararray ());
NS. Write (outbytes, 0, outbytes. Length );
Console. writeline (Sr. Readline ());
Sr. Close ();
NS. Close ();
Return tokens [1]. toint32 ();
}
Catch (invalidoperationexception IOE ){
Console. writeline ("cocould not connect to mail server ");
Return 0;
}
}
Public static void main ()
{
Pop = new POP ("pop-server", "loginname", "password ");
Console. writeline ("new messages = {0}", Pop. getnumberofnewmessages ());
Console. Readline ();
}
}