How to receive letters in the client in C #
Create an interface
Type object name text attribute label label1
Label Label2 User Name: Label Label3 Password TextBox TextBox1
Textbox textbox2
Button Button1 Receive Button Button2 Exit
When the sending verification is successful, the text attribute of Label1 will show a successful message.
When the sending verification does not pass the TEXT attribute through Label1, the failed message will be displayed.
Display code
1. Add code at the front end of the code
Using system.threading;
Using system.net;
Using system.net.sockets;
Using system.io;
2. Add field code
PRIVATE TCPCLIENT MAILCLIENT;
Private netstream 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; // The information variable returned from the server
INT TOTMESSAGES; / / indicates a few in your mailbox
Try
{
/ / Connect the server, where the first parameter represents the server address, the second parameter represents the port number of POP3
MailClient = New TcpClient ("POP.163.com", 110);
}
Catch (Exception E)
{
Label1.text = "error"; // server connection failed
Return 2;
}
ns = mailclient.getStream (); // Return to server word current
SR = New StreamReader (NS);
SW = New streamwriter (NS);
m_response = sr.readline (); // Read server byte stream character
// Send a username to the server
SW.WRITELINE ("User" TextBox1.text);
Sw.flush ();
// Verify that the username is correct
m_response = sr.readline ();
IF (m_response.substring (0, 3) == "- ER")
{
Label1.text = "user error"; // user connection failed
Return 2;
}
/ / Send a password to the 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"); // Execute POP3 permission check command
Sw.flush ();
m_response = sr.readline ();
String [] Nummess = m_Response.split (''); TotMessages = Convert.Toint16 (Nummess [1]); // Get a few letters in the server
IF (Totmessages> 0)
{
Label1.text = "You Have" Totmessages "Messages";
}
Else
{
Label1.text = "You Have no message";
}
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 ();
}
}
be careful:
STAT represents the permission check command, if you want to get the title and sender information, use the TOP execution command, you have to use the RETR to execute the command, these contents need me to sum up, I will introduce the next week.
On Thursday I wrote a "How to send a letter in C #" written, so I also wrote "How to receive letters in C #", I will summarize the entire C # in the next week. Send Email and receive the basic content of Email, please visit you. Thank you.