This article will show you how to write your own mailbox monitoring program, the program will call the Winsock function directly for network communication. In addition to having Winsock programming knowledge, you must understand the POP3 protocol. The following is a rough introduction to POP3, and readers can refer to RFC1225 for a more detailed understanding of the protocol.
I. about the POP3 agreement
POP3 server programs typically provide services on TCP port 110. When a customer wants to use a service, it establishes a TCP connection with the server. Once the connection is established, the POP3 server sends a welcome message to the customer. The client then starts sending commands to the server, and the server gives the answer accordingly. POP3 commands consist of a keyword or keyword plus parameters. Each command takes a carriage return newline (0xd0xa) as the closing flag. For all commands, the POP3 server will provide an answer. The answer to the server consists of a status flag plus some additional information. The two flags currently in use are "+ok" and "-err", respectively, indicating whether the customer's order is legal. All answers are also wrapped in a return line.
The four POP3 commands associated with the topic discussed in this article are user, pass, list, and quit.
User command
Format username
Where name is the user identity of the user on the POP3 server. The customer should be able to send this command after receiving a welcome message from the server or after the previous user or pass failed.
Pass Command
Format passstring
Where string is the user's password. The customer can send this command after sending the user command and receiving a +ok answer. If the username and password are correct, the server answers +ok or-err.
List command
Format list
If the user has a message, the list command answers +ok, lists the identifiers and sizes of all messages (one line per message), and the last line containing only a period (0xd0xa0x2e) indicates the end of the entire answer. If the user does not have mail, some servers return-err, some of which may return a +ok and a line containing only one period. Of course, the client must be able to send a list command to the server after the pass command has passed.
Quit command
Exit the login from the POP3 server.
II. implementation of related functions
Next we implement a function called Pop3checkmail by following the communication rules defined in the POP3 protocol, so that we can detect the mailboxes as long as we call this function.
The following code is implemented with the DELPHI4-compliant Pascal language, we must include the Winsock unit, and initialize the Winsock dynamic Connection library before calling the following functions. The code for initializing the Winsock dynamic Connection library is as follows:
Ifwsastartup ($002,wsadata) <>0thenHalt;
Pop3checkmail's prototype is as follows:
Functionpop3checkmail (email,password:string;varmaillist:tstringlist;varerrormsg:string): Bool;
Parameter description:
Email and password are the user's email name and password respectively.
Variable parameter maillist is used to return the identity and size of the message, Maillist.count represents the number of messages.
Variable parameter errormsg returns an error message.