MailClient Object


Provides properties and methods for receiving messages from POP3/IMAP4 server and managing IMAP4 folder.

IDispatch
    IMailClient

[Visual Basic 6.0]
Public Class MailClient
[Visual C++]
public interface IMailClient

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

Connected Gets whether the server is connected.
Conversation Gets the latest conversation beween mail server and mail client.
CurrentMailServer Gets the latest mail server that is used to receive e-mail message.
Imap4Folders Gets the folders on IMAP4 server.
LicenseCode Sets license code to current MailClient instance.
LogFileName Gets or sets the log file name for mail transaction.
SelectedFolder Gets the current selected folder of IMAP4 server.
Tag

Gets or sets an object that contains data to associate with the MailClient.

Timeout

Gets or sets the maximum time interval(seconds) the object will wait for a response from server before returning an error.

Public Methods

Append Appends an email to specified IMAP4 folder.
AppendEx Appends an email to specified IMAP4 folder with flags.
ChangeMailFlags Changes the flags of an email on IMAP4 server.
Close Closes the connection to server forcely.
Connect Connects a mail server.
Copy Copies a specified email from selected folder to another folder on IMAP4 server.
CreateFolder Creates an IMAP4 folder on IMAP4 server.
Delete Marks the specified email as deleted.
DeleteFolder Removes the specified IMAP folder from IMAP4 server.
Expunge Pures the deleted emails in current selected folder on IMAP4 server.
GetMail Receives a specified email from mail server.
GetMailHeader Receives a specified email headers from mail server.
GetMailInfos Retrieves all emails' information on mail server.
GetMailsByQueue Retrieves emails by EAGetMail Service in background.
Logout Disconnects with IMAP4 server without expunging the deleted emails.
Quit Disconnects the mail server and expunging the deleted emails.
Reset Undeletes the deleted emails on mail server.
SearchMail Searchs emails in sepecified IMAP4 folder.
SelectFolder Selects an IMAP4 folder to operate.
SubscribeFolder Subscribes an IMAP4 folder.
UnsubscribeFolder Unsubscribes an IMAP4 folder.

Public Events

OnAuthorized Occurs when mail server has authorized the user.
OnConnected Occurs when the client has connected to mail server successfully.
OnIdle Occurs when the client is connecting server or waiting response from mail server.
OnQuit Occurs when the client is disconnecting the mail server.
OnReceiveResponse Occurs when the client has received a response from mail server.
OnReceivingDataStream Occurs when the client is receiving the e-mail data from mail server.
OnSecuring Occurs when the client is establishing the SSL connection.
OnSendCommand Occurs when the client has sent a command to the mail server.
OnSendingDataStream Occurs when the client is sending the e-mail data (Append) to the IMAP4 server.

Example

[VisualĀ Basic 6.0, VBScript, Visual C++] The following example demonstrates how to receive email with EAGetMail POP3 & IMAP ActiveX Object, but it doesn't demonstrates the events and mail parsing usage. To get the full samples of EAGetMail, please refer to Samples section.

[Visual Basic 6.0]
Public Sub ReceiveMail( _
ByVal sServer As String, _
ByVal sUserName As String, _
ByVal sPassword As String, _
ByVal bSSLConnection As Boolean)
    
    Const MailServerPop3 = 0
    Const MailServerImap4 = 1

    '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.
    Dim oClient As New EAGetMailObjLib.MailClient
    oClient.LicenseCode = "TryIt"
    
    'To receive email from imap4 server, please change
    'MailServerPop3 to MailServerImap4 in MailServer constructor
    Dim oServer As New EAGetMailObjLib.MailServer
    oServer.Server = sServer
    oServer.User = sUserName
    oServer.Password = sPassword
    oServer.SSLConnection = bSSLConnection
    oServer.Protocol = MailServerPop3
    
    ''by default, the pop3 port is 110, imap4 port is 143,
    'the pop3 ssl port is 995, imap4 ssl port is 993
    'you can also change the port like this
    'oServer.Port = 110
    If oServer.Protocol = MailServerImap4 Then
        If oServer.SSLConnection Then
            oServer.Port = 993 'SSL IMAP4
        Else
            oServer.Port = 143 'IMAP4 normal
        End If
    Else
        If oServer.SSLConnection Then
            oServer.Port = 995 'SSL POP3
        Else
            oServer.Port = 110 'POP3 normal
        End If
    End If
    
    On Error GoTo ErrorHandle
        oClient.Connect oServer
        Dim infos
        infos = oClient.GetMailInfos()
        
        Dim i, Count
        Count = UBound(infos)
        For i = LBound(infos) To Count
            Dim info As EAGetMailObjLib.MailInfo
            Set info = infos(i)
            
            MsgBox "UIDL: " & info.UIDL
            MsgBox "Index: " & info.Index
            MsgBox "Size: " & info.Size
            'For POP3 server, the IMAP4MailFlags is meaningless.
            MsgBox "Flags: " & info.IMAP4Flags 

            Dim oMail As EAGetMailObjLib.Mail
            Set oMail = oClient.GetMail(info)
            'Save mail to local
            oMail.SaveAs "d:\tempfolder\" & i & ".eml", True
        Next

        For i = LBound(infos) To Count
            oClient.Delete (infos(i))
        Next

        '' Delete method just mark the email as deleted,
        ' Quit method pure the emails from server exactly.
        oClient.Quit
        Exit Sub
