MailBee.NET Objects 3.1

Imap.BeginUploadMessage Method 

Begins an asynchronous request for uploading the message into the specified folder on the server.

public IAsyncResult BeginUploadMessage(
   MailMessage msg,
   string folderName,
   string flags,
   string dateTimeString,
   bool batchMode,
   UidPlusResult result,
   AsyncCallback callback,
   object state
);

Parameters

msg
A reference to the MailMessage object representing the message to be uploaded.
folderName
The full name of the folder to upload the message to.
flags
The string containing the message flags (in IMAP4 format) to be assigned to the message, or a null reference (Nothing in Visual Basic) to set default flags at the discretion of the server.
dateTimeString
The string containing datetime (in IMAP4 format) to be assigned to the INTERNALDATE attribute of the message (the date of receiving the message by the server), or a null reference to let the server assign its current datetime value.
batchMode
If true and LITERAL+ extension is supported by the server, the message will be uploaded in a single network operation; otherwise, in two operations.
result
A reference to the UidPlusResult object to be filled with the outcome of the upload operation reported by UIDPLUS enabled server (the outcome includes the UID assigned to the uploaded message and the UIDVALIDITY of the folder the message was uploaded to), or a null reference if the application does not need this information.
callback
The AsyncCallback delegate. You can leave it a null reference (Nothing in Visual Basic) if you do not use callbacks.
state
An object that contains state information for this request. You can leave it a null reference (Nothing in Visual Basic).

Return Value

An IAsyncResult that references the asynchronous upload process.

Remarks

This method is an asynchronous version of UploadMessage.

Exceptions

Exception Type Condition
MailBeeInvalidStateException There is already an operation in progress.

Example

This sample demonstrates asynchronous uploading a message into "Draft" folder and use of a callback function in a console application.

[C#]
using System;
using MailBee;
using MailBee.ImapMail;
using MailBee.Mime;

class Sample
{
    // A callback function.
    private static void UploadMessageCallback(IAsyncResult result)
    {
        Imap imp = (Imap)result.AsyncState;

        try
        {
            imp.EndUploadMessage();
            Console.WriteLine("The message was successfully uploaded");
        }
        catch (MailBeeException e)
        {
            // In callback functions (generally speaking, in worker threads),
            // it's better to handle exceptions. If exception is not handled,
            // the worker thread silently dies and the application may never
            // know that something went wrong.
            Console.WriteLine(e.Message);
        }
    }

    // The actual code.
    static void Main(string[] args)
    {
        Imap imp = new Imap();

        // Connect to the server and log in the account.
        imp.Connect("imap.somedomain.com");
        imp.Login("jdoe", "secret");

        // Create a new message. Assume we need to save a draft of the message.
        MailMessage msg = new MailMessage();

        // Initiate an asynchronous upload attempt with the following settings:
        // - Object to download: msg
        // - Folder to download to: Draft
        // - Flags: \Draft
        // - Message date on server: datetime/timezone of the local computer
        // - batch mode: enabled
        // - obtain UID of uploaded message: no, thanks
        IAsyncResult ar = imp.BeginUploadMessage(msg, "Draft", @"\Draft",
            null, true, null, new AsyncCallback(UploadMessageCallback), imp);

        // Simulate some lengthy work here. At the same time,
        // uploading occurs on another thread.
        System.Threading.Thread.Sleep(3000);

        // If the upload process is still in progress,
        // then wait until it's finished.
        while (imp.IsBusy) ar.AsyncWaitHandle.WaitOne();

        // Disconnect from the server.
        imp.Disconnect();
    }
}
[Visual Basic]
Imports System
Imports MailBee
Imports MailBee.ImapMail
Imports MailBee.Mime

Module Sample
    ' A callback function.
    Private Sub UploadMessageCallback(ByVal result As IAsyncResult)
        Dim imp As Imap = CType(result.AsyncState, Imap)

        Try
            imp.EndUploadMessage()
            Console.WriteLine("The message was successfully uploaded")
        Catch e As MailBeeException
            ' In callback functions (generally speaking, in worker threads),
            ' it's better to handle exceptions. If exception is not handled,
            ' the worker thread silently dies and the application may never
            ' know that something went wrong.
            Console.WriteLine(e.Message)
        End Try
    End Sub

    ' The actual code.
    Sub Main()
        Dim imp As New Imap

        ' Connect to the server and log in the account.
        imp.Connect("imap.somedomain.com")
        imp.Login("jdoe", "secret")

        ' Create a new message. Assume we need to save a draft of the message.
        Dim msg As New MailMessage

        ' Initiate an asynchronous upload attempt with the following settings:
        ' - Object to download: msg
        ' - Folder to download to: Draft
        ' - Flags: \Draft
        ' - Message date on server: datetime/timezone of the local computer
        ' - batch mode: enabled
        ' - obtain UID of uploaded message: no, thanks
        Dim ar As IAsyncResult = imp.BeginUploadMessage(msg, "Draft", "\Draft", _
            Nothing, True, Nothing, New AsyncCallback(AddressOf UploadMessageCallback), imp)

        ' Simulate some lengthy work here. At the same time,
        ' uploading occurs on another thread.
        System.Threading.Thread.Sleep(3000)

        ' If the upload process is still in progress,
        ' then wait until it's finished.
        While imp.IsBusy
            ar.AsyncWaitHandle.WaitOne()
        End While

        ' Disconnect from the server.
        imp.Disconnect()
    End Sub
End Module

See Also

Imap Class | MailBee.ImapMail Namespace | UploadMessage