MailBee. NET Objects Tutorials

Asynchronous methods of MailBee.NET

Sending and receiving mail can take a lot of time and make your application appear frozen for a while. To allow the user to interact with your application while the mail operation is in progress, you should use worker threads for mail operations and the main thread of the application for keeping UI (user interface) responsive. Other tasks when worker threads can be useful too are checking mail in multiple mailboxes simultaneously, sending mails in multiple threads, etc.

Note: usually, using worker threads for keeping main UI thread responsive makes sense only for desktop applications. Web and console applications operate in a different way. Still, worker threads can be used in web and console applications too (for instance, checking mail in multiple mailboxes simultaneously).

Although you can create a worker thread manually, it can be easier to let MailBee do this. The components of MailBee.NET Objects assembly support many methods which can spawn worker threads themselves. They are called asynchronous methods.

BeginSend method of Smtp object begins an asynchronous request for sending a mail message to recipients. This method behaves in the same way as Send method, the only difference is that BeginSend returns control to the caller immediately and performs all the activities in the background.


Smtp mailer = new Smtp();
// Initiate an asynchronous send mail attempt.
mailer.BeginSend("", EmailAddressCollection.Parse(""), new AsyncCallback(SendCallback), mailer);


Dim mailer As New Smtp() 
' Initiate an asynchronous send mail attempt.
mailer.BeginSend("", EmailAddressCollection.Parse(""), New AsyncCallback(AddressOf SendCallback), mailer)

SendCallback function is called when the asynchronous operation is completed. You can leave it a null reference if you are not planning to use callbacks (if you do not need to run any code after the the asynchronous operation has completed. If you are using callbacks, you can implement the callback function as below:


// Send callback function.
private void SendCallback(IAsyncResult result)
    Smtp mailer = (Smtp)result.AsyncState;
    MessageBox.Show("The e-mail was sent to: " + mailer.GetAcceptedRecipients().ToString());


' Send callback function.
Private  Sub SendCallback(ByVal result As IAsyncResult)
    Dim mailer As Smtp = CType(result.AsyncState, Smtp)
    MessageBox.Show("The e-mail was sent to: " + mailer.GetAcceptedRecipients().ToString())
End Sub

Connect method also has its asynchronous version - BeginConnect.

For instance, we can make an attempt to connect to the POP3 mail server as follows, the rest of the POP3 conversation will be made in the callback function.


Pop3 pop = new Pop3();

pop.BeginConnect("", 110, true, new AsyncCallback(ConnectCallbackAndGetMessageList), pop);


Dim pop As New Pop3()

pop.BeginConnect("", 110, True, New AsyncCallback(AddressOf ConnectCallbackAndGetMessageList), pop)


private void ConnectCallbackAndGetMessageList(IAsyncResult result)
	Pop3 pop = (Pop3)result.AsyncState;
	// End the pending asynchronous connection request

	pop.Login("jdoe", "secret");

	MailMessageCollection msgs;

	// Download message headers
	msgs = pop.DownloadMessageHeaders();

	// The message headers are downloaded, so we may disconnect now


Private Sub ConnectCallbackAndGetMessageList(ByVal result As IAsyncResult)
	Dim pop As Pop3 = CType(result.AsyncState, Pop3)
	' End the pending asynchronous connection request

	pop.Login("jdoe", "secret")

	Dim msgs As MailMessageCollection

	' Download message headers.
	msgs = pop.DownloadMessageHeaders()

	' The message headers are downloaded, so we may disconnect now.
End Sub

The callback function ends the asynchronous connection attempt and then logs in the mailbox and downloads message headers of all messages. As you can see, it's allowed to use synchronous methods (like Login) in conjunction with asynchronous methods. The entire callback function will execute in the worker thread and even synchronous method calls made in this thread will not block the main thread.

Note: You can find detailed examples of using MailBee.NET asynchronous functions in Samples folder (see "Sample Applications" link in "MailBee.NET Objects" folder of Start menu). All sample projects with number 2 in their names work in asynchronous mode. For instance, in MailBee.NET SMTP Demo 2 user interface stays responsive while in SMTP Demo 1 it gets blocked until the operation completes.