Mail Object


Provides properties and methods for presenting an e-mail message.

IDispatch
    IMail

[Visual Basic 6.0]
Public Class Mail
[Visual C++]
public: interface Mail

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Public Properties

Attachments Gets an array of the attachments of the email message.
BodyHeaders Gets the headers of the body part.
Cc Gets an array of e-mail addresses that receive a carbon copy (CC) of the e-mail message.
Content Gets the content of the email message.
From Gets the e-mail address of the sender.
Headers Gets the HeaderCollection for headers of the e-mail message.
HtmlBody Gets the html body of the e-mail message.
LicenseCode Sets your Mail object license code.
IsEncrypted Gets whether the email is encrypted by digital certificate.
IsReport Gets whether the email is a delivery report.
IsSigned Gets whether the email has a digital signature.
Priority Gets the priority of the e-mail message.
ReceivedDate Gets the date time when the server received this email.
ReplyTo Gets the email address to reply this email.
SentDate Gets the date time when the sender sent this email.
Subject Gets the subject of the e-mail message.
Tag Gets or sets an object that contains data to associate with the Mail.
TextBody Gets the text body of the e-mail message.
To Gets an array of recipient e-mail addresses.

Public Methods

Clear Clears all data in Mail class.
Decrypt Decrypts the encrypted email by digital certificate.
GetReport Gets detailed information of a delivery report.
Load Loads a RFC822 message binary email content to current Mail instance.
LoadFile Loads a RFC822 message ( *.eml) file to current Mail instance.
ParseTNEF Parses the winmail.dat (TNEF attachment).
SaveAs Saves the e-mail message to a file (*.eml file).
VerifySignature Verifies the digital signature of the email.

Example

[Visual Basic 6.0, VBScript, Visual C++] The following example demonstrates how to parse email with EAGetMail POP3 & IMAP4 ActiveX Object. To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic 6.0]
Private Function FormatHtmlTag(ByVal src As String) As String
    src = Replace(src, ">", ">")
    src = Replace(src, "<", "&lt;")
    FormatHtmlTag = src
End Function

