Content on this page
Introduction
Communication with POP3 servers
Download email
Download an email to a specified folder
Download mail header information
Delete email
Introduction
POP stands for Post Office Protocol, which is the Post Office Protocol used for email reception. It uses TCP port 110 and is now commonly used in the third edition. Therefore, it is referred to as POP3. POP3 still adopts the Client/Server working mode ,. When the client needs services, the client's software (OutlookExpress or FoxMail) will establish a TCP connection with the POP3 server. After that, it will go through three working states of the POP3 protocol. The first is the authentication process, confirm the user name and password provided by the client. After the authentication is passed, the user enters the processing status. In this status, the user can receive his/her own email or delete the email, after the response is completed, the client issues the quit command, and then enters the update status. The deleted emails marked with the deletion are deleted from the server. So far, the whole POP process has been completed.
Under the namespace Eastasp. Framework. Net. Mail. Pop3, Snake. Net provides a series of classes and functions to provide developers with comprehensive and complete Mail receiving functions using the POP3 protocol. This section describes how Pop3Client and Pop3Express can send emails.
Communication with POP3 servers
Let's take a look at a piece of code, and then briefly learn how to get information about POP3 servers.
// Declare
Pop3Client client;
StatInfo statInfo;
ListInfo [] infoList;
UidInfo [] uidInfoList;
Client = new Pop3Client (new Pop3Server ("pop.mailserver.com", "user", "pass "));
Try {
Client. Open ();
// Send noop comand
Client. Noop ();
// Get box stat
StatInfo = client. Stat ();
// List mail
InfoList = client. List ();
UidInfoList = client. UidList ();
}
Finally {
If (client! = Null ){
Client. Close ();
Client = null;
}
}
From the code above, we can see that Pop3Client first connects to the server, and then calls three methods Stat, List, And UidList in sequence to obtain server information. Next we will briefly describe the functions of these three methods.
The Stat method is used to obtain information about all emails on the server. It returns a StatInfo structure to describe the total number and occupied space of all emails on the server.
The List method is used to obtain the serial number (integer) and occupied space of a single message on the server. It returns an array of ListInfo struct.
The UidList method is similar to the List method. However, the unique identifier and occupied space of a single message are returned, and an array of UidInfo struct is returned.
Download email
In the previous section, we learned how to obtain the abstract information of the Pop3 mailbox. Now we have to proceed to the topic. For how to receive emails, see the following DEMO code.
// Declare
Pop3Client client;
String uid;
ListInfo [] infoList;
MailMessage. Message [] messages;
Client = new Pop3Client (new Pop3Server ("pop.mailserver.com", "user", "pass "));
Try {
Client. Open ();
Client. Noop ();
InfoList = client. List ();
If (infoList. Length> 0 ){
Messages = new MailMessage. Message [infoList. Length];
For (int I = 0; I <infoList. Length; I ++ ){
Uid = client. GetUid (infoList [I]. Order );
Messages [I] = client. GetMessage (infoList [I]. Order );
}
}
}
Finally {
If (client! = Null ){
Client. Close ();
Client = null;
}
}
The above Code shows that the process of receiving mail is to first ask the server to obtain a list of mail numbers, and then obtain the mail content according to the number specified by the mail. The code above demonstrates the basic process of POP3 service communication. In actual application, we don't need to be so troublesome every time. You can use the Pop3Express object to implement a simple email receiving method. See the following code:
// Declare
MailMessage. Message [] messages;
Messages = Pop3Express. GetAllMessages (
New Pop3Server ("pop.mailserver.com", "user", "pass "),
Pop3ReceiveMode. RecevieWithoutDelete );
Pop3Express can be implemented simply by calling the GetAllMessages method. The functions implemented by the previous Code are described. Here, pay attention to the second parameter recevieMode of the GetAllMessages method. It has three values: ReceiveAllThenDelete, ReceiveOneDeleteOne, and RecevieWithoutDelete. As the name implies, ReceiveAllThenDelete indicates that all emails are deleted after the download. ReceiveOneDeleteOne indicates that an email is deleted after the download, and RecevieWithoutDelete indicates that only the downloaded email is not deleted.
Download an email to a specified folder
The previous section describes how to download an email. We can find that the GetMessage method of the Pop3Client returns a MailMessage. message object real column. Sometimes we do not need to return such an object. We only need to save the content of the email as a separate file for other programs to process it. Therefore, the Pop3Client also provides another method, SaveMessage, to save the mail content as a file. Please refer to the following code.
// Declare
String savePath;
Pop3Client client;
String uid;
ListInfo [] infoList;
SavePath = @ "c: \ pop3mails ";
Client = new Pop3Server ("pop.mailserver.com", "user", "pass ");
Try {
If (! Directory. Exists (savePath) Directory. CreateDirectory (savePath );
Client. Open ();
Client. Noop ();
InfoList = client. List ();
If (infoList. Length> 0 ){
For (int I = 0; I <infoList. Length; I ++ ){
Uid = client. GetUid (infoList [I]. Order). Replace (":","");
Client. SaveMessage (string. Format ("{0 }\\ {1}. eml", savePath, infoList [I]. Order), infoList [I]. Order );
}
}
}
Finally {
If (client! = Null ){
Client. Close ();
Client = null;
}
}
Download mail header information
Sometimes, we do not want to download the entire email, but we hope to know the general content of the email. In this case, we can first obtain the mail header information, and then decide whether to download the entire mail or delete the mail based on the title, sender, and other content. Pop3Client provides the GetMessageHeader method for obtaining mail header information. See the following code:
// Declare
Pop3Client client;
ListInfo [] infoList;
Pop3MessageHeader [] headers;
Try {
Client = new Pop3Server ("pop.mailserver.com", "user", "pass ");
Client. Open (); client. Noop ();
InfoList = client. List ();
Headers = new Pop3MessageHeader [infoList. Length];
For (int I = 0; I Headers [I] = client. GetMessageHeader (infoList [I]. Order );
}
}
Finally {
If (client! = Null ){
Client. Close ();
Client = null;
}
}
Delete email
After using POP3 to download an email, the email copy is still saved on the server and needs to be deleted. Pop3Client provides the Delete method to Delete emails on the server. See the following code.
// Declare
Pop3Client client;
ListInfo [] infoList;
Pop3MessageHeader [] headers;
Try {
Client = new Pop3Server ("pop.mailserver.com", "user", "pass ");
Client. Open (); client. Noop ();
InfoList = client. List ();
Headers = new Pop3MessageHeader [infoList. Length];
For (int I = 0; I Headers [I] = client. Delete (infoList [I]. Order );
}
}
Finally {
If (client! = Null ){
Client. Close ();
Client = null;
}
}
You can also use Pop3Express to simplify the operation.
// Declare
MailMessage. Message [] messages;
Messages = Pop3Express. DeleteAllMessages (
New Pop3Server ("pop.mailserver.com", "user", "pass "));