Sample Application

Initialization

µC/POP3c requires no initialization. 

Note that µC/TCP-IP must be initialized without error. Also, µC/DNSc must be initialize to resolve POP3 Server host name. 

Connect to a POP3c Server and retrieve e-mails

Listing - Example Code in Standard Mode
#define  MSG_BUF_LEN 		4096


void  POP3c_Test (void)
{
    NET_IPv4_ADDR  ip_server;
    NET_ERR      errMsg;
    NET_SOCK_ID  sock;
    CPU_INT32U   msg_qty;
    CPU_INT32U   mbox_size;
    CPU_INT16U   i;
    CPU_INT32U   msg_size;
    CPU_CHAR     msg_buf [MSG_BUF_LEN];
    POP3c_MSG    msg;

    ip_server = NetASCII_Str_to_IPv4("192.168.0.2", &errMsg);								(1)
    if (errMsg != NET_ASCII_ERR_NONE ) {
        printf("Error - NetASCII_Str_to_IP: %d\n", errMsg);
        return;
    }
    sock = POP3c_Connect(ip_server, POP3c_CFG_IPPORT, DEF_DISABLED, &errMsg);				(2)
    if (errMsg != POP3c_ERR_NONE ) {
        printf("Error - POP3c_Connect: %d\n", errMsg);
        return;
    }
    POP3c_Authenticate(sock, "foo@xyz.com", "password", &errMsg);							(3)
    if (errMsg != POP3c_ERR_NONE) {
        printf("Error - POP3c_Authenticate: %d\n", &errMsg);
        return;
    }
    POP3c_MboxStat(sock, &msg_qty, &mbox_size, &errMsg);									(4)
    if (errMsg != POP3c_ERR_NONE) {
        return;
    }
    printf("POP3c_MboxStat: %d msg, %d bytes\n", msg_qty, mbox_size);

   for (i = 1; i <= msg_qty; i++) {
        POP3c_MsgStat(sock, i, &msg_size, &errMsg);											(5)
        if (errMsg == POP3c_ERR_NONE) {
            printf("Message %d: %d bytes\n", i, msg_size);
            POP3c_MsgRetrieve(sock,															(6)
                              i,
                              msg_buf,
                              MSG_BUF_LEN,
                              DEF_NO,
                              &errMsg);
            printf("Original message (buffer):\n");
            printf("%s\n", msg_buf);
            POP3c_MsgRead(msg_buf,															(7)
                           MSG_BUF_LEN,
                          &msg,
                          &errMsg);
            printf("Content of message %d\n\n", i);
            printf("From     : %s\n",   msg.From);
            printf("Sender   : %s\n",   msg.Sender);
            printf("Date     : %s\n",   msg.Date);
            printf("Reply to : %s\n",   msg.Reply_to);
            printf("Subject  : %s\n\n", msg.Subject);
            printf("%s\n\n", msg.Body);
        }
    }
    if (msg_qty >= 1) {
        POP3c_MsgDel(sock, 1, &errMsg);														(8)
    }
    POP3c_Disconnect(sock, &errMsg);														(9)
    printf("Disconnected\n");
    return;
}
  1. Convert the ASCII dotted-decimal notation to a network protocol IPv4 address.
  2. Establish a standard TCP connection to the POP3 server (192.168.0.2) using the default port (POP3c_CFG_IPPORT). In this example, the secure mode is disabled. See POP3c_Connect() and SSL/TLS Documentation for more information on how to connect to a POP3 server in secure mode.
  3. Authenticate on the POP3 server. This function relies on the USER and PASS commands. As demonstrated in the above listing, the password is sent in clear text.
  4. Get information on logged in maildrop (message quantity, except for deleted ones, and total maildrop size).
  5. Get information on particular message (total message size, in octets).
  6. Retrieve “raw” message from the POP3 server. Header and body are put into msg_buf (MSG_BUF_SIZE has to be set to a large enough value). Use the function POP3c_MsgStat() to determine a message’s size. Note that providing a buffer that is too small for the whole message won’t lead to runtime errors; however, it might prevent the application from running in a predictable way if the complete message was necessary for execution of the program.
  7. Read (i.e. parse) message and fill in POP3c_MSG structure. This function takes in parameter the msg_buf previously filled by POP3c_MsgRetrieve() and extract a few pieces of information from its header. However, the addresses fields are not parsed . Also, the message field of this structure is in fact a pointer to the first bytes of the message body. Finally, note that message_buffer is not modified by this function.
  8. Delete a message from the maildrop.
  9. Disconnect from the server. The update mechanism takes place here (permanently removing messages set for deletion).