Work with EAGetMail Service


EAGetMail Service is an advanced service which can download emails from POP3/IMAP4/Exchange servers in background. It provides many features such as retrieving and saving emails to specified folders in schedule, and starting a specified application to handle downloaded emails.

This service facilitates EAGetMail POP3/IMAP4 Component developers that their applications can now accept an email retrieving request from EAGetMail Application, and then download emails in background. This is particularly useful for ASP/ASP.NET web application.

Retrieve emails in background

In an ASP/ASP.NET email application, if email download takes longer than the timeout value (in seconds) that the current asp page is allowed to run, user will get an error "ASP Timeout". This often happens when user has a large quantity of emails to download or user downloads emails via a slow connection. By default the timeout is set to 90 seconds, it is easy to exceed this limit.

Obviously, a solution to ASP timeout is to set ASPScriptTimeout a larger value. You may click here for details. Technically the timeout problem can be solved in this way; however, some users may get frustrated and press the stop button on the browser toolbar as he waits too long.

EAGetMail Component introduces a more intelligent method to retrieve emails in background. You should download the EAGetMail Service installer and install it on your machine at first. Then you can use MailClient.GetMailsByQueue method to send the request to EAGetMail Service, the method returns to the user immediately and the EAGetMail Service performs task in background.

Example

[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 from imap4 server, please change
    'MailServerPop3 to MailServerImap4 to MailServer.Protocol

    'To receive email with Exchange Web Service, please change
    'MailServerPop3 to MailServerEWS to MailServer.Protocol

    'To receive email with Exchange WebDAV, please change
    'MailServerPop3 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 = MailServerPop3
    
    ''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
        Dim leaveCopy As Boolean
        leaveCopy =  True 'leave a copy of message on server.
        Dim downloadFolder As String
        downloadFolder = "c:\popdownload" 'download emails to this local
            folder. 'send request to EAGetMail Service, then EAGetMail Service retrieves email
            in background and 'this method returns immediately.
        oClient.GetMailsByQueue oServer, downloadFolder, leaveCopy
        
        Exit Sub
ErrorHandle:
        ''Error handle
        MsgBox Err.Description
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 from imap4 server, please change 'MailServerPop3 to MailServerImap4 to MailServer.Protocol 'To receive email with Exchange Web Service, please change 'MailServerPop3 to MailServerEWS to MailServer.Protocol 'To receive email with Exchange WebDAV, please change 'MailServerPop3 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 = MailServerPop3 '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 Dim leaveCopy leaveCopy = True 'leave a copy of message on server. Dim downloadFolder downloadFolder = "c:\popdownload" 'download emails to this local folder. 'send request to EAGetMail Service, then EAGetMail Service retrieves email in background and 'this method returns immediately. oClient.GetMailsByQueue oServer, downloadFolder, leaveCopy 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; 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 = MailServerPop3; //To receive email from imap4 server, please change //MailServerPop3 to MailServerImap4 in MailServer constructor //To receive email with Exchange Web Service, please change //MailServerPop3 to MailServerEWS to MailServer.Protocol //To receive email with Exchange WebDAV, please change //MailServerPop3 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; } } VARIANT_BOOL leaveCopy = VARIANT_TRUE; //leave a copy of message on server. LPCTSTR downloadFolder = _T("c:\\popdownload"");//download emails to this local folder //send request to EAGetMail Service, then EAGetMail Service retrieves email in background and //this method returns immediately. oClient->GetMailsByQueue( oServer, downloadFolder, leaveCopy ); } catch( _com_error &ep ) { _tprintf( _T("ERROR: %s\r\n"), (TCHAR*)ep.Description()); } ::CoUninitialize(); }
[Delphi] procedure ReceiveMail(); var oServer: TMailServer; oClient: TMailClient; oTools: TTools; localInbox: WideString; leaveCopy: Boolean; 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 := 'yourserveraddress'; oServer.User := 'user@domain'; oServer.Password := 'testpassword'; oServer.Protocol := MailServerPop3; // Enable SSL/TLS connection, most modern email servers require SSL/TLS by default oServer.SSLConnection := true; oServer.Port := 995; // If your POP3 doesn't deploy SSL connection // Please use // oServer.SSLConnection := false; // oServer.Port := 110; // Leave a copy of message leaveCopy := true; oClient := TMailClient.Create(Application); oClient.LicenseCode := 'TryIt'; // Send request to EAGetMail Service, then EAGetMail Service retrieves email // in background and this method returns immediately. oClient.GetMailsByQueue(oServer.DefaultInterface, localInbox, leaveCopy); except on ep:Exception do ShowMessage('Error: ' + ep.Message); end; end; end.

Scheduled to retrieve emails

EAGetMail Service provides another advanced feature which allow you set a POP3/IMAP4 account in the EAGetMail Service Manager. EAGetMail Service can download emails from this account in specified time interval. If you need to process the email in the mailbox on a regular basis, you just need to create your mail process application, you don't need to write your email download task part. Finally you just need to set your mail account in EAGetMail Service Manager and specify your application or COMMAND, EAGetMail service will download the emails and invoke your application automatically. Please refer to Mail Pull for more detail.

I also suggest that you have a look at Parse Non-Delivery Report (NDR) using EAGetMail Service. It demonstrates how to set a schedule to check a mailbox and insert non-delivery report to SQL database.

See Also

Using EAGetMail POP3 & IMAP4 Component
Registration-free COM with Manifest File
User Authentication and SSL Connection
Enable TLS 1.2 on Windows XP/2003/2008/7/2008 R2
Using Gmail IMAP4 OAUTH
Using Gmail/GSuite Service Account + IMAP4 OAUTH
Using Office365 EWS OAUTH
Using Office365 EWS OAUTH in Background Service
Using Hotmail IMAP4 OAUTH
Digital Signature and E-mail Encryption/Decryption
Parse Bounced Email (delivery-report)
Work with winmail.dat (TNEF Parser)
EAGetMail Namespace References
EAGetMail POP3 & IMAP4 Component Samples

Online Tutorials

Download Email with Background Service in VB6 - Tutorial
Download Email with Background Service in Delphi - Tutorial
Download Email with Background Service in VC++ - Tutorial