Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
ivan  
#1 Posted : Monday, May 2, 2011 5:14:53 AM(UTC)
ivan

Rank: Administration

Groups: Administrators
Joined: 11/11/2010(UTC)
Posts: 1,154

Thanks: 9 times
Was thanked: 55 time(s) in 55 post(s)
Visual C++ Example

The following code demonstrates how to convert email to a HTML page and display it using Web browser in C#.

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.

Code:

// The following example codes demonstrate converting email to HTML page
// To get full sample projects, please download and install EAGetMail on your machine.
// To run it correctly, please change email server, user, password, folder, file name value to yours

#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( "EAGetMailObj.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")); 
    } 

    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; 
            if( name.CompareNoCase( _T("winmail.dat")) == 0 ) 
            { 
                // this attachment is in OUTLOOK RTF (TNEF) format, decode it here.
                _variant_t tatts; 
                tatts = oMail->ParseTNEF( pAtt->Content, VARIANT_TRUE ); 

                long XLBound = 0, XUBound = 0; 

                SAFEARRAY* tpsa = tatts.parray; 
                SafeArrayGetLBound( tpsa, 1, &XLBound ); 
                SafeArrayGetUBound( tpsa, 1, &XUBound ); 
                for( long x = XLBound; x <= XUBound; x++ ) 
                { 
                    _variant_t vttAtt; 
                    SafeArrayGetElement( tpsa, &x, &vttAtt ); 
                    IAttachmentPtr ptAtt; 
                    vttAtt.pdispVal->QueryInterface( __uuidof(IAttachment), (void**)&ptAtt ); 

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

                    hdr.Append( _T("<a href=\"")); 
                    hdr.Append(tattname); 
                    hdr.Append( _T("\" target=\"_blank\">")); 
                    hdr.Append((TCHAR*)ptAtt->Name); 
                    hdr.Append(_T("</a> ")); 
                } 
                continue; 
            } 

            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("EAGetMailObj.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[]) 
{ 
    // Initialize COM environment
    ::CoInitialize( NULL ); 

    ConvertMailToHtml(_T("c:\\my folder\\test.eml")); 

    return 0; 
} 

Click here to read original topic - full version ...

If you have any comments or questions about above example codes, please add your comments here.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.043 seconds.

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.