Parse Email Body, Attachment and Convert Email to HTML page in Visual C++

In previous section, I introduced how to parse winmail.dat. In this section, I will introduce how to parse email body and attachment, then convert email to a HTML page and display it using Web browser in Visual C++.

Introduction

After the email was converted to HTML page, you can browse it with web browser. You can get everything in the HTML page such as From, To, Cc, Subject, Date, Attachments and Embedded images.

Note

Remarks: All of examples in this section are based on first section: A simple Visual C++ project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference to your project.

[Visual C++ Example - Convert email to HTML]

The following example codes demonstrate how to use EAGetMail POP3 component to convert email to HTML page.

Note

To get the full sample projects, please refer to Samples section.

#include "stdafx.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlstr.h>

#include "eagetmailobj.tlh"
using namespace EAGetMailObjLib;

CString
_FormatHtmlTag( LPCTSTR lpszSrc )
{
    CString src = lpszSrc;

    src.Replace( _T(">"), _T("&gt;"));
    src.Replace( _T("<"), _T("&lt;"));
    return src;
}

void _GenerateHtmlForEmail( CString &htmlName, CString &emlFile, CString& tempFolder )
{
    IMailPtr oMail;
    oMail.CreateInstance(__uuidof(EAGetMailObjLib::Mail));
    oMail->LicenseCode = _T("TryIt");
    oMail->LoadFile( emlFile.GetString(), VARIANT_FALSE );

    if( oMail->IsEncrypted == VARIANT_TRUE )
    {
        //this email is encrypted, we decrypt it by user default certificate.
        oMail = oMail->Decrypt( NULL );

    }

    if( oMail->IsSigned == VARIANT_TRUE )
    {
        // This email is digital signed.
        ICertificatePtr oCert = oMail->VerifySignature();
        _tprintf(_T("This email contains a valid digital signature.\r\n"));
    }

    // Decode winmail.dat (TNEF) and RTF body
    // also convert RTF to HTML automatically
    oMail->DecodeTNEF();

    CString html = (TCHAR*)oMail->HtmlBody;
    CString hdr;
    hdr.Preallocate( 1024 * 5 );

    hdr.Append( _T("<font face=\"Courier New,Arial\" size=2>"));
    hdr.Append( _T("<b>From:</b> "));

    CString tp = (TCHAR*)oMail->From->Name;
    tp += _T("<");
    tp += (TCHAR*)oMail->From->Address;
    tp += _T(">");
    hdr.Append( _FormatHtmlTag(tp.GetString()));
    hdr.Append( _T("<br>"));

    long LBound = 0, UBound = 0;
    SAFEARRAY *psa = NULL;

    _variant_t arAddr = oMail->To;

    psa = arAddr.parray;
    SafeArrayGetLBound( psa, 1, &LBound );
    SafeArrayGetUBound( psa, 1, &UBound );

    INT count = UBound-LBound+1;
    if( count > 0 )
    {
        hdr.Append( _T("<b>To:</b> "));
        for( long i = LBound; i <= UBound; i++ )
        {
            _variant_t vtAddr;
            SafeArrayGetElement( psa, &i, &vtAddr );

            IMailAddressPtr pAddr;
            vtAddr.pdispVal->QueryInterface( __uuidof(IMailAddress), (void**)&pAddr );

            tp = (TCHAR*)pAddr->Name;
            tp += _T("<");
            tp += (TCHAR*)pAddr->Address;
            tp += _T(">");

            hdr.Append( _FormatHtmlTag( tp.GetString()));
            if( i < UBound )
            {
                hdr.Append( _T(";"));
            }
        }
        hdr.Append( _T("<br>"));
    }

    arAddr.Clear();
    arAddr = oMail->Cc;

    psa = arAddr.parray;
    SafeArrayGetLBound( psa, 1, &LBound );
    SafeArrayGetUBound( psa, 1, &UBound );

    count = UBound-LBound+1;
    if( count > 0 )
    {
        hdr.Append( _T("<b>Cc:</b> "));
        for( long i = LBound; i <= UBound; i++ )
        {
            _variant_t vtAddr;
            SafeArrayGetElement( psa, &i, &vtAddr );

            IMailAddressPtr pAddr;
            vtAddr.pdispVal->QueryInterface( __uuidof(IMailAddress), (void**)&pAddr );

            tp = (TCHAR*)pAddr->Name;
            tp += _T("<");
            tp += (TCHAR*)pAddr->Address;
            tp += _T(">");

            hdr.Append( _FormatHtmlTag( tp.GetString()));
            if( i < UBound )
            {
                hdr.Append( _T(";"));
            }
        }
        hdr.Append( _T("<br>"));
    }

    hdr.Append( _T( "<b>Subject:</b>" ));
    hdr.Append( _FormatHtmlTag((TCHAR*)oMail->Subject));
    hdr.Append( _T("<br>"));

    _variant_t atts = oMail->Attachments;
    psa = atts.parray;
    SafeArrayGetLBound( psa, 1, &LBound );
    SafeArrayGetUBound( psa, 1, &UBound );

    count = UBound-LBound+1;
    if( count > 0 )
    {
        ::CreateDirectory( tempFolder.GetString(), NULL );
        hdr.Append( _T("<b>Attachments:</b>"));
        for( long i = LBound; i <= UBound; i++ )
        {
            _variant_t vtAtt;
            SafeArrayGetElement( psa, &i, &vtAtt );

            IAttachmentPtr pAtt;
            vtAtt.pdispVal->QueryInterface( __uuidof(IAttachment), (void**)&pAtt );

            CString name = (TCHAR*)pAtt->Name;

            CString attname = tempFolder;
            attname.Append(_T("\\"));
            attname.Append((TCHAR*)pAtt->Name);
            pAtt->SaveAs( attname.GetString(), VARIANT_TRUE );

            hdr.Append( _T("<a href=\""));
            hdr.Append(attname);
            hdr.Append( _T("\" target=\"_blank\">"));
            hdr.Append((TCHAR*)pAtt->Name);
            hdr.Append(_T("</a> "));

            CString contentID = (TCHAR*)pAtt->ContentID;
            CString contentType = (TCHAR*)pAtt->ContentType;
            if( contentID.GetLength() > 0 )
            {
                CString find = _T("cid:");
                find.Append( contentID );
                // Show embedded image.
                html.Replace( find, attname );
            }
            else if( _tcsnicmp( contentType.GetString(),
                _T("image/"), _tcslen(_T("image/"))) == 0 )
            {
                // Show attached image
                html.Append( _T("<hr><img src=\""));
                html.Append( attname );
                html.Append( _T("\">"));
            }
        }
    }

    hdr.Insert( 0, _T("<meta HTTP-EQUIV=\"Content-Type\" Content=\"text-html; charset=utf-8\">"));

    html = hdr + "<hr>" + html;
    IToolsPtr oTools;
    oTools.CreateInstance(__uuidof(EAGetMailObjLib::Tools))
    oTools->WriteTextFile( htmlName.GetString(), html.GetString(), CP_UTF8 );

    oMail->Clear();
    oMail.Release();
}

