Download Email with Background Service in VB.NET

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 VB.NET.

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 VB.NET 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.

[VB.NET 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.

Imports System.IO
Imports EAGetMail 'imports EAGetMail namespace

Module Module1
    Sub Main()
        ' Create a folder named "inbox" under current directory
        ' to save the email retrieved.
        Dim curpath As String = Directory.GetCurrentDirectory()
        Dim mailbox As String = [String].Format("{0}\inbox", curpath)

        ' If the folder is not existed, create it.
        If Not Directory.Exists(mailbox) Then
            Directory.CreateDirectory(mailbox)
        End If

        Dim oServer As New MailServer("pop3.emailarchitect.net", "test@emailarchitect.net", _
            "testpassword", ServerProtocol.Pop3)
        Dim oClient As New MailClient("TryIt")

        ' If your POP3 server requires SSL connection,
        ' Please add the following codes:
        ' oServer.SSLConnection = True
        ' oServer.Port = 995

        Try
            ' Leave a copy of message on server.
            Dim leaveCopy As Boolean = True

            ' Download emails to this local folder
            Dim downloadFolder As String = mailbox

            ' Send request to EAGetMail Service, then EAGetMail Service retrieves email
            ' in background and this method returns immediately.
            oClient.GetMailsByQueue(oServer, downloadFolder, leaveCopy)
        Catch ep As Exception
            Console.WriteLine(ep.Message)
        End Try
    End Sub
End Module

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 VB.NET.

Appendix

Comments

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