EAGetMail POP3 & IMAP4 Component > Developer Center > Retrieve Email and Parse Email in Visual C++ - Tutorial

Retrieve Email and Parse Email in Visual C++ - Tutorial

Introduction

EAGetMail is a POP3 & IMAP4 component which supports all operations of POP3/IMAP4/MIME protocol. This tutorial covers everything of retrieving email and parsing email with EAGetMail in Visual Basic 6.0.

Installation

Before you can use the following sample codes, you should download the EAGetMail Installer and install it on your machine at first.

A simple Visual C++ project

To better demonstrate how to use EAGetMail retrieving and parsing email, let's create a Visual C++ Win32 console project at first.

C++ console project

Add Reference of EAGetMail to Visual C++ Project

To use EAGetMail POP3 & IMAP4 ActiveX Object in your project, the first step is "Add header files of EAGetMail to your project". Please go to "C:\Program Files\EAGetMail\Samples_VS2008\pop3_imap4_simple.vcNative" or "C:\Program Files (x86)\EAGetMail\Samples_VS2008\pop3_imap4_simple.vcNative" folder, find "eagetmailobj.tlh" and "eagetmailobj.tli", and then copy these files to your project folder.

add reference in C++

[Visual C++ Example - Retrieve email from POP3 server]

Now add the following codes to the project. The following code demonstrates how to retrieve email from a POP3 mail account. This sample downloads emails from POP3 server and deletes the email after the email is retrieved.

#include "stdafx.h"

#include "eagetmailobj.tlh"
using namespace EAGetMailObjLib;

int _tmain(int argc, _TCHAR* argv[])
{
    const int MailServerPop3 = 0;
    const int MailServerImap4 = 1;

    // Initialize COM environment
    ::CoInitialize( NULL );

    // Create a folder named "inbox" under current exe file directory
    // to save the emails retrieved.
    TCHAR szPath[MAX_PATH+1];
    memset( szPath, 0, sizeof(szPath));
    ::GetModuleFileName( NULL, szPath,  MAX_PATH );

    // Change file name to current full path
    LPCTSTR psz = _tcsrchr( szPath, _T('\\'));
    if( psz != NULL )
    {
        szPath[psz-szPath] = _T('\0');
    }
    TCHAR szMailBox[MAX_PATH+1];
    memset( szMailBox, 0, sizeof(szMailBox));
    wsprintf( szMailBox, _T("%s\\inbox"), szPath );
    
    // Create a folder to store emails
    ::CreateDirectory( szMailBox, NULL );
    
    try
    {
        IMailServerPtr oServer = NULL;
        oServer.CreateInstance("EAGetMailObj.MailServer");
        oServer->Server = _T("pop3.emailarchitect.net");
        oServer->User = _T("test@emailarchitect.net");
        oServer->Password = _T("testpassword");
        oServer->Protocol = MailServerPop3;

        // If your POP3 requires SSL connection
        // Please add the following codes
        // oServer->SSLConnection = VARIANT_TRUE;
        // oServer->Port = 995;
    
        IMailClientPtr oClient = NULL;
        oClient.CreateInstance("EAGetMailObj.MailClient");
        oClient->LicenseCode = _T("TryIt");

        oClient->Connect( oServer );
        _tprintf(_T("Connected\r\n"));

        _variant_t infos = oClient->GetMailInfos();
        SAFEARRAY *psa = infos.parray;
        long LBound = 0, UBound = 0;
        SafeArrayGetLBound( psa, 1, &LBound );
        SafeArrayGetUBound( psa, 1, &UBound );
    
        INT count = UBound-LBound+1;
        _tprintf(_T("Total %d emails\r\n"), count );

        for( long i = LBound; i <= UBound; i++ )
        {
            _variant_t vtInfo;
            SafeArrayGetElement( psa, &i, &vtInfo );
            
            IMailInfoPtr pInfo;
            vtInfo.pdispVal->QueryInterface(__uuidof(IMailInfo), (void**)&pInfo);

            TCHAR szFile[MAX_PATH+1];
            memset( szFile, 0, sizeof(szFile));
            
            // Generate a random file name by current local datetime,
            // You can use your method to generate the filename if you do not like it
            SYSTEMTIME curtm;
            ::GetLocalTime( &curtm );
            ::wsprintf( szFile, _T("%s\\%04d%02d%02d%02d%02d%02d%03d%d.eml"),
                szMailBox,
                curtm.wYear,
                curtm.wMonth,
                curtm.wDay,
                curtm.wHour,
                curtm.wMinute,
                curtm.wSecond,
                curtm.wMilliseconds,
                i );

            // Receive email from POP3 server
            IMailPtr oMail = oClient->GetMail(pInfo);

            // Save email to local disk
            oMail->SaveAs( szFile, VARIANT_TRUE );

            // Mark email as deleted from POP3 server.
            oClient->Delete( pInfo );
        }
        
        // Delete method just mark the email as deleted, 
        // Quit method pure the emails from server exactly.
        infos.Clear();
        oClient->Quit();
    }
    catch( _com_error &ep )
    {
        _tprintf( _T("Error: %s"), (const TCHAR*)ep.Description());
    }

    return 0;
}

If you set everything right, you can get emails in the mail folder. If the codes threw exception, then please have a look at the following section.

Where can I get my POP3 server address, user and password?

Because each email account provider has different server address, so you should query your POP3 server address from your email account provider. User name is your email address or your email address without domain part. It depends on your email provider setting.

When you execute above example code, if you get error about "Networking connection" or "No such host", it is likely that your POP3 server address is not correct. If you get an error like "Invalid user or password", it is likely that you did not set the correct user or password.

Finally, if you have already set your account in your email client such as Outlook or Window Mail, you can query your POP3 server address, user in your email client. For example, you can choose menu -> "Tools" - > - "Accounts" - > "Your email account" - > "Properties" - > "Servers" in Outlook express or Windows Mail to get your POP3 server, user. Using EAGetMail to receive email does not require you have email client installed on your machine or MAPI, however you can query your exist email accounts in your email client.

Visual C++ console email sample

Next Section

In this section, I introduced the basic things of retrieving email in Visual C++ with EAGetMail. At next section I will introduce how to retrieve email from IMAP4 server.

Next: Retrieve email from IMAP4 server in Visual C++ ->

Comments

If you have any comments or questions about above example codes, please click here to add your comments.

Download

Send Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Retrieve Email and Parse Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Email Solution - Email Server - DomainKeys/DKIM - Disclaimer

2003 - 2011 © Copyright AdminSystem Software Limited. All rights reserved.   About us     Follow emailarchitect on Twitter