asp.net to realize pop function

Source: Internet
Author: User
Tags getstream readline socket split
asp.net/*
Tofu production is a boutique
Http://www.asp888.net Tofu Technology Station
If reproduced please retain the full copyright information
*/
I have said before how to use socket programming under the ASP.net to implement domain name query, details see article: http://www.asp888.net/lanmu/viewarticle.asp?id=51[title]asp+ Complete code to implement domain name query in [/title]
Tofu One day suddenly think, since you can use the socket to realize the domain name query, it is not as long as the use of the socket client
Can the previous Windows desktop program of programming be implemented with ASP.net? Just do it, and the first thing that I'm going to do is
A asp.net version of the mail receiver, we know that in the asp+ to send email is very convenient to see things (http://www.asp888.net/lanmu/viewarticle.asp?id=95[title]asp+ Send email full manual [/title]), but how to proceed
What about the procedure for collecting pop letters? It seems only to take out the Tofu Killer sword:
First, let's take a look at the code for this program: pop.aspx
<%@ Assembly name= "System.Net"%>
<%@ Import namespace= "System.Net"%>
<% @Import namespace= "System.Net.Sockets"%>
<%@ Import namespace= "System.IO"%>
<script language= "C #" runat=server>
protected void Page_Load (Object Src, EventArgs E) {
String user= "Doufu"; Mailbox user code
String pass= "Asp888.net"; Mailbox User Password
String popserver= "localhost"; POP Server IP Address
TcpClient TCPC = new TcpClient ();
if (0 = = Tcpc. Connect (Popserver, 110))
{
If the program executes here, it means the connection pop Server succeeded
Stream s;
StreamReader SR;
String Strcmd;
Byte[] Arrcmd;
String strret;
String[] Arrret;



sr = new StreamReader (TCPC. GetStream (), encoding.default);
Response.Write (Sr.  ReadLine () + "<br>"); Mail server's Welcome language

Strret=logon (Tcpc,user,pass);

if (judgestring (strret)!= "+ok") {
Response.Write ("Sorry, no this user/password mismatch");
Return
}

The user and password match, the following start statistics user mailbox information
All of the return information is included in this array.
Arrret=staticmailbox (TCPC);
if (arrret[0]!= "+ok") {
Response.Write ("a mistake!") ");
Return
}
Response.Write ("The current user <font color=blue>" + user + "</font>" box has <font color=red> "+ arrret[1] +" </ Font> Seal, a total of <font color=red> "+ arrret[2" + "</font>byte");

Receiving letters
Arrret=popmail (tcpc,4);
Judge return
if (arrret[0]!= "+ok") {
Response.Write ("Made a mistake");
}
Response.Write ("Mail Date:" + arrret[1] + "\n<br>");
Response.Write ("Sender:" + arrret[2] + "\n<br>");
Response.Write ("the addressee:" + arrret[3] + "\n<br>");
Response.Write ("Mail Subject:" + arrret[4] + "\n<br>");
Response.Write ("Mail content:" + arrret[5] + "\n<br>");
Close socket Join
Tcpc. Close ();
}
Else
{
Response.Write ("Could not connect to server!");
}
}
String sendpopcmd (tcpclient tcpc,string strcmd) {
Byte[] Arrcmd;
String strret;
StreamReader SR;
Stream s;
S=tcpc. GetStream ();
Strcmd = Strcmd + "\ r \ n";
Arrcmd= Encoding.Default.GetBytes (Strcmd.tochararray ());
S=tcpc. GetStream ();
S.write (arrcmd, 0, strcmd.length);
sr = new StreamReader (TCPC. GetStream (), encoding.default);
STRRET=SR. ReadLine ();
return strret;
}
String Logon (tcpclient tcpc,string user,string pass) {
The function of this function is to verify the identity of the user who gets the connection
Send user code
String strret;
Strret=sendpopcmd (TCPC, "user" + user);
Strret=sendpopcmd (TCPC, "pass" + Pass);
return strret;
}
String[] Staticmailbox (tcpclient tcpc) {
String strret;
Strret=sendpopcmd (TCPC, "stat");
if (judgestring (strret)!= "+ok") {
Return "-err-err". Split ("". ToCharArray ());
}
String[] Arrret=strret.split ("". ToCharArray ());
return arrret;
}
String judgestring (String strcheck) {
if (strcheck.substring (0,3)!= "+ok") {
return "-err";
}
Else
return "+ok";
}
String[] Popmail (tcpclient tcpc,int i) {
String strret;
BOOL Strbody=false;
String[] Arrret =new string[10];
String[] arrtemp;
Strret=sendpopcmd (TCPC, "RETR" + i.tostring ());
if (judgestring (strret)!= "+ok") {
It means there's no such letter or any other mistake.
Return "-err-err". Split ("". ToCharArray ());
}
StreamReader SR;
sr = new StreamReader (TCPC. GetStream (), encoding.default);
Response.Write ("\n<br>");
Response.Write ("<font color=red>\n<br>");
while (Sr. Peek ()!=46)
{
Oh, do not know why not if 46 can!
STRRET=SR. ReadLine ();
Arrtemp=strret.split (":"). ToCharArray ());
if (strret== "")
Strbody=true; Now start receiving body information.
if (arrtemp[0]== "Date") {
ARRRET[1]=ARRTEMP[1]; Date the letter was sent
}
if (arrtemp[0]== "from")
ARRRET[2]=ARRTEMP[1]; Sender
if (arrtemp[0]== "to")
ARRRET[3]=ARRTEMP[1]; The addressee.
if (arrtemp[0]== "Subject")
ARRRET[4]=ARRTEMP[1]; Theme
if (strbody) {
ARRRET[5]=ARRRET[5] + strret + "\ n";
}
}

Arrret[0]= "+ok";
return arrret;
}
</script>
All right, everybody. Through this program, we can first understand the pop (Post ofice protocal) protocol, followed by the asp.net socket program to deepen the
Understanding, you can also use this program to receive an ISP's email inbox without a web-based program to manage your ISP's mailbox. This program is mainly for ASP.net
Operation POP server for programming, plus time haste, so many places are directly used Response.Write ("") such statements, I mean this article
Just simple knowledge to explain, we finally concrete application, it must be another optimization of this procedure! There are people throwing bricks: this tofu how lazy:

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.