How to use C # to access POP3 servers (cycles)

Source: Internet
Author: User
Tags mail connect readline
Access to | server//Hopefully through this article, you can write your own email client program in C #

This is a follow up to my SMTP example which shows how to access your POP3 server. This is the program connects and logs in to your POP3 server, and checks to the how-to-you many new messages.

The instantiation of the POP is in Main ():
Pop 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 into 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 is getnumberofnewmessages, which returns the number of unread to on the server. The response to the "stat" command are 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 ("Could 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 ("Could not connect to mail Server");
return 0;
}
}
public static void Main ()
{
Pop pop = new Pop ("Pop-server", "LoginName", "password");
Console.WriteLine ("New Messages = {0}", pop. Getnumberofnewmessages ());
Console.ReadLine ();
}
}


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.