Search email on IMAP4 Server and MS Exchange Server. It doesn't support POP3 protocol.
[Visual Basic] Public Property GetMailInfosParam As GetMailInfosParamType
[C#] public GetMailInfosParamType GetMailInfosParam {get;}
[C++] public: __property GetMailInfosParamType get_GetMailInfosParam();
[JScript] public function get GetMailInfosParam() : GetMailInfosParamType;
Property Value
Remarks
By default, MailClient.GetMailInfos method returns all emails in current selected folder. However, you can use GetMailInfosParam property to set search filters/parameters.
You can use this property to retrieve only unread/new email(s) from server, then use MailClient.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. |
DateRange | 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 |
GetMailInfosOptionType.All | Search all emails. |
GetMailInfosOptionType.NewOnly | Search emails with unread flags. |
GetMailInfosOptionType.ReadOnly | Search emails with read flags. |
GetMailInfosOptionType.SeqRange | Search emails in specified IMAP4 sequence range. |
GetMailInfosOptionType.UIDRange | Search emails in specified IMAP4 UID range. |
GetMailInfosOptionType.PR_ENTRYID | Reserved. |
GetMailInfosOptionType.DateRange | Search emails in specified date time range. |
GetMailInfosOptionType.OrderByDateTime | Sort emails by date time ascending. |
GetMailInfosOptionType.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. |
GetMailInfosOptionType.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. |
GetMailInfosOptionType.IncludeAllFolders | Search emails in all folders instead in current folder, only Graph API supports this feature. |
Example
[Visual Basic, C#, C++] The following example demonstrates how to search emails with conditions. To get the full samples of EAGetMail, please refer to Samples section.
[VB - Search emails in IMAP4 Server or MS Exchange Server] Imports System Imports System.Globalization Imports System.IO Imports System.Text Imports EAGetMail Public Class TestClass ' Generate an unqiue email file name based on date time. Shared Function _generateFileName(ByVal sequence As Integer) As String Dim currentDateTime As DateTime = DateTime.Now Return String.Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", New CultureInfo("en-US")), currentDateTime.Millisecond, sequence) End Function Public Sub ReceiveMail(server As String, user As String, password As String, useSsl As Boolean) Try ' Create a folder named "inbox" under current directory ' to save the email retrieved. Dim localInbox As String = String.Format("{0}\inbox", Directory.GetCurrentDirectory()) ' If the folder is not existed, create it. If Not Directory.Exists(localInbox) Then Directory.CreateDirectory(localInbox) End If ' ExchangeEWS Or ExchangeWebDAV protocol also supports GetMailInfosParam. ' Most modern email server require SSL/TLS connection, ' set useSsl to true Is recommended. Dim oServer As New MailServer(server, user, password, useSsl, ServerAuthType.AuthLogin, ServerProtocol.Imap4) ' IMAP4 port Is 143, IMAP4 SSL port Is 993. ' EWS/WebDAV, please ignore Port property. oServer.Port = If(useSsl, 993, 143) Console.WriteLine("Connecting server ...") Dim oClient As New MailClient("TryIt") oClient.Connect(oServer) ' search emails that have: ' unread/new flag and received datetime is from now to latest 12 months ' and sender contains "support" and subject contains "test" Dim options As GetMailInfosOptionType = GetMailInfosOptionType.NewOnly options = options Or GetMailInfosOptionType.DateRange options = options Or GetMailInfosOptionType.OrderByDateTime oClient.GetMailInfosParam.Reset() oClient.GetMailInfosParam.GetMailInfosOptions = options oClient.GetMailInfosParam.SubjectContains = "test" oClient.GetMailInfosParam.SenderContains = "support" oClient.GetMailInfosParam.DateRange.SINCE = System.DateTime.Now.AddMonths(-12) oClient.GetMailInfosParam.DateRange.BEFORE = System.DateTime.Now.AddDays(1) ' More Examples: ' Get only new email: ' Dim options As GetMailInfosOptionType = GetMailInfosOptionType.NewOnly ' oClient.GetMailInfosParam.GetMailInfosOptions = options ' Get only new email that subject contains "test": ' Dim options As GetMailInfosOptionType = GetMailInfosOptionType.NewOnly ' oClient.GetMailInfosParam.GetMailInfosOptions = options ' oClient.GetMailInfosParam.SubjectContains = "test" ' Get emails by IMAP4 sequence range: ' Dim options As GetMailInfosOptionType = GetMailInfosOptionType.SeqRange ' oClient.GetMailInfosParam.GetMailInfosOptions = options ' oClient.GetMailInfosParam.SeqRange = "1:10" Console.WriteLine("Searching email list ...") Dim infos() As MailInfo = oClient.GetMailInfos() Console.WriteLine("Total {0} email(s)", infos.Length) For i As Integer = 0 To infos.Length - 1 Console.WriteLine("Checking {0}/{1} ...", i + 1, infos.Length) Dim info As MailInfo = infos(i) ' Generate an unqiue email file name based on date time. Dim fileName As String = _generateFileName(i + 1) Dim fullPath As String = String.Format("{0}\{1}", localInbox, fileName) Console.WriteLine("Downloading {0}/{1} ...", i + 1, infos.Length) Dim oMail As Mail = oClient.GetMail(info) ' Save email to local disk oMail.SaveAs(fullPath, True) ' Mark email as deleted on server. Console.WriteLine("Deleting ... {0}/{1}", i + 1, infos.Length) oClient.Delete(info) Next Console.WriteLine("Disconnecting ...") ' Delete method just mark the email as deleted, ' Quit method expunge the emails from server permanently. oClient.Quit() Console.WriteLine("Completed!") Catch ep As Exception Console.WriteLine("Error: {0}", ep.Message) End Try End Sub End Class
[C# - Search emails in IMAP4 Server or MS Exchange Server] using System; using System.IO; using System.Globalization; using System.Text; using EAGetMail; class TestClass { // Generate an unqiue email file name based on date time static string _generateFileName(int sequence) { DateTime currentDateTime = DateTime.Now; return string.Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", new CultureInfo("en-US")), currentDateTime.Millisecond, sequence); } public void ReceiveMail(string server, string user, string password, bool useSsl) { try { // Create a folder named "inbox" under current directory // to save the email retrieved. string localInbox = string.Format("{0}\\inbox", Directory.GetCurrentDirectory()); // If the folder is not existed, create it. if (!Directory.Exists(localInbox)) { Directory.CreateDirectory(localInbox); } // ExchangeEWS Or ExchangeWebDAV protocol also supports GetMailInfosParam. // Most modern email server require SSL/TLS connection, // set useSsl to true is recommended. MailServer oServer = new MailServer(server, user, password, useSsl, ServerAuthType.AuthLogin, ServerProtocol.Imap4); // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. oServer.Port = (useSsl) ? 993 : 143; Console.WriteLine("Connecting server ..."); MailClient oClient = new MailClient("TryIt"); oClient.Connect(oServer); // search emails that have: // unread/new flag and received datetime is from now to latest 12 months // and sender contains "support" and subject contains "test" oClient.GetMailInfosParam.Reset(); oClient.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.NewOnly; oClient.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.DateRange; oClient.GetMailInfosParam.GetMailInfosOptions |= GetMailInfosOptionType.OrderByDateTime; oClient.GetMailInfosParam.SubjectContains = "test"; oClient.GetMailInfosParam.SenderContains = "support"; oClient.GetMailInfosParam.DateRange.SINCE = System.DateTime.Now.AddMonths(-12); oClient.GetMailInfosParam.DateRange.BEFORE = System.DateTime.Now.AddDays(1); // More Examples: // Get only new email: // oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly; // Get only new email that subject contains "test": // oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.NewOnly; // oClient.GetMailInfosParam.SubjectContains = "test"; // Get emails by IMAP4 sequence range: // oClient.GetMailInfosParam.GetMailInfosOptions = GetMailInfosOptionType.SeqRange; // oClient.GetMailInfosParam.SeqRange = "1:10"; Console.WriteLine("Searching email list ..."); MailInfo[] infos = oClient.GetMailInfos(); Console.WriteLine("Total {0} email(s)", infos.Length); for (int i = 0; i < infos.Length; i++) { Console.WriteLine("Checking {0}/{1} ...", i+1, infos.Length); MailInfo info = infos[i]; // Generate an unqiue email file name based on date time. string fileName = _generateFileName(i + 1); string fullPath = string.Format("{0}\\{1}", localInbox, fileName); Console.WriteLine("Downloading {0}/{1} ...", i + 1, infos.Length); Mail oMail = oClient.GetMail(info); // Save mail to local file oMail.SaveAs(fullPath, true); // Mark the email as deleted on server. Console.WriteLine("Deleting ... {0}/{1}", i + 1, infos.Length); oClient.Delete(info); } Console.WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient.Quit(); Console.WriteLine("Completed!"); } catch (Exception ep) { Console.WriteLine("Error: {0}", ep.Message); } } }
[C++/CLI - Search emails in IMAP4 Server or MS Exchange Server] using namespace System; using namespace System::Globalization; using namespace System::IO; using namespace EAGetMail; //add EAGetMail namespace // Generate an unqiue email file name based on date time static String ^ _generateFileName(int sequence) { DateTime currentDateTime = DateTime::Now; return String::Format("{0}-{1:000}-{2:000}.eml", currentDateTime.ToString("yyyyMMddHHmmss", gcnew CultureInfo("en-US")), currentDateTime.Millisecond, sequence); } void ReceiveMail(String ^server, String ^user, String ^password, bool useSsl) { try { // Create a folder named "inbox" under current directory // to save the email retrieved. String ^localInbox = String::Format("{0}\\inbox", Directory::GetCurrentDirectory()); // If the folder is not existed, create it. if (!Directory::Exists(localInbox)) { Directory::CreateDirectory(localInbox); } // ExchangeEWS Or ExchangeWebDAV protocol also supports GetMailInfosParam. // Most modern email server require SSL/TLS connection, // set useSsl to true is recommended. MailServer ^oServer = gcnew MailServer(server, user, password, useSsl, ServerAuthType::AuthLogin, ServerProtocol::Imap4); // IMAP4 port is 143, IMAP4 SSL port is 993. // EWS/WebDAV, please ignore Port property. oServer->Port = (useSsl) ? 993 : 143; Console::WriteLine("Connecting server ..."); MailClient ^oClient = gcnew MailClient("TryIt"); oClient->Connect(oServer); // search emails that have: // unread/new flag and received datetime is from now to latest 12 months // and sender contains "support" and subject contains "test" oClient->GetMailInfosParam->Reset(); oClient->GetMailInfosParam->GetMailInfosOptions |= GetMailInfosOptionType::NewOnly; oClient->GetMailInfosParam->GetMailInfosOptions |= GetMailInfosOptionType::DateRange; oClient->GetMailInfosParam->GetMailInfosOptions |= GetMailInfosOptionType::OrderByDateTime; oClient->GetMailInfosParam->SubjectContains = "test"; oClient->GetMailInfosParam->SenderContains = "support"; oClient->GetMailInfosParam->DateRange->SINCE = System::DateTime::Now.AddMonths(-12); oClient->GetMailInfosParam->DateRange->BEFORE = System::DateTime::Now.AddDays(1); // More Examples: // Get only new email: // oClient->GetMailInfosParam->GetMailInfosOptions = GetMailInfosOptionType::NewOnly; // Get only new email that subject contains "test": // oClient->GetMailInfosParam->GetMailInfosOptions = GetMailInfosOptionType::NewOnly; // oClient->GetMailInfosParam->SubjectContains = "test"; // Get emails by IMAP4 sequence range: // oClient->GetMailInfosParam->GetMailInfosOptions = GetMailInfosOptionType::SeqRange; // oClient->GetMailInfosParam.SeqRange = "1:10"; Console::WriteLine("Searching email list ..."); array<MailInfo^>^infos = oClient->GetMailInfos(); Console::WriteLine("Total {0} email(s)", infos->Length); for (int i = 0; i < infos->Length; i++) { Console::WriteLine("Checking {0}/{1}", i + 1, infos->Length); MailInfo ^info = infos[i]; // Generate an unqiue email file name based on date time String^ fileName = _generateFileName(i + 1); String^ fullPath = String::Format("{0}\\{1}", localInbox, fileName); Console::WriteLine("Downloading {0}/{1}", i + 1, infos->Length); Mail ^oMail = oClient->GetMail(info); // Save email to local disk oMail->SaveAs(fullPath, true); // Mark email as deleted on server. Console::WriteLine("Deleting ... {0}/{1}", i + 1, infos->Length); oClient->Delete(info); } Console::WriteLine("Disconnecting ..."); // Delete method just mark the email as deleted, // Quit method expunge the emails from server permanently. oClient->Quit(); Console::WriteLine("Completed!"); } catch (Exception ^ep) { Console::WriteLine("Error: {0}", ep->Message); } }
See Also
MailClient.MarkAsRead Method
MailClient.GetMailInfos Method
User Authentication and SSL/TLS Connection
MailInfo Class
Mail Class
Online Tutorials
Read Email and Parse Email in C# - Tutorial
Read Email and Parse Email in VB.NET - Tutorial
Read Email and Parse Email C++/CLI - Tutorial
Read Email over SSL/TLS Connection in C# - Tutorial
Read Email from Gmail Account in C# - Tutorial
Read Email from Yahoo Account in C# - Tutorial
Read Email from Hotmail Account in C# - Tutorial
Read Email over SSL/TLS Connection in VB.NET - Tutorial
Read Email from Gmail Account in VB.NET - Tutorial
Read Email from Yahoo Account in VB.NET - Tutorial
Read Email from Hotmail Account in VB.NET - Tutorial
Read Email over SSL/TLS Connection C++/CLI - Tutorial
Read Email from Gmail Account in C++/CLI - Tutorial
Read Email from Yahoo Account in C++/CLI - Tutorial
Read Email from Hotmail Account in C++/CLI - Tutorial