' we parse the email file and generate a html + attachment folder for the email, once the html is create,
' you can open this html with IE and view the content and get the attachments.
Private Sub ParseEmailToHtml(emlFile As String)
    On Error GoTo ErrorHandle
    
    Dim pos
    pos = InStrRev(emlFile, ".")
    Dim mainName
    Dim htmlName
    mainName = Mid(emlFile, 1, pos - 1)
    htmlName = mainName & ".htm"

    Dim tempFolder As String
    tempFolder = mainName
    
    Dim oMail As New EAGetMailObjLib.Mail
    'For evaluation usage, please use "TryIt" as the license code, otherwise the
    '"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
    '"trial version expired" exception will be thrown.
    oMail.LicenseCode = "TryIt"
        
    
    oMail.LoadFile emlFile, False
    If Err.Number <> 0 Then
        MsgBox Err.Description
        Exit Sub
    End If
    
    On Error Resume Next
    If oMail.IsEncrypted Then
        Set oMail = oMail.decrypt(Nothing)
        If Err.Number <> 0 Then
            MsgBox Err.Description
            Err.Clear
        End If
    End If
    
    If oMail.IsSigned Then
        oMail.VerifySignature
        If Err.Number <> 0 Then
            MsgBox Err.Description
            Err.Clear
        End If
    End If
    
    On Error GoTo ErrorHandle
    
    Dim oTools As New EAGetMailObjLib.Tools
    Dim html As String
    html = oMail.HtmlBody
    Dim hdr As String
    hdr = hdr & "<font face=""Courier New,Arial"" size=2>"
    hdr = hdr & "<b>From:</b> " + FormatHtmlTag(oMail.From.Name & "<" & oMail.From.Address & ">") + "<br>"
    
    
    Dim addrs
    addrs = oMail.To
    Dim i, Count
    
    i = LBound(addrs)
    Count = UBound(addrs)
    If (Count >= i) Then
        hdr = hdr & "<b>To:</b> "
        For i = LBound(addrs) To Count
            hdr = hdr & FormatHtmlTag(addrs(i).Name & "<" & addrs(i).Address & ">")
            If (i < Count) Then
                hdr = hdr & ";"
            End If
        Next
        hdr = hdr & "<br>"
    End If
    
    addrs = oMail.Cc
    i = LBound(addrs)
    Count = UBound(addrs)
    If (Count >= i) Then
        hdr = hdr & "<b>Cc:</b> "
        For i = LBound(addrs) To Count
            hdr = hdr & FormatHtmlTag(addrs(i).Name & "<" & addrs(i).Address & ">")
            If (i < Count) Then
                hdr = hdr & ";"
            End If
        Next
        hdr = hdr & "<br>"
    End If
        
    hdr = hdr & "<b>Subject:</b>" & FormatHtmlTag(oMail.Subject) & "<br>" & vbCrLf
    Dim atts
    atts = oMail.Attachments
    i = LBound(atts)
    Count = UBound(atts)
    If (Count >= i) Then
        If Not oTools.ExistFile(tempFolder) Then
            oTools.CreateFolder (tempFolder)
        End If
        
        hdr = hdr & "<b>Attachments:</b>"
        For i = LBound(atts) To Count
            Dim att As Attachment
            Set att = atts(i)
            'this is a OUTLOOK RTF attachment, parse it
            If LCase(att.Name) = "winmail.dat" Then
                Dim tatts
                tatts = oMail.ParseTNEF(att.Content, True)
                For x = LBound(tatts) To UBound(tatts)
                        Dim tatt As Attachment
                        Set tatt = tatts(x)
                        Dim tattname As String
                        tattname = tempFolder & "\" & tatt.Name
                        tatt.SaveAs tattname, True
                        hdr = hdr & "<a href=""" & tattname & """ target=""_blank"">" & tatt.Name & "</a> "
                Next
                
            Else
                Dim attname
                attname = tempFolder & "\" & att.Name
                att.SaveAs attname, True
                hdr = hdr & "<a href=""" & attname & """ target=""_blank"">" & att.Name & "</a> "
                If Len(att.ContentID) > 0 Then
                    'show embedded image.
                    html = Replace(html, "cid:" + att.ContentID, attname)
                ElseIf InStr(1, att.ContentType, "image/", vbTextCompare) = 1 Then
                    'show attached image.
                    html = html & "<hr><img src=""" & attname & """>"
                End If
            End If
        Next
    End If
    
    hdr = "<meta http-equiv=""Content-Type"" content=""text-html; charset=utf-8"">" & hdr
    html = hdr & "<hr>" & html
    oTools.WriteTextFile htmlName, html, 65001
    oMail.Clear
    Exit Sub
    
ErrorHandle:
    MsgBox "Failed to generate html file for the email; " & Err.Description
    
End Sub	

[VBScript]
Function FormatHtmlTag(ByVal src)
    src = Replace(src, ">", "&gt;")
    src = Replace(src, "<", "&lt;")
    FormatHtmlTag = src
End Function

' we parse the email file and generate a html + attachment folder for the email, once the html is create,
' you can open this html with IE and view the content and get the attachments.
Sub ParseEmailToHtml(emlFile As String)
    
    Dim pos
    pos = InStrRev(emlFile, ".")
    Dim mainName
    Dim htmlName
    mainName = Mid(emlFile, 1, pos - 1)
    htmlName = mainName & ".htm"

    Dim tempFolder
    tempFolder = mainName
    
    Dim oMail 
    Set oMail = CreateObject("EAGetMailObj.Mail")
    'For evaluation usage, please use "TryIt" as the license code, otherwise the
    '"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
    '"trial version expired" exception will be thrown.
    oMail.LicenseCode = "TryIt"
    
    oMail.LoadFile emlFile, False
    
    If oMail.IsEncrypted Then
        Set oMail = oMail.decrypt(Nothing)
    End If
    
    If oMail.IsSigned Then
        oMail.VerifySignature
    End If

    Dim oTools 
    Set oTools = CreateObject("EAGetMailObj.Tools")
    Dim html
    html = oMail.HtmlBody
    Dim hdr
    hdr = hdr & "<font face=""Courier New,Arial"" size=2>"
    hdr = hdr & "<b>From:</b> " + FormatHtmlTag(oMail.From.Name & "<" & oMail.From.Address & ">") + "<br>"
    
    
    Dim addrs
    addrs = oMail.To
    Dim i, Count
    
    i = LBound(addrs)
    Count = UBound(addrs)
    If (Count >= i) Then
        hdr = hdr & "<b>To:</b> "
        For i = LBound(addrs) To Count
            hdr = hdr & FormatHtmlTag(addrs(i).Name & "<" & addrs(i).Address & ">")
            If (i < Count) Then
                hdr = hdr & ";"
            End If
        Next
        hdr = hdr & "<br>"
    End If
    
    addrs = oMail.Cc
    i = LBound(addrs)
    Count = UBound(addrs)
    If (Count >= i) Then
        hdr = hdr & "<b>Cc:</b> "
        For i = LBound(addrs) To Count
            hdr = hdr & FormatHtmlTag(addrs(i).Name & "<" & addrs(i).Address & ">")
            If (i < Count) Then
                hdr = hdr & ";"
            End If
        Next
        hdr = hdr & "<br>"
    End If
        
    hdr = hdr & "<b>Subject:</b>" & FormatHtmlTag(oMail.Subject) & "<br>" & vbCrLf
    Dim atts
    atts = oMail.Attachments
    i = LBound(atts)
    Count = UBound(atts)
    If (Count >= i) Then
        If Not oTools.ExistFile(tempFolder) Then
            oTools.CreateFolder (tempFolder)
        End If
        
        hdr = hdr & "<b>Attachments:</b>"
        For i = LBound(atts) To Count
            Dim att
            Set att = atts(i)
            'this is a OUTLOOK RTF attachment, parse it
            If LCase(att.Name) = "winmail.dat" Then
                Dim tatts
                tatts = oMail.ParseTNEF(att.Content, True)
                For x = LBound(tatts) To UBound(tatts)
                        Dim tatt
                        Set tatt = tatts(x)
                        Dim tattname
                        tattname = tempFolder & "\" & tatt.Name
                        tatt.SaveAs tattname, True
                        hdr = hdr & "<a href=""" & tattname & """ target=""_blank"">" & tatt.Name & "</a> "
                Next
                
            Else
                Dim attname
                attname = tempFolder & "\" & att.Name
                att.SaveAs attname, True
                hdr = hdr & "<a href=""" & attname & """ target=""_blank"">" & att.Name & "</a> "
                If Len(att.ContentID) > 0 Then
                    'show embedded image.
                    html = Replace(html, "cid:" + att.ContentID, attname)
                ElseIf InStr(1, att.ContentType, "image/", vbTextCompare) = 1 Then
                    'show attached image.
                    html = html & "<hr><img src=""" & attname & """>"
                End If
            End If
        Next
    End If
    
    hdr = "<meta http-equiv=""Content-Type"" content=""text-html; charset=utf-8"">" & hdr
    html = hdr & "<hr>" & html
    oTools.WriteTextFile htmlName, html, 65001
    oMail.Clear

End Sub	

[Visual C++]
//if you do not use MFC, please add this line to support CString type.
#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;
}
// we parse the email file and generate a html + attachment folder for the email, once the html is create,
// you can open this html with IE and view the content and get the attachments.
void
ParseEmailToHtml( CString &emlFile )
{
    try
    {
        int pos = emlFile.ReverseFind(_T('.'));
        CString mainName = emlFile.Mid(0, pos);
        CString htmlName = mainName + _T(".htm");

        CString tempFolder = mainName;
        //For evaluation usage, please use "TryIt" as the license code, otherwise the 
        //"invalid license code" exception will be thrown. However, the object will expire in 1-2 months, then
        //"trial version expired" exception will be thrown.
        IMailPtr oMail;
        oMail.CreateInstance( "EAGetMailObj.Mail");
        oMail->LicenseCode = _T("TryIt");
        oMail->LoadFile( emlFile.GetString(), VARIANT_FALSE );

        if( oMail->IsEncrypted == VARIANT_TRUE )
        {
            try
            {
                //this email is encrypted, we decrypt it by user default certificate.
                // you can also use specified certificate like this
                // oCert = new Certificate();
                // oCert.Load("c:\\test.pfx", "pfxpassword", Certificate.CertificateKeyLocation.CRYPT_USER_KEYSET)
                // oMail = oMail.Decrypt( oCert );
                oMail = oMail->Decrypt( NULL );
            }
            catch( _com_error &ep )
            {
                _tprintf( _T("%s\r\n"), (TCHAR*)ep.Description());
                oMail->LoadFile( emlFile.GetString(), VARIANT_FALSE );
            }	
        }

        if( oMail->IsSigned == VARIANT_TRUE )
        {
            try
            {
                //this email is digital signed.
                ICertificatePtr oCert = oMail->VerifySignature();
                _tprintf( _T("This email contains a valid digital signature.\r\n"));
                //you can add the certificate to your certificate storage like this
                //cert.AddToStore( Certificate.CertificateStoreLocation.CERT_SYSTEM_STORE_CURRENT_USER,
                //	"addressbook" );
                // then you can use send the encrypted email back to this sender.
            }
            catch(_com_error &ep )
            {
                _tprintf( _T("%s\r\n"), (TCHAR*)ep.Description());
            }
        }

        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 format, decode it here.
                    _variant_t tatts;
                    try
                    {
                        tatts = oMail->ParseTNEF( pAtt->Content, VARIANT_TRUE );
                    }
                    catch(_com_error &ep )
                    {
                        _tprintf( _T("%s\r\n"), (TCHAR*)ep.Description() );
                        continue;
                    }

                    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();

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