Click here to Skip to main content
12,818,816 members (32,951 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


28 bookmarked
Posted 18 Apr 2011

Email checking program with support for multiple POP accounts

, 18 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A program to check for email messages with multiple POP accounts.


This application initially was based on an Open Source sample NetManager V1.1 by Petr Stejskal. I used it to explore both MFC and Internet/Networking (email in particular). Email clients nowadays are mostly replaced by Web based email services, but I think this code sample can be useful for programmers studying Internet protocols. This is a relatively simple MFC program to check for email messages on multiple POP accounts. The application was upgraded from Visual C++ 6.0 to VC++ 2008.


To send email, the SMTP (Simple Mail Transfer Protocol) protocol is used. An email message consists of two parts: header and body (+ optional attachment). For ASCII based alphabets, the body (text) is sent non encoded (7-bit ASCII). For non-ASCII data (languages outside the Latin alphabet), encoding is used: the MIME specification lists two binary-to-text encoding schemes quoted-printable for text and base64 for binary attachments. Two protocols are mostly used to retrieve email from a remote server over a TCP/IP connection:

  • Internet message access protocol (IMAP);
  • Post Office Protocol (POP).

Email clients and mail servers usually support both protocols.

Using the code

By writing this code, I got some practice in MFC, but mostly explored the coding of Internet protocols. Thus, the code is a mix of MFC and C++ classes with some C and Win32 API functions. Every time, I simply used what I thought was best applicable for a task to be solved. For implementation of the POP3 protocol, I adapted the CPop class by Robert E. Peters that is an update from the POP3 protocol wrapper class written by Asif Rasheed. The code is well commented and should be clear to people with an MFC and Win32 background.

May be of some interest is a function to receive email. On a slow internet connection (such as dial-up), the transmission can get stuck with the program hanging. So, I added a bit of multithreading to the implementation to be able to interrupt the transmission (to close the socket) from the worker thread in such situations. Every time the transmission begins, you can stop it from the message box window shown. Here is the corresponding code (with some omissions):

//Function to receive mail and write it in the file:
void CMailPop3::OnReceive() 
    //1. To have the new socket, create the instance of CThreadInfo class 
    //   (structure/union) where we have a member of CPop3Comm class.
    //   It is done to be able to pass a pointer to the socket in worker thread:
    CThreadInfo m_CThreadInfo;

    //2.Connect the POP3 server:
    int nItem = -1;
    while((nItem = m_Servers.GetNextItem(nItem, LVNI_SELECTED)) != -1)
        //1. Autorization state:////////////////////////////////////////////
        csMailAccount =m_aAccounts[nItem].sServer;
        userName = m_aAccounts[nItem].sUsername;
        //Restore the condition variable if communication was interrupted by the user
        //(it is used later in this function when output message parameters):

        //Pass the pointer to the CThreadInfo class
        //(structure/union) to the worker thread:
        AfxBeginThread(ThreadProc, &m_CThreadInfo);
        //2.Transaction state:
        //2.1 Get number of mails (STAT)
        m_Mails.DeleteAllItems();//clear the Mails list
        //get the number of mails
        nMailNum = m_CThreadInfo.m_Pop3Comm.GetNumberOfMails();

        int nMsgSize;//variable to calculate the size of single message

        // Open file to write the received messages
        // (output and append data).
        //Get the name of server selected and create an output file:
        csFileName =csMailAccount + (".dat");
        ofstream outfile(csFileName,ios::out, ios::app);

        //Get the mails:
        for(nItem = 0; nItem <nMailNum;  nItem++)
            //2.2.Get information regarding the size of a single message
            //or all the messages in a mail drop (LIST):

            if ((nMailNum % 2) == 0)//if even number of messages 
            else//if odd number of messages 

            //2.3 Display a given message's header (TOP).
            //List box is 0-based, but message's numbering is 1-based,
            //thus, add 1 to the list view first line:

            //Output the server's name:
            m_Mails.InsertItem(nItem, csMailAccount);
            //Output mail fields (Attachment, From, Subject, Date):
            if (m_CThreadInfo.m_Pop3Comm.t_Attachment == "text/plain;")
                m_CThreadInfo.m_Pop3Comm.t_Attachment = "";
            if (nSocketClosed ==0)
                m_Mails.SetItemText(nItem, 1, m_CThreadInfo.m_Pop3Comm.t_Attachment);
                m_Mails.SetItemText(nItem, 2, m_CThreadInfo.m_Pop3Comm.t_From);
                m_Mails.SetItemText(nItem, 3, m_CThreadInfo.m_Pop3Comm.t_Subject);
                m_Mails.SetItemText(nItem, 4, m_CThreadInfo.m_Pop3Comm.t_Date);
            else//if communication is interrupted by the user
                m_Mails.DeleteAllItems();//clear the Mails list box

            //2.4 Retrieve a message from the host(RETR):

            //some code here...

            //Disconnect from the server:

            //If mail is received successfully, post message to the worker thread to close 
            //the socket controlling message box (for explanation on the following code 
            //see "Remarks" section in MSDN article on PostThreadMessage API):
            PostThreadMessage(nThreadID, WM_QUIT, 0, 0);
            //to create a message queue needed to handle PostThreadMessage:
            PostThreadMessage(nThreadID, WM_QUIT, 0, 0);
        }//end of "while" loop

The application

The application has a very simple user friendly interface:

The features of the email check program are limited by the purpose of the code sample:

  • It can read only ASCII (Latin alphabet) email messages from multiple POP accounts.
  • The messages are simply output in an Edit Control and a file.

  • It can receive attachments but they have to be cut out of the message and decoded by an external base64 decoding application or using an online service.
  • Text in languages outside ASCII have to be cut out of the message and decoded by an external quoted printable decoding application or using an online service.



  • 04.2009 - Built in Visual C++ 6.0.
  • 04.2011 - Upgraded to Visual C++ 2008.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sergey Chepurin
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionOne file is missing in your rar file! Pin
Hadi Dayvary26-Feb-12 21:02
memberHadi Dayvary26-Feb-12 21:02 
AnswerRe: One file is missing in your rar file! Pin
Sergey Chepurin27-Feb-12 0:23
memberSergey Chepurin27-Feb-12 0:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 18 Apr 2011
Article Copyright 2011 by Sergey Chepurin
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid