How to receive letters on the client in C #

Source: Internet
Author: User
Client-side Creation interface



Type
Object Name
Text Property

Label
Label1


Label
Label2
User name:

Label
Label3
Password

Textbox
TextBox1


Textbox
TextBox2


Button
Button1
Receive

Button
Button2
Exit




A successful message is displayed when the validation is sent through the Label1 Text property

A failed message is displayed when the sent validation does not pass the Label1 Text property



Display code



1. Add code to the front of the code

Using System.Threading;

Using System.Net;

Using System.Net.Sockets;

Using System.IO;

2. Add field code

Private TcpClient mailclient;

Private NetworkStream NS;

Private StreamReader SR;

Private StreamWriter SW;

3. Double-click the Receive button

private void Button1_Click (object sender, System.EventArgs e)

{

Login ();

}

private int Login ()

{

String M_response; Information variables returned from the server

int totmessages; Indicate how many letters are in your mailbox.

Try

{

Connect the server, where the first parameter represents the server address, and the second parameter represents the number of ports POP3

Mailclient=new tcpclient ("pop.163.com", 110);

}

catch (Exception e)

{

label1.text= "error";//server Connection failed

return 2;

}

Ns=mailclient. GetStream ()//return server byte stream

Sr=new StreamReader (NS);

Sw=new StreamWriter (NS);

M_RESPONSE=SR. ReadLine ()//read Server byte throttle character

Send user name to server

Sw. WriteLine ("User" +textbox1.text);

Sw. Flush ();

Verify that the user name is correct

M_RESPONSE=SR. ReadLine ();

if (m_response. Substring (0,3) = = "-er")

{

label1.text= "User error";//Connection Failed

return 2;

}

Send password to server

Sw. WriteLine ("pass" +textbox2.text);

Sw. Flush ();

Try

{

Verify that the password is correct

M_RESPONSE=SR. ReadLine ();

}

catch (Exception e)

{

label1.text= "Password error";//Password connection failed

return 2;

}

if (m_response. Substring (0,4) = = "-err")

{

label1.text= "Password error";//Password error

return 2;

}

Sw. WriteLine ("Stat");//Perform POP3 permission check command

Sw. Flush ();

M_RESPONSE=SR. ReadLine ();

String[] Nummess=m_response. Split (")";

Totmessages=convert.toint16 (nummess[1]);//Get a few letters from the server

if (totmessages>0)

{

Label1.text= "You have" +totmessages+ "messages";

}

Else

{

Label1.text= "You have no messages";

}

return 1;

}

4. Double-click the Exit button

private void Button2_Click (object sender, System.EventArgs e)

{

if (ns!=null)

{

Sw. Close ();

Sr. Close ();

Ns. Close ();

Mailclient. Close ();

}

}

Note the point:

Stat on behalf of the Permission Check command, if you want to get the title and sender information to use the top to execute the command, to get the text information to use the RETR to execute the command, these contents need me to sum up, I will be in detail next week.



In Thursday I wrote an article about "How to send a letter in C #". So today I wrote another article "How to receive letters in C #", and I will summarize the basic content of email and email in C # next week. Please, thank you.



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.