Asp. NET read POP3 message operations

Source: Internet
Author: User
Tags getstream mail connect readline tostring valid
asp.net
Namespace Pop3client
{
Using System.IO;
Using System.Net;
Using System.Net.Sockets;
Please note this all code being Copyright 2002 by William J. Dean
public class Pop3client
{
public enum connect_state {disc,authorization,transaction,update};
public string User;
public string pwd;
public string Pop;
public bool error;
Public Connect_state State=connect_state.disc;

Borrowed from Agus Kurniawan's article: "Retrieve Mail from a POP3 Server Using C #" at http://www.codeproject.com/csharp/ Popapp.asp
Private TcpClient Server;
Private NetworkStream NETSTRM;
Private StreamReader RDSTRM;
private string Data;
Private byte[] szdata;
private string CRLF = "";

Public Pop3client ()
{
Nothing todo. Just create to Object
}

Public pop3client (string pop_server,string user_name,string password)
{
Put the specied server (pop_server), User (user_name) and password (password)
into the appropriate properties.
Pop=pop_server;
User=user_name;
Pwd=password;
}

#region Utility Methods, some public, some private
public string Connect (string pop_server)
{
Pop=pop_server; Put the specified server into the Pop property
return (connect ()); Call the Connect method
}
public string Connect ()
{
Initialize to the POP server. This code snipped "borrowed"
With some modifications ...
From the article ' Retrieve Mail from a POP3 Server Using C # ' at
www.codeproject.com by Agus Kurniawan
Http://www.codeproject.com/csharp/popapp.asp

Create server with Port 110
Server = new TcpClient (pop,110);

Try
{
Initialization
NETSTRM = Server.getstream ();
rdstrm= New StreamReader (Server.getstream ());

The pop session was now on the AUTHORIZATION state
State=connect_state. AUTHORIZATION;
Return (Rdstrm.readline ());
}
catch (InvalidOperationException err)
{
Return ("Error:" +err.) ToString ());
}

}
private String Disconnect ()
{
String temp= "disconnected successfully.";
if (state!=connect_state.disc)
{

Close connection
Netstrm.close ();
Rdstrm.close ();
State=connect_state.disc;
}
Else
{
Temp= "Not Connected."
}
return (temp);
}

private void Issue_command (String command)
{
Send the command to the POP server. This code snipped "borrowed"
With some modifications ...
From the article ' Retrieve Mail from a POP3 Server Using C # ' at
www.codeproject.com by Agus Kurniawan
Http://www.codeproject.com/csharp/popapp.asp
data= COMMAND + CRLF;
Szdata = System.Text.Encoding.ASCII.GetBytes (Data.tochararray ());
Netstrm.write (szdata,0,szdata.length);

}
private String Read_single_line_response ()
{
Read the response of the POP server. This code snipped "borrowed"
With some modifications ...
From the article ' Retrieve Mail from a POP3 Server Using C # ' at
www.codeproject.com by Agus Kurniawan
Http://www.codeproject.com/csharp/popapp.asp
string temp;
Try
{
temp = Rdstrm.readline ();
Was_pop_error (temp);
return (temp);
}
catch (InvalidOperationException err)
{
Return ("Error in Read_single_line_response ():" + Err. ToString ());
}

}
private String Read_multi_line_response ()
{
Read the response of the POP server. This code snipped "borrowed"
With some modifications ...
From the article ' Retrieve Mail from a POP3 Server Using C # ' at
www.codeproject.com by Agus Kurniawan
Http://www.codeproject.com/csharp/popapp.asp
String temp= "";
String sztemp;

Try
{
Sztemp = Rdstrm.readline ();
Was_pop_error (sztemp);
if (!error)
{

while (sztemp!= ".")
{
temp = SZTEMP+CRLF;
Sztemp = Rdstrm.readline ();
}
}
Else
{
Temp=sztemp;
}
return (temp);
}
catch (InvalidOperationException err)
{
Return ("Error in Read_multi_line_response ():" + Err. ToString ());
}
}
private void Was_pop_error (string response)
{
Detect if the POP server that issued the response believes that
An error has occured.

if (response. StartsWith ("-"))
{
If the character of the response is "-" then the
POP server has encountered an error executing the last
command send by the client
Error=true;
}
Else
{
Success
Error=false;
}
}
#endregion
#region POP Commands
public string DELE (int msg_number)
{
string temp;

if (state!= connect_state. TRANSACTION)
{
DELE is only valid when the POPs session was in the TRANSACTION state
temp= "Connection State not = TRANSACTION";
}
Else
{
Issue_command ("DELE" + msg_number). ToString ());
Temp=read_single_line_response ();
}
return (temp);
}

