Search email on IMAP4 Server and MS Exchange Server. It doesn't support POP3 protocol.
[Visual Basic 6.0] Public Property Get GetMailInfosParam() As GetMailInfosParamType
[Visual C++] public: get_GetMailInfosParam(IGetMailInfosParamType** pVal);
Property Value
Remarks
By default, GetMailInfoList method returns all emails in "INBOX". You can use SelectFolder method to retrieve email list in selected folder.
GetMailInfosParam property can set some filters for returned email list on server.
For example, you can use GetMailInfosParam property to retrieve only unread/new email(s) from server, then use MarkAsRead method to mark the email as read to prevent retrieving the same email again.
GetMailInfosParamType Members
| Members name | Description |
| GetMailInfosOptions | Specify the filter flags, it can be combined by any GetMailInfosOptionType value. |
| CategoryFilter |
Search emails that have specific categories, only Graph API supports this feature.
|
| FollowUpFlag | Search emails that have specific follow-up flag, only Graph API supports this feature. Use one of "notFlagged", "complete", "flagged". |
| Reset | This method resets GetMailInfosParam to default value. |
| SeqRange | Search emails in specified IMAP4 sequence range. |
| UIDRange | Search emails in specified IMAP4 UID range. |
| DateRangeSINCE/DateRangeBEFORE | Search emails in specified date time range. |
| SenderContains | Search emails that sender contains specified value. |
| ToOrCcContains | Search emails that To or Cc contains specified value. |
| SubjectContains | Search emails that subject contains specified value. |
| BodyContains | Search emails that body contains specified value. |
| HeaderContains | Search emails that headers contain specified header, the syntax is: header: value; For example, "Message-ID: <mymessageid>" can search email by message-id. |
GetMailInfosOptionType Members
| Members name | Description |
| GetMailInfos_All | Search all emails. |
| GetMailInfos_NewOnly | Search emails with unread flags. |
| GetMailInfos_ReadOnly | Search emails with read flags. |
| GetMailInfos_SeqRange | Search emails in specified IMAP4 sequence range. |
| GetMailInfos_UIDRange | Search emails in specified IMAP4 UID range. |
| GetMailInfos_PR_ENTRYID | Reserved. |
| GetMailInfos_DateRange | Search emails in specified date time range. |
| GetMailInfos_OrderByDateTime | Sort emails by date time ascending. |
| GetMailInfos_GetCategories | Retrieve categories to MailInfo.Categories property, only Graph API and EWS support this feature. By default, GetMailInfos method doesn't retrieve categories to MailInfo.Categories property. |
| GetMailInfos_GetFollowUpFlag | Retrieve follow-up flag to MailInfo.FollowUpFlag property, only Graph API and EWS support this feature. By default, GetMailInfos method doesn't retrieve follow-up flag to MailInfo.FollowUpFlag property. |
| GetMailInfos_IncludeAllFolders | Search emails in all folders instead in current folder, only Graph API supports this feature. |
Example
[Visual Basic 6.0, VBScript, Visual C++, Delphi] The following example demonstrates how to search emails with conditions. 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
Const GetMailInfos_All = 1
Const GetMailInfos_NewOnly = 2
Const GetMailInfos_ReadOnly = 4
Const GetMailInfos_SeqRange = 8
Const GetMailInfos_UIDRange = 16
Const GetMailInfos_PR_ENTRYID = 32
Const GetMailInfos_DateRange = 64
Const GetMailInfos_OrderByDateTime = 128
'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 with Exchange Web Service, please change
'MailServerImap4 to MailServerEWS to MailServer.Protocol
'To receive email with Exchange WebDAV, please change
'MailServerImap4 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 = MailServerImap4
''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
' get emails that have:
' unread/new flag and received datetime is from now to 2013-10-1
' and sender contains "support" and subject contains "test"
Dim options
options = GetMailInfos_NewOnly
options = options Or GetMailInfos_DateRange
options = options Or GetMailInfos_OrderByDateTime
oClient.GetMailInfosParam.Reset
oClient.GetMailInfosParam.GetMailInfosOptions = options
oClient.GetMailInfosParam.SubjectContains = "test"
oClient.GetMailInfosParam.SenderContains = "support"
oClient.GetMailInfosParam.DateRangeSINCE = "2013-10-1"
oClient.GetMailInfosParam.DateRangeBEFORE = Now()
'More Examples:
'Get only new email:
'Dim options
'options = GetMailInfos_NewOnly
'oClient.GetMailInfosParam.GetMailInfosOptions = options
'Get only new email that subject contains "test":
'Dim options
'options = GetMailInfos_NewOnly
'oClient.GetMailInfosParam.GetMailInfosOptions = options
'oClient.GetMailInfosParam.SubjectContains = "test"
'Get emails by IMAP4 sequence range:
'Dim options
'options= GetMailInfos_SeqRange
'oClient.GetMailInfosParam.GetMailInfosOptions = options
'oClient.GetMailInfosParam.SeqRange = "1:10"
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)
MsgBox "UIDL: " & info.UIDL
MsgBox "Index: " & info.Index
MsgBox "Size: " & info.Size
'For POP3/Exchange Web Service/WebDAV, the IMAP4MailFlags is meaningless.
MsgBox "Flags: " & info.IMAP4Flags
'For POP3, the Read is meaningless.
MsgBox "Read: " & info.Read
MsgBox "Deleted: " & info.Deleted
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
Const GetMailInfos_All = 1
Const GetMailInfos_NewOnly = 2
Const GetMailInfos_ReadOnly = 4
Const GetMailInfos_SeqRange = 8
Const GetMailInfos_UIDRange = 16
Const GetMailInfos_PR_ENTRYID = 32
Const GetMailInfos_DateRange = 64
Const GetMailInfos_OrderByDateTime = 128
'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 with Exchange Web Service, please change
'MailServerImap4 to MailServerEWS to MailServer.Protocol
'To receive email with Exchange WebDAV, please change
'MailServerImap4 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 = MailServerImap4
''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
' get emails that have:
' unread/new flag and received datetime is from now to 2013-10-1
' and sender contains "support" and subject contains "test"
Dim options
options = GetMailInfos_NewOnly
options = options Or GetMailInfos_DateRange
options = options Or GetMailInfos_OrderByDateTime
oClient.GetMailInfosParam.Reset
oClient.GetMailInfosParam.GetMailInfosOptions = options
oClient.GetMailInfosParam.SubjectContains = "test"
oClient.GetMailInfosParam.SenderContains = "support"
oClient.GetMailInfosParam.DateRangeSINCE = "2013-10-1"
oClient.GetMailInfosParam.DateRangeBEFORE = Now()
'More Examples:
'Get only new email:
'Dim options
'options = GetMailInfos_NewOnly
'oClient.GetMailInfosParam.GetMailInfosOptions = options
'Get only new email that subject contains "test":
'Dim options
'options = GetMailInfos_NewOnly
'oClient.GetMailInfosParam.GetMailInfosOptions = options
'oClient.GetMailInfosParam.SubjectContains = "test"
'Get emails by IMAP4 sequence range:
'Dim options
'options= GetMailInfos_SeqRange
'oClient.GetMailInfosParam.GetMailInfosOptions = options
'oClient.GetMailInfosParam.SeqRange = "1:10"
Dim infos
Set infos = oClient.GetMailInfoList()
Dim i
For i = 0 To infos.Count - 1
Dim info
Set info = infos.Item(i)
MsgBox "UIDL: " & info.UIDL
MsgBox "Index: " & info.Index
MsgBox "Size: " & info.Size
'For POP3/Exchange Web Service/WebDAV, the IMAP4MailFlags is meaningless.
MsgBox "Flags: " & info.IMAP4Flags
'For POP3, the Read is meaningless.
MsgBox "Read: " & info.Read
MsgBox "Deleted: " & info.Deleted
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;
const int GetMailInfos_All = 1;
const int GetMailInfos_NewOnly = 2;
const int GetMailInfos_ReadOnly = 4;
const int GetMailInfos_SeqRange = 8;
const int GetMailInfos_UIDRange = 16;
const int GetMailInfos_PR_ENTRYID = 32;
const int GetMailInfos_DateRange = 64;
const int GetMailInfos_OrderByDateTime = 128;
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 = MailServerImap4;
//To receive email with Exchange Web Service, please change
//MailServerImap4 to MailServerEWS to MailServer.Protocol
//To receive email with Exchange WebDAV, please change
//MailServerImap4 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);
// get emails that have:
// unread/new flag and received datetime is from now to latest one year (12 months)
// and sender contains "support" and subject contains "test"
int options = GetMailInfos_NewOnly;
options = (options | GetMailInfos_DateRange);
options = (options | GetMailInfos_OrderByDateTime);
oClient->GetMailInfosParam->Reset();
oClient->GetMailInfosParam->GetMailInfosOptions = options;
oClient->GetMailInfosParam->SubjectContains = _T("test");
oClient->GetMailInfosParam->SenderContains = _T("support");
SYSTEMTIME tcur;
::GetSystemTime(&tcur);
DATE DateRangeBEFORE = 0;
::SystemTimeToVariantTime(&tcur, &DateRangeBEFORE);
DATE DateRangeSINCE = 0;
tcur.wYear -= 1;
::SystemTimeToVariantTime(&tcur, &DateRangeSINCE);
oClient->GetMailInfosParam->DateRangeSINCE = DateRangeSINCE;
oClient->GetMailInfosParam->DateRangeBEFORE = DateRangeBEFORE;
//More Examples:
//Get only new email:
//oClient->GetMailInfosParam->GetMailInfosOptions = GetMailInfos_NewOnly;
//Get only new email that subject contains "test":
//oClient->GetMailInfosParam->GetMailInfosOptions = GetMailInfos_NewOnly;
//oClient->GetMailInfosParam->SubjectContains = _T("test");
//Get emails by IMAP4 sequence range:
//oClient->GetMailInfosParam->GetMailInfosOptions = GetMailInfos_SeqRange;
//oClient->GetMailInfosParam->SeqRange = _T("1:10");
IMailInfoCollectionPtr infos = oClient->GetMailInfoList();
for(long i = 0; i < infos->Count; i++)
{
IMailInfoPtr pInfo = infos->GetItem(i);
_tprintf(_T("UIDL: %s\r\n"), (TCHAR*)pInfo->UIDL);
_tprintf(_T("Index: %d\r\n"), pInfo->Index);
_tprintf(_T("Size: %d\r\n"), pInfo->Size);
//For POP3/Exchange Web Service/WebDAV, the IMAP4MailFlags is meaningless
_tprintf(_T("Flags: %s\r\n"), (TCHAR*)pInfo->IMAP4Flags);
//For POP3, the Read is meaningless
_tprintf(_T("Read: %s\r\n"), (pInfo->Read == VARIANT_TRUE)?_T("TRUE"):_T("FALSE"));
_tprintf(_T("Deleted: %s\r\n"), (pInfo->Deleted == VARIANT_TRUE)?_T("TRUE"):_T("FALSE"));
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)
Button1: TButton;
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;
// GetMailInfosParam Flags
GetMailInfos_All = 1;
GetMailInfos_NewOnly = 2;
GetMailInfos_ReadOnly = 4;
GetMailInfos_SeqRange = 8;
GetMailInfos_UIDRange = 16;
GetMailInfos_PR_ENTRYID = 32;
GetMailInfos_DateRange = 64;
GetMailInfos_OrderByDateTime = 128;
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 := MailServerImap4;
// Enable SSL/TLS Connection, most modern email server require SSL/TLS connection by default.
oServer.SSLConnection := true;
// Set 993 SSL IMAP4 port
oServer.Port := 993;
// If your IMAP doesn't deploy SSL connection
// Please use
// oServer.SSLConnection := false;
// oServer.Port := 143;
oClient := TMailClient.Create(Application);
oClient.LicenseCode := 'TryIt';
oClient.Connect1(oServer.DefaultInterface);
ShowMessage('Connected!');
// retrieve unread/new email only
oClient.GetMailInfosParam.Reset();
oClient.GetMailInfosParam.GetMailInfosOptions := GetMailInfos_NewOnly;
infos := oClient.GetMailInfoList();
ShowMessage(Format('Total %d unread 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 IMAP 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 unread email as read, next time this email won't be retrieved again
if not oInfo.Read then
oClient.MarkAsRead(oInfo, true);
// if you don't want to leave a copy on server, please use
// oClient.Delete(oInfo);
// instead of MarkAsRead
end;
// Quit and expunge emails marked as deleted from IMAP server
oClient.Quit;
except
on ep:Exception do
ShowMessage('Error: ' + ep.Message);
end;
end;
end.
See Also
MailClient.GetMailInfos Method
User Authentication and SSL/TLS Connection
MailClient.Connect Method
MailClient.SelectFolder 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