Download Email with Background Service in C++/CLI/CLR

In previous section, I introduced how to use UIDL function to mark the email has been downloaded. In this section, I will introduce how to download email with background service in C++/CLI/CLR.

Introduction

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 POP3 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.

Important

To run the following example, you need to download EAGetMail and EAGetMail Service at first, and then install both on your machine.

Note

Remarks: All of examples in this section are based on first section: A simple C++/CLI/CLR project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference to your project.

[C++/CLI/CLR Example - Retrieve email with background service]

The following example codes demonstrate how to use EAGetMail POP3 component to retrieve email with background service. In order to run it correctly, please change email server, user, password, folder, file name values.

Note

To get the full sample projects, please refer to Samples section.

#include "stdafx.h"

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);
}

int main(array<System::String ^> ^args)
{
    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);
        }

        MailServer ^oServer = gcnew MailServer("pop3.emailarchitect.net",
            "test@emailarchitect.net",
            "testpassword",
            ServerProtocol::Pop3);

        // Enable SSL/TLS connection, most modern email server require SSL/TLS by default
        oServer->SSLConnection = true;
        oServer->Port = 995;

        // if your server doesn't support SSL/TLS, please use the following codes
        // oServer->SSLConnection = false;
        // oServer->Port = 110;


        MailClient ^oClient = gcnew MailClient("TryIt");

        // Leave a copy of message on server.
        bool leaveCopy = true;

        // Download emails to this local folder
        String^ downloadFolder = localInbox;

        // Send request to EAGetMail Service, then EAGetMail Service retrieves email
        // in background and this method returns immediately.
        oClient->GetMailsByQueue(oServer, downloadFolder, leaveCopy);

        Console::WriteLine("Completed!");
    }
    catch (Exception ^ep)
    {
        Console::WriteLine(ep->Message);
    }

    return 0;
}

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.

Next Section

At next section I will introduce how to parse email in C++/CLI/CLR.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.