Search emails in IMAP4 mail server.
[Visual Basic] Public Function SearchMail( condition As String _ ) As MailInfo() Public Async Function SearchMailAsync( condition As String _ ) As Task (Of MailInfo())
[C#] public MailInfo[] SearchMail( string condition ); public async Task<MailInfo[]> SearchMailAsync( string condition );
[C++] public: array<MailInfo^> SearchMail( String* condition );
[JScript] public function SearchMail( condition : String ) : MailInfo [];
Parameters
Return Value
Remarks
The SearchMail method searches the mailbox for messages that match the given searching criteria. Searching criteria consist of one or more search keys.
The defined search keys are as follows. Refer to the Formal Syntax section for the precise syntactic definitions of the arguments. <message set> Messages with message sequence numbers corresponding to the specified message sequence number set ALL All messages in the mailbox; the default initial key for ANDing. ANSWERED Messages with the \Answered flag set. BCC <string> Messages that contain the specified string in the envelope structure's BCC field. BEFORE <date> Messages whose internal date is earlier than the specified date. BODY <string> Messages that contain the specified string in the body of the message. CC <string> Messages that contain the specified string in the envelope structure's CC field. DELETED Messages with the \Deleted flag set. DRAFT Messages with the \Draft flag set. FLAGGED Messages with the \Flagged flag set. FROM <string> Messages that contain the specified string in the envelope structure's FROM field. HEADER <field-name> <string> Messages that have a header with the specified field-name (as defined in [RFC-822]) and that contains the specified string in the [RFC-822] field-body. KEYWORD <flag> Messages with the specified keyword set. LARGER <n> Messages with an [RFC-822] size larger than the specified number of octets. NEW Messages that have the \Recent flag set but not the \Seen flag. This is functionally equivalent to "(RECENT UNSEEN)". NOT <search-key> Messages that do not match the specified search key. OLD Messages that do not have the \Recent flag set. This is functionally equivalent to "NOT RECENT" (as opposed to "NOT NEW"). ON <date> Messages whose internal date is within the specified date. OR <search-key1> <search-key2> Messages that match either search key. RECENT Messages that have the \Recent flag set. SEEN Messages that have the \Seen flag set. SENTBEFORE <date> Messages whose [RFC-822] Date: header is earlier than the specified date. SENTON <date> Messages whose [RFC-822] Date: header is within the specified date. SENTSINCE <date> Messages whose [RFC-822] Date: header is within or later than the specified date. SINCE <date> Messages whose internal date is within or later than the specified date. SMALLER <n> Messages with an [RFC-822] size smaller than the specified number of octets. SUBJECT <string> Messages that contain the specified string in the envelope structure's SUBJECT field. TEXT <string> Messages that contain the specified string in the header or body of the message. TO <string> Messages that contain the specified string in the envelope structure's TO field. UID <message set> Messages with unique identifiers corresponding to the specified unique identifier set. UNANSWERED Messages that do not have the \Answered flag set. UNDELETED Messages that do not have the \Deleted flag set. UNDRAFT Messages that do not have the \Draft flag set. UNFLAGGED Messages that do not have the \Flagged flag set. UNKEYWORD <flag> Messages that do not have the specified keyword set. UNSEEN Messages that do not have the \Seen flag set.
Example
[Visual Basic, C#, C++] The following example demonstrates how to receive email with EAGetMail POP3 & IMAP Component, but it doesn't demonstrates the events and mail parsing usage. To get the full samples of EAGetMail, please refer to Samples section.
[VB - Search emails on IMAP4 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 SearchMail(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 ' 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. oServer.Port = If(useSsl, 993, 143) Console.WriteLine("Connecting server ...") Dim oClient As New MailClient("TryIt") oClient.Connect(oServer) ' Search the emails whose internal date is within 8-AUG-2008 and sender contains "John" Dim infos() As MailInfo = oClient.SearchMail( "ALL ON 8-AUG-2008 FROM ""John""" ) Console.WriteLine("Total {0} email(s) found", 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 on IMAP4 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 SearchMail(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); } // 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. oServer.Port = (useSsl) ? 993 : 143; Console.WriteLine("Connecting server ..."); MailClient oClient = new MailClient("TryIt"); oClient.Connect(oServer); Console.WriteLine("Searching email list ..."); // Search the emails whose has no SEEN flag and sender contains "John" MailInfo [] infos = oClient.SearchMail( "ALL NEW FROM \"John\"" ); Console.WriteLine("Total {0} email(s) found", 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 on IMAP4 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 SearchMail(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); } // 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. oServer->Port = (useSsl) ? 993 : 143; Console::WriteLine("Connecting server ..."); MailClient ^oClient = gcnew MailClient("TryIt"); oClient->Connect(oServer); // Search the emails whose has no SEEN flag and subject contains "test" array<MailInfo^> ^infos = oClient->SearchMail( "ALL NEW SUBJECT \"test\"" ); Console::WriteLine("Total {0} email(s) found", 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