public string LIST ()
{
String temp= "";
if (state!= connect_state. TRANSACTION)
{
The pop command LIST is only valid in the TRANSACTION state
temp= "Connection State not = TRANSACTION";
}
Else
{
Issue_command ("LIST");
Temp=read_multi_line_response ();
}
return (temp);
}

public string LIST (int msg_number)
{
String temp= "";

if (state!= connect_state. TRANSACTION)
{
The pop command LIST is only valid in the TRANSACTION state
temp= "Connection State not = TRANSACTION";
}
Else
{
Issue_command ("LIST" + msg_number. ToString ());
Temp=read_single_line_response (); When the message number is supplied, expect a single line response
}
return (temp);

}

public string NOOP ()
{
string temp;
if (state!= connect_state. TRANSACTION)
{
The pop command NOOP is only valid into the TRANSACTION state
temp= "Connection State not = TRANSACTION";
}
Else
{
Issue_command ("NOOP");
Temp=read_single_line_response ();

}
return (temp);

}
public String Pass ()
{
string temp;
if (state!= connect_state. AUTHORIZATION)
{
The pop command pass are only valid in the AUTHORIZATION state
temp= "Connection State not = AUTHORIZATION";
}
Else
{
if (pwd!=null)
{
Issue_command ("pass" + pwd);
Temp=read_single_line_response ();

if (!error)
{
Transition to the Transaction state
State=connect_state. TRANSACTION;
}
}
Else
{
temp= "No Password set."
}
}
return (temp);
}
public string Pass (string password)
{
Pwd=password; Put the supplied password in the appropriate property
Return (pass ()); Call Pass () with no arguement
}

public string QUIT ()
{
QUIT is valid in all pop states

string temp;
if (state!=connect_state.disc)
{
Issue_command ("QUIT");
Temp=read_single_line_response ();
Temp + + CRLF + disconnect ();

}
Else
{
Temp= "Not Connected."
}
return (temp);

}
public string RETR (int msg)
{
String temp= "";
if (state!= connect_state. TRANSACTION)
{
The pop command RETR is only valid into the TRANSACTION state
temp= "Connection State not = TRANSACTION";
}
Else
{
Retrieve mail with number mail parameter
Issue_command ("RETR" + MSG.) ToString ());
Temp=read_multi_line_response ();
}
return (temp);

}

public string RSET ()
{
string temp;
if (state!= connect_state. TRANSACTION)
{
The pop command STAT is only valid into the TRANSACTION state
temp= "Connection State not = TRANSACTION";
}
Else
{
Issue_command ("RSET");
Temp=read_single_line_response ();
}
return (temp);

}

public string STAT ()
{
string temp;
if (state==connect_state. TRANSACTION)
{
Issue_command ("STAT");
Temp=read_single_line_response ();

return (temp);
}
Else

{
The pop command STAT is only valid into the TRANSACTION state
Return ("Connection state is not = TRANSACTION");
}
}

public string USER ()
{
string temp;
if (state!= connect_state. AUTHORIZATION)
{
The pop command USER is only valid in the AUTHORIZATION state
temp= "Connection State not = AUTHORIZATION";
}
Else
{
if (user!=null)
{
Issue_command ("user" + user);
Temp=read_single_line_response ();
}
Else
{//no user has been specified
temp= "No User specified."
}
}
return (temp);
}

public string USER (string user_name)
{
User=user_name; Put the user name in the appropriate propertity
Return (USER ()); Call USER with no arguements
}
#endregion
}

}


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.