void ConvertMailToHtml( LPCTSTR lpszFile )
{
    try
    {
        CString fileName = lpszFile;

        int pos = fileName.ReverseFind(_T('.'));
        CString mainName = fileName.Mid(0, pos);
        CString htmlName = mainName + _T(".htm");

        CString tempFolder = mainName;
        WIN32_FIND_DATA findData;
        HANDLE hFind = ::FindFirstFile( htmlName.GetString(), &findData );
        BOOL bExist = FALSE;

        if( hFind != INVALID_HANDLE_VALUE )
        {
            ::FindClose( hFind );
            bExist = TRUE;
        }

        if( !bExist )
        {   // We haven't generate the html for this email, generate it now.
            _GenerateHtmlForEmail( htmlName, fileName, tempFolder );
        }

    }
    catch(  _com_error &ep )
    {
        _tprintf(_T("Error: %s\r\n"), (const TCHAR*)ep.Description());
    }
}

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
    {
        IToolsPtr oTools = NULL;
        oTools.CreateInstance(__uuidof(EAGetMailObjLib::Tools));
        TCHAR szFind[MAX_PATH+1];
        memset( szFind, 0, sizeof(szFind));
        wsprintf( szFind, _T("%s\\*.eml"), szMailBox );

        // Get all *.eml files in specified folder and parse it one by one.
        _variant_t files = oTools->GetFiles( szFind );
        SAFEARRAY *psa = files.parray;
        long i = 0, LBound = 0, UBound = 0;
        SafeArrayGetLBound( psa, 1, &LBound );
        SafeArrayGetUBound( psa, 1, &UBound );
        for( i = LBound; i <= UBound; i++ )
        {
            _variant_t vt;
            SafeArrayGetElement( psa, &i, &vt );

            ConvertMailToHtml(_bstr_t(vt.bstrVal));
        }
    }
    catch( _com_error &ep )
    {
        _tprintf( _T("Error: %s"), (const TCHAR*)ep.Description());
    }

    return 0;
}

In EAGetMail installer, there are many samples demonstrate how to use Web browser control to display the email, I suggest that you download it and have a try

pop3, imap4 samples

Next Section

At next section I will introduce how to parse Non-delivery report.

Appendix

Comments

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