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.
GetMailInfosParam Set search filters/parameters for GetMailInfos method.
Imap4Folders Gets the folders on IMAP4 server. This property is obsoleted by GetFolderList method.
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/Exchange folder.
AppendEx Appends an email to specified IMAP4/Exchange folder with flags.
CancelWaitEmail Cancel WaitNewEmail method.
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/Exchange server.
Move Moves a specified email from selected folder to another folder on IMAP4/Exchange 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 Expunges the deleted emails in current selected folder on IMAP4 server.
GetCategories Get categories of the email message, only EWS and Graph API support this method.
GetFollowUpFlag Get follow-up flag of the email message, only EWS and Graph API support this method.
GetFolderList Gets the folders on server.
GetMail Receives a specified email from mail server.
GetMailCount Get total emails count on POP3/IMAP4/MS Exchange Server.
GetMailHeader Receives a specified email headers from mail server.
GetMailInfos Retrieves all emails' information on mail server. This method is obsoleted by GetMailInfoList.
GetMailInfoList 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.
MarkAsRead Change the email read flag on IMAP4/Exchange server.
QueryEWSPublicFolders Retrieves all public folders from MS Exchange Server 2007 or later version. This method is obsoleted by GetEWSPublicFolderList method.
GetEWSPublicFolderList Retrieves all public folders from MS Exchange Server 2007 or later version.
Quit Disconnects the mail server and expunging the deleted emails.
Reset Undeletes the deleted emails on mail server.
RenameFolder Rename the specified folder on IMAP4/Exchange server.
SearchMail Searchs emails in sepecified IMAP4 folder. This method is obsoleted by SearchMailList method.
SearchMailList Searchs emails in sepecified IMAP4 folder.
SelectFolder Selects an IMAP4 folder to operate.
SetCategories Set categories to the email message, only EWS and Graph API support this method.
SetFollowUpFlag Set follow-up flag to the email message, only EWS and Graph API support this method.
SubscribeFolder Subscribes an IMAP4 folder.
UnsubscribeFolder Unsubscribes an IMAP4 folder.
Undelete Undeletes specified deleted email on IMAP4 server.
WaitNewEmail Get notification of new email event on IMAP4 server and MS Exchange Server.

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
    Const MailServerEWS = 2
    Const MailServerDAV = 3
    Const MailServerMsGraph = 4

    '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 to MailServer.Protocol

    'To receive email with Exchange Web Service, please change
    'MailServerPop3 to MailServerEWS to MailServer.Protocol

    'To receive email with Exchange WebDAV, please change
    'MailServerPop3 to MailServerDAV to MailServer.Protocol

    'Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007
    'or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4
    'to download email from Exchange Server, you can use Exchange Web Service(Exchange 2007/2010 or
    'later version) or WebDAV(Exchange 2000/2003) protocol.

    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 As EAGetMailObjLib.MailInfoCollection
        Set infos = oClient.GetMailInfoList()
        
        Dim i
        For i = 0 To infos.Count - 1
            Dim info As EAGetMailObjLib.MailInfo
            Set info = infos.Item(i)
            
            Dim oMail As EAGetMailObjLib.Mail
            Set oMail = oClient.GetMail(info)
            'Save mail to local
            oMail.SaveAs "d:\tempfolder\" & i & ".eml", True

            ' Delete email from server
            oClient.Delete info
        Next
        
        '' Delete method just mark the email as deleted,
        ' Quit method expunge the emails from server permanently.
        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 Const MailServerEWS = 2 Const MailServerDAV = 3 Const MailServerMsGraph = 4 '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 to MailServer.Protocol 'To receive email with Exchange Web Service, please change 'MailServerPop3 to MailServerEWS to MailServer.Protocol 'To receive email with Exchange WebDAV, please change 'MailServerPop3 to MailServerDAV to MailServer.Protocol 'Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 'or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 'to download email from Exchange Server, you can use Exchange Web Service(Exchange 2007/2010 or 'later version) or WebDAV(Exchange 2000/2003) protocol. 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 Set infos = oClient.GetMailInfoList() Dim i For i = 0 To infos.Count - 1 Dim info Set info = infos.Item(i) Dim oMail Set oMail = oClient.GetMail(info) 'Save mail to local oMail.SaveAs "d:\tempfolder\" & i & ".eml", True ' Delete email from server oClient.Delete info Next '' Delete method just mark the email as deleted, ' Quit method expunge the emails from server permanently. oClient.Quit End Sub
[Visual C++] #include "stdafx.h" #include <windows.h> #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; const int MailServerEWS = 2; const int MailServerDAV = 3; const int MailServerMsGraph = 4; try { IMailClientPtr oClient; oClient.CreateInstance(__uuidof(EAGetMailObjLib::MailClient)); IMailServerPtr oServer; oServer.CreateInstance(__uuidof(EAGetMailObjLib::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; //To receive email from imap4 server, please change //MailServerPop3 to MailServerImap4 in MailServer constructor //To receive email with Exchange Web Service, please change //MailServerPop3 to MailServerEWS to MailServer.Protocol //To receive email with Exchange WebDAV, please change //MailServerPop3 to MailServerDAV to MailServer.Protocol //Exchange Server supports POP3/IMAP4 protocol as well, but in Exchange 2007 //or later version, POP3/IMAP4 service is disabled by default. If you don't want to use POP3/IMAP4 //to download email from Exchange Server, you can use Exchange Web Service(Exchange 2007/2010 or //later version) or WebDAV(Exchange 2000/2003) protocol. //For Exchange Web Service/WebDAV, please ignore //Port property. But for Exchange Web Service, please set SSLConnection to True 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); IMailInfoCollectionPtr infos = oClient->GetMailInfoList(); for(long i = 0; i < infos->Count; i++) { IMailInfoPtr pInfo = infos->GetItem(i); 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(); // delete email from server oClient->Delete(pInfo); } // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient->Quit(); } catch(_com_error &ep) { _tprintf(_T("ERROR: %s\r\n"), (TCHAR*)ep.Description()); } ::CoUninitialize(); }
[Delphi] unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, EAGetMailObjLib_TLB; type TForm1 = class(TForm) private { Private declarations } procedure ReceiveMail(server: WideString; user: WideString; password: WideString; useSslConnection: Boolean); public { Public declarations } end; const MailServerPop3 = 0; MailServerImap4 = 1; MailServerEWS = 2; MailServerDAV = 3; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.ReceiveMail(server: WideString; user: WideString; password: WideString; useSslConnection: Boolean); var oServer: TMailServer; oClient: TMailClient; oTools: TTools; oMail: IMail; infos: IMailInfoCollection; oInfo: IMailInfo; localInbox, fileName: WideString; i: Integer; begin try // set current thread code page to system default code page. SetThreadLocale(GetSystemDefaultLCID()); oTools := TTools.Create(Application); // Create a folder named "inbox" under // current directory to store the email files localInbox := GetCurrentDir() + '\inbox'; oTools.CreateFolder(localInbox); oServer := TMailServer.Create(Application); oServer.Server := server; oServer.User := user; oServer.Password := password; oServer.Protocol := MailServerPop3; // Enable SSL/TLS Connection, most modern email server require SSL/TLS connection by default. oServer.SSLConnection := useSslConnection; if useSslConnection then begin // Set 995 SSL POP3 port, for IMAP4, use 993 SSL port oServer.Port := 995; end else begin // Set 110 POP3 port, for IMAP4, use 143 port oServer.Port := 110; end; // 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 := TMailClient.Create(Application); oClient.LicenseCode := 'TryIt'; oClient.Connect1(oServer.DefaultInterface); ShowMessage('Connected!'); infos := oClient.GetMailInfoList(); ShowMessage(Format('Total %d email(s)', [infos.Count])); for i := 0 to infos.Count - 1 do begin oInfo := infos.Item[i]; ShowMessage(Format('Index: %d; Size: %d; UIDL: ' + oInfo.UIDL, [oInfo.Index, oInfo.Size])); // Generate a random file name by current local datetime, // You can use your method to generate the filename if you do not like it fileName := localInbox + '\' + oTools.GenFileName(i) + '.eml'; // Receive email from POP3 server oMail := oClient.GetMail(oInfo); ShowMessage('From: ' + oMail.From.Address + #13#10 + 'Subject: ' + oMail.Subject); // Save email to local disk oMail.SaveAs(fileName, true); // Mark email as deleted from POP3 server oClient.Delete(oInfo); end; // Quit and expunge emails marked as deleted from POP3 server oClient.Quit; except on ep:Exception do ShowMessage('Error: ' + ep.Message); end; end; end.

See Also

User Authentication and SSL/TLS Connection
MailClient.Connect Method

Online Tutorials

Read Email over SSL/TLS Connection in Delphi - Tutorial
Read Email from Gmail Account in Delphi - Tutorial
Read Email from Yahoo Account in Delphi - Tutorial
Read Email from Hotmail Account in Delphi - Tutorial

Read Email over SSL/TLS Connection in VB6 - Tutorial
Read Email from Gmail Account in VB6 - Tutorial
Read Email from Yahoo Account in VB6 - Tutorial
Read Email from Hotmail Account in VB6 - Tutorial

Read Email over SSL/TLS Connection VC++ - Tutorial
Read Email from Gmail Account in VC++ - Tutorial
Read Email from Yahoo Account in VC++ - Tutorial
Read Email from Hotmail Account in VC++ - Tutorial