<CRLF>.<CRLF>
." Commands are line-oriented and can return a single or multi-line response.
For detailed information about POP3, see RFC 1939: "Post Office Protocol - Version 3."
[Top]
Figure 5.1 POP3 Session States
Step |
Section with details
|
|
|
|
|
|
|
|
| |
---|
Response Type |
Response Code and Description
|
|
|
Final line: . (dot) and Note: If an error occurs on a multi-line response, a single line is returned. |
---|
pop3Sink_t
structure contains callbacks for each client call. The client's processResponses
function invokes the interface function that corresponds to the client call. Functions with multi-line responses map to more than one callback. The first type of callback indicates the start of a notification. The second type passes back multi-line data. The third type of callback provides a notification that the operation is complete.
If a server error occurs, the error callback is invoked.
Table 5.3 shows which POP3 functions are mapped to callbacks in the
pop3Sink_t
structure. Table 5.4 shows functions that do not map to callbacks.
Table 5.3 Functions with Callbacks
pop3Sink_t
structure. The response sink is made up of function pointers and opaque data. Initializing the sink sets its function pointers and opaque data to null
. For general information about the response sink, see SDK Response Sinks for C.
To initialize and allocate the response sink, call the pop3Sink_initialize
function and supply the sink you want the POP3 client to use. If successful, this function returns
NSMAIL_OK
. Use this syntax:
int pop3Sink_initialize( pop3Sink_t ** out_ppPOP3Sink )The following section of code initializes the response sink and sets the sink function pointer.
int l_retCode = 0;
pop3Sink_t * l_pPOP3Sink = NULL;
l_retCode = pop3Sink_initialize(&l_pPOPSink);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
setSinkFunctions(l_pPOP3Sink);When a session is finished, you must free any memory associated with the response sink. To free the response sink and its data members, use this function:
void pop3Sink_free( pop3Client_t ** in_ppPOP3Sink );When this function returns, the response sink is set to
null
. The user must free any opaque data.
After you create the response sink, the next step is Creating a Client.
[Top] [Creating a Response Sink]
pop3Client_t
structure to communicate with a POP3 server. To initialize and allocate the pop3Client_t
structure and set the response sink for the client's use, call the
pop3_initialize
function.
When you create the client structure, you supply the identifier of the POP3 client you are creating, along with an initialized response sink, as described in Creating a Response Sink. Use this syntax:
int pop3_initialize( pop3Client_t ** out_ppPOP3,The following section of code creates a client.
pop3SinkPtr_t in_pPOP3Sink );
/* Initialize sink first as described in Creating a Response Sink */
pop3Client_t * l_pPOP3Client = NULL;
l_retCode = pop3_initialize(&l_pPOP3Client, l_pPOP3Sink);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }When a session is finished, you must free any memory associated with the client. To free the client structure and its data members, use this function:
void pop3_free( pop3Client_t ** in_pPOP3 );The
in_pPOP3
parameter represents the POP3 client. When this function returns, the client structure is set to null
.
After you initialize the client, the next step is Connecting to a Server.
[Top]pop3_connect
function.
Supply the identifier of the POP3 client that wants to connect with the server. If the server is using the default port for the POP3 protocol (port 110), you can enter 0 as the value of the port parameter. The SDK function performs some error-checking. Use this syntax:
int pop3_connect( pop3Client_t * in_pPOP3,
const char * in_server,
unsigned short in_port );
NOTE: For this function's callback mapping, see POP3 Function Callback Mapping. §The following section of code connects the client to the server.
/* After Creating a Response Sink and Creating a Client */
int l_retCode = 0;
char* l_szServer = "pop3Server.com";
pop3Client_t * l_pPOP3Client = NULL;
l_retCode = pop3_connect(l_pPOP3Client, l_szServer, 110);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }After connecting, the next step is Logging In. When a session is finished, you must free any memory associated with the client. To disconnect the client from the server and close the socket connection, use this function:
int pop3_disconnect( pop3Client_t * in_pPOP3 );The
in_pPOP3
parameter represents the POP3 client. This function could be useful as part of a Cancel operation while retrieving a message. Remember that you do not call pop3_processResponses
after pop3_disconnect
.
NOTE: For the callback mapping for these functions, see POP3 Function Callback Mapping. §[Top]
pop3_user
and pass the POP3 client and the user name. Use this function:
int pop3_user( pop3Client_t * in_pPOP3, const char * in_user );This function sends the USER POP3 protocol command. To submit the user password, call the
pop3_pass
function:
int pop3_pass( pop3Client_t * in_pPOP3,This function sends the PASS POP3 protocol command. While these commands execute, the session is in Authorization state. Successful completion moves the session to the Transaction state. For a list of states and commands that can be executed in each state, see POP3 Session States. A successful login moves the POP3 session from the Authorization state to the Transaction state, where the user can perform a number of operations.
const char * in_password );
NOTE: For the callback mapping for these functions, see POP3 Function Callback Mapping. §The following section of code logs the user in with user name and password.
/* User id sequence */
int l_retCode = 0;
char* l_szUser = "test1";
char* l_szPassword = "test1Password";
pop3Client_t * l_pPOP3Client = NULL;
l_retCode = pop3_user(l_pPOP3Client, l_szUser);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
/* Password sequence */
l_retCode = pop3_pass(l_pPOP3Client, l_szPassword);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }After you log in, you can perform any Transaction state operations. The next step is Retrieving a Message. [Top]
pop3_stat
function gets the mailbox status for a given client by issuing the STAT protocol command. Use this function:
int pop3_stat( pop3Client_t * in_pPOP3 );The status returned includes the number of messages present and the octet size of the mail drop.
NOTE: For this function's callback mapping, see POP3 Function Callback Mapping. §The following section of code counts the messages in the client's mailbox.
int l_retCode = 0;
pop3Client_t * l_pPOP3Client = NULL;
l_retCode = pop3_stat(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }[Top]
pop3_list
function:
int pop3_list( pop3Client_t * in_pPOP3 );This function takes only the POP3 client as a parameter. It sends the LIST POP3 protocol command. To list only the message specified by the message number, use the
pop3_listA
function:
int pop3_listA( pop3Client_t * in_pPOP3, int in_messageNumber );Supply the POP3 client and a message number as parameters. This function sends the LIST [arg] POP3 protocol command.
NOTE: For the callback mapping for these functions, see POP3 Function Callback Mapping. §The following section of code retrieves the email address of the sender along with authenticated messages.
int l_retCode = 0;
pop3Client_t * l_pPOP3Client = NULL;
l_retCode = pop3_list(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }[Top]
pop3_top
function, which issues the TOP protocol command:
int pop3_top(pop3Client_t * in_pPOP3,To retrieve all headers for a given mailbox, combine
int in_messageNumber, int in_lines );
pop3_top
with a call to
pop3_stat
. First, call pop3_stat
to find the number of messages in the mailbox. When you get the number, for example, 10, call pop3_top
once, passing each message number in turn, until you get to the total (in this case, 10). For the in_lines
parameter, use a value of 0 so that no body lines are returned.
NOTE: For this function's callback mapping, see POP3 Function Callback Mapping. §The following section of code lists the header of the message specified by its message number. It retrieves no body lines.
int l_retCode = 0;
int l_messageNumber = 1;
pop3Client_t * l_pPOP3Client = NULL;
/* Get only the message headers: request 0 body lines */
l_retCode = pop3_top(l_pPOP3Client, l_messageNumber, 0);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }After you retrieve message headers, you can go on to Retrieving a Message if you like. [Top]
pop3_retrieve
function takes the identifier of the POP3 client that is retrieving the mail and and the message number, and retrieves the contents of the message:
int pop3_retrieve( pop3Client_t * in_pPOP3, int in_messageNumber );The message is returned in the form of data chunks. The
pop3_retrieve
function issues a RETR command. It fails if the message with the specified number does not exist.
NOTE: For this function's callback mapping, see POP3 Function Callback Mapping. §The following section of code retrieves the contents of a message.
int l_retCode = 0;
int l_messageNumber = 1;
pop3Client_t * l_pPOP3Client = NULL;
l_retCode = pop3_retrieve(l_pPOP3Client, l_messageNumber);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }[Top]
pop3_quit
to notify the server that it is closing the session:
int pop3_quit( pop3Client_t * in_pPOP3 );This function sends the QUIT POP3 protocol command. The server closes the TCP connection and sends back a response. It is preferable to end a session with
pop3_quit
instead of just closing.
If the session is in the Authentication state when this function is called, the server simply closes the connection. If the session is in the Transaction state, the server goes into the Update state and expunges any messages marked for deletion, and then quits.
NOTE: For this function's callback mapping, see POP3 Function Callback Mapping. §The following section of code notifies the server that the client is terminating the session.
int l_retCode = 0;
pop3Client_t * l_pPOP3Client = NULL;
l_retCode = pop3_quit(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }
l_retCode = pop3_processResponses(l_pPOP3Client);
if (l_retCode != NSMAIL_OK) { /*Deal with error*/ }[Top]
Last Updated: June 3, 1998