ErrorHandle:
        ''Error handle
        MsgBox Err.Description
        
        oClient.Close
End Sub

[VBScript]
Sub ReceiveMail( _
ByVal sServer, _
ByVal sUserName, _
ByVal sPassword, _
ByVal bSSLConnection)
    
    Const MailServerPop3 = 0
    Const MailServerImap4 = 1

    '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.
    Dim oClient
    Set oClient = CreateObject("EAGetMailObj.MailClient")
    oClient.LicenseCode = "TryIt"
    
    'To receive email from imap4 server, please change
    'MailServerPop3 to MailServerImap4 in MailServer constructor
    Dim oServer
    Set oServer = CreateObject("EAGetMailObj.MailServer")
    oServer.Server = sServer
    oServer.User = sUserName
    oServer.Password = sPassword
    oServer.SSLConnection = bSSLConnection
    oServer.Protocol = MailServerPop3
    
    ''by default, the pop3 port is 110, imap4 port is 143,
    'the pop3 ssl port is 995, imap4 ssl port is 993
    'you can also change the port like this
    'oServer.Port = 110
    If oServer.Protocol = MailServerImap4 Then
        If oServer.SSLConnection Then
            oServer.Port = 993 'SSL IMAP4
        Else
            oServer.Port = 143 'IMAP4 normal
        End If
    Else
        If oServer.SSLConnection Then
            oServer.Port = 995 'SSL POP3
        Else
            oServer.Port = 110 'POP3 normal
        End If
    End If
    

    oClient.Connect oServer
    Dim infos
    infos = oClient.GetMailInfos()
    
    Dim i, Count
    Count = UBound(infos)
    For i = LBound(infos) To Count
        Dim info
        Set info = infos(i)
        
        MsgBox "UIDL: " & info.UIDL
        MsgBox "Index: " & info.Index
        MsgBox "Size: " & info.Size
        'For POP3 server, the IMAP4MailFlags is meaningless.
        MsgBox "Flags: " & info.IMAP4Flags 
                    
        Dim oMail
        Set oMail = oClient.GetMail(info)
        'Save mail to local
        oMail.SaveAs "d:\tempfolder\" & i & ".eml", True
    Next

    For i = LBound(infos) To Count
        oClient.Delete (infos(i))
    Next

    '' Delete method just mark the email as deleted,
    ' Quit method pure the emails from server exactly.
    oClient.Quit
End Sub

[Visual C++]
#include "eagetmailobj.tlh"
using namespace EAGetMailObjLib;

void ReceiveMail( 
        LPCTSTR sServer, 
        LPCTSTR sUserName,
        LPCTSTR sPassword,
        bool bSSLConnection)
{
    ::CoInitialize( NULL );

    const int MailServerPop3 = 0;
    const int MailServerImap4 = 1;

    try
    {
        IMailClientPtr oClient;
        oClient.CreateInstance( "EAGetMailObj.MailClient" );

        IMailServerPtr oServer;
        oServer.CreateInstance( "EAGetMailObj.MailServer" );
        
        // 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.
        oClient->LicenseCode = _T("TryIt");

        oServer->Server = sServer;
        oServer->User = sUserName;
        oServer->Password = sPassword;
        
        INT nProtocol = MailServerPop3;

        oServer->Protocol = nProtocol;
        if( nProtocol == MailServerPop3 )
        {
            if( bSSLConnection )
            {
                oServer->Port = 995;
                oServer->SSLConnection = VARIANT_TRUE;
            }
            else
            {
                oServer->Port = 110;
            }
        }
        else
        {
            if( bSSLConnection )
            {
                oServer->Port = 993;
                oServer->SSLConnection = VARIANT_TRUE;
            }
            else
            {
                oServer->Port = 143;
            }
        }

        oClient->Connect( oServer );

        _variant_t arInfo = oClient->GetMailInfos();
        SAFEARRAY *psa = arInfo.parray;

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

            _tprintf( _T("UIDL: %s\r\n"), (TCHAR*)pInfo->UIDL );
            _tprintf( _T("Index: %d\r\n"), (TCHAR*)pInfo->Index);
            _tprintf( _T("Size: %d\r\n"), (TCHAR*)pInfo->Size);
            //For POP3 server, the IMAP4MailFlags is meaningless
            _tprintf( _T("Flags: %s\r\n"), (TCHAR*)pInfo->IMAP4Flags );

            IMailPtr oMail = oClient->GetMail(pInfo);
            TCHAR szFile[MAX_PATH+1];
            memset(szFile, 0, sizeof(szFile));
            ::wsprintf( szFile, _T("d:\\tempfolder\\%d.eml"), i );
            //save to local disk
            oMail->SaveAs( szFile, VARIANT_TRUE );
            oMail.Release();
        }

        
        for( long i = LBound; i <= UBound; i++)
        {
            _variant_t vtInfo;
            SafeArrayGetElement( psa, &i, &vtInfo );

            IMailInfoPtr pInfo;
            vtInfo.pdispVal->QueryInterface(__uuidof(IMailInfo), (void**)&pInfo);

            //delete email from server
            oClient->Delete( pInfo );
        }

        // Delete method just mark the email as deleted, 
        // Quit method pure the emails from server exactly.
        arInfo.Clear();
        oClient->Quit();
    }
    catch( _com_error &ep )
    {
        _tprintf( _T("ERROR: %s\r\n"),  (TCHAR*)ep.Description());
    }

    ::CoUninitialize();
}