Search emails in IMAP4 mail server.
[Visual Basic 6.0] Public Function SearchMailList( condition As String _ ) As MailInfoCollection
[Visual C++] public: HRESULT SearchMailList( BSTR condition, IMailInfoCollection* pVal );
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.
You can also use MailClient.GetMailInfosParam Property to set filters such as "retrieve new emails only" instead of SearchMailList method.
Example
[Visual Basic 6.0, VBScript, Visual C++] The following example demonstrates how to search 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 = MailServerImap4 If oServer.SSLConnection Then oServer.Port = 993 'SSL IMAP4 Else oServer.Port = 143 'IMAP4 normal End If On Error GoTo ErrorHandle oClient.Connect oServer Dim infos Set infos = oClient.SearchMail("ALL ON 8-AUG-2008 FROM ""John""") 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 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 ' 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 '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 = MailServerImap4 If oServer.SSLConnection Then oServer.Port = 993 'SSL IMAP4 Else oServer.Port = 143 'IMAP4 normal End If oClient.Connect oServer Dim infos Set infos = oClient.SearchMail("ALL NEW FROM ""John""") 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 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 ' 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; 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; if(bSSLConnection) { oServer->Port = 993; oServer->SSLConnection = VARIANT_TRUE; } else { oServer->Port = 143; } oClient->Connect(oServer); IMailInfoCollectionPtr infos = oClient->SearchMail("ALL NEW FROM \"John\""); 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 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); // 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(); }
See Also