MailClient.WaitNewEmail Method


Get notification of new email on IMAP4 server and MS Exchange server(WebDAV, EWS).

[Visual Basic 6.0]
Public Function WaitNewEmail( _
    Timeout As Long
) As Boolean
[Visual C++]
public: HRESULT WaitNewEmail(
    LONG Timeout,
    VARIANT_BOOL* pVal
);

Parameters

Timeout
The number of milliseconds to wait, or -1 to wait indefinitely. If Timeout is zero, the method does not block. It tests the state of the new email and returns immediately.

Return Value

True means there is new email arrived; false means time out waiting for new email.

Remarks

This method only supports IMAP4/EWS/WebDAV protocols, it doesn't support POP3 protocol.

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

    '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
    
    If oServer.SSLConnection Then
        oServer.Port = 993 'SSL IMAP4
    Else
        oServer.Port = 143 'IMAP4 normal
    End If
    
    
    On Error GoTo ErrorHandle
        oClient.Connect oServer

        ' Retrieve current emails at first
        RefreshMail oClient

        ' Wait for new email notification for 10 seconds
        If oClient.WaitNewEmail(10000) Then
            ' New email arrived, Retrieve emails again
            RefreshMail oClient
        Else
            'No new email, cancel waiting
            oClient.CancelWaitEmail
        End If

        oClient.Logout
        Exit Sub
ErrorHandle:
        ''Error handle
        MsgBox Err.Description
        
        oClient.Close
End Sub

Sub RefreshMail(ByRef oClient)
    oClient.RefreshMailInfos
    
    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

    ' Expunge deleted email from server
    oClient.Expunge
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 '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 If oServer.SSLConnection Then oServer.Port = 993 'SSL IMAP4 Else oServer.Port = 143 'IMAP4 normal End If oClient.Connect oServer ' Retrieve current emails at first RefreshMail oClient ' Wait for new email notification for 10 seconds If oClient.WaitNewEmail(10000) Then ' New email arrived, Retrieve emails again RefreshMail oClient Else 'No new email, cancel waiting oClient.CancelWaitEmail End If oClient.Logout End Sub Sub RefreshMail(oClient) oClient.RefreshMailInfos 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 ' Expunge deleted email from server oClient.Expunge End Sub
[Visual C++] #include "stdafx.h" #include <windows.h> #include "eagetmailobj.tlh" using namespace EAGetMailObjLib; void RefreshMail(IMailClientPtr &oClient) { oClient->RefreshMailInfos(); 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); } // Expunge deleted email from server. oClient->Expunge(); } 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; 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(bSSLConnection) { oServer->Port = 993; oServer->SSLConnection = VARIANT_TRUE; } else { oServer->Port = 143; } oClient->Connect(oServer); // Retrieve current emails at first RefreshMail(oClient); // Wait for new email notification for 10 seconds if(oClient.WaitNewEmail(10000)) { // New email arrived, Retrieve emails again RefreshMail(oClient); } else { // No new email, cancel waiting oClient->CancelWaitEmail(); } oClient->Logout(); } catch(_com_error &ep) { _tprintf(_T("ERROR: %s\r\n"), (TCHAR*)ep.Description()); } ::CoUninitialize(); }

See Also

MailClient.CancelWaitEmail 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