How to use C # to access a POP3 server)

Source: Internet
Author: User
 

// 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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.