Technical documentation-use C # for sending and receiving e-mail
Source: Internet
Author: User
Technical documentation-use C # for sending and receiving e-mail
1. Target description
Sending requirements:
Input:
1. Recipient's email address
2. SMTP server address
3. Username
4. Password
5. Email body
6. Title (optional)
Send the body of the email.
Receiving requirements:
Input:
1. POP3 server address
2. Username
3. Password
Retrieve the email list and receive the email body.
Ii. Problem Description
1. How to Use C # To send emails.
2. How to Use C # To receive mails Based on POP3 protocol
Iii. Solutions
1. Sending emails
Use the mailmessage class and smtpclient class in the system. net. Mail namespace to send emails. This method is simple and will not be described.
2. receiving emails
I mainly use two classes: tcpclient class and networkstream class. Tcpclient class is a very important class that uses C # for network programming. It provides a simple method to connect, send, and receive data through the network, thus greatly simplifying network programming. The networkstream class provides a standard. NET Framework Stream Mechanism for sending and receiving data through network sockets. It supports synchronous and asynchronous access to network data streams and is an important part of network communication.
Iv. Summary
Through this exercise, the workflow for receiving mail based on POP3 protocol is as follows:
After the client successfully connects to the server, the server will return the following information: + OK ...... Character + OK is the return information of POP3 protocol. Unlike the SMTP protocol, its response information is represented by a variety of numbers, with only two: + OK or-err. + OK indicates that the connection is successful, and-err indicates that the connection fails.
Then, enter user <User Name> on the client to tell the server your user name. (Note: Some servers are case-sensitive ). When the server returns + OK, enter pass <password> on the client and the server returns + OK, some mailbox statistics will be returned, for example: + OK n message (s) [*** byte (s)] Different server returns different information formats, so we can use the STAT command to view the mailbox information. The STAT command responds with two numbers, indicating the number of mails and the size of mails, respectively. If there is a letter in the mailbox, you can use the RETR command to obtain the body of the email. The RETR command is in the format of RETR <email no.> If the first line of the returned result is + OK, the RETR command is successful. The body of the email starts from the second line. The last line is the same as the SMTP protocol. It is a separate English ending line, indicating the end Of the mail. It is read using reader. Readline (), where Reader is a member of stringreader. If an email is received through POP3, use the DELE command to delete the email in the mailbox. Otherwise, your mailbox will pop up. The format of the DELE Command is: DELE <email no.> if an error is deleted, you can use the rset command to restore all deleted emails (if not, the system is not exited ). After all the operations are completed, enter the quit command to exit the POP3 server.
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.