PowerTCP Mail for .NET
Update Event
Example 




Raised when unsolicited data is received from the server.
Syntax
Event Data

The event handler receives an argument of type ImapUpdateEventArgs containing data related to this event. The following ImapUpdateEventArgs properties provide information specific to this event.

PropertyDescription
MessagesGets an ImapMessage array that represents the messages affected by the update.  
Response Gets an ImapResponse encapsulating a response from the IMAP server.  
Remarks
This event is raised when the number of messages in the selected mailbox has changed.
Example
This example demonstrates use of the Idle method and the Update event.
/// <summary>
/// Used here as a stand-in for any collection that should be updated to reflect 
/// the current state of messages on the server, such as a ListView.Items collection.
/// </summary>
Dictionary<string, ImapMessage> independentImapMessageCollection = new Dictionary<string, ImapMessage>();

private void button1_Click(object sender, EventArgs e)
{
    //Set server and account info
    imap1.Session.RemoteEndPoint = new Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session));
    imap1.Session.Username = myUsername;
    imap1.Session.Password = myPassword;

    //Start a worker thread
    //This allows the function to execute without blocking the UI
    imap1.Start(listenForUpdates, null);
}

private void button2_Click(object sender, EventArgs e)
{
    //Set server and account info
    smtp1.Session.RemoteEndPoint = new Dart.Mail.IPEndPoint(mySmtpServer, Smtp.GetDefaultPort(smtp1.Session));
    smtp1.Session.Username = myUsername;
    smtp1.Session.Password = myPassword;

    //Send a message to trigger an exists update.
    smtp1.Send(myEmail, myEmail, "Test Message", "Test Message");
}

private void listenForUpdates(object state)
{
    //This function executes on a worker thread
    //Connect and log into the account
    imap1.Connect();
    imap1.Authenticate();

    //Set the current mailbox to INBOX
    imap1.SelectedMailbox = imap1.Mailboxes["INBOX"];

    //Retrieve all messages in the mailbox
    imap1.SelectedMailbox.Get();

    //Add the retrieved messages to the independent collection of ImapMessages
    foreach (ImapMessage message in imap1.SelectedMailbox.ToArray())
        independentImapMessageCollection.Add(message.Uid, message);

    //Initiate Idle to await new updates from server
    imap1.Idle();

    //Idle() returns once the client intiates communication with the server (on a different thread).
    System.Diagnostics.Debug.WriteLine("DONE IDLING");
}

private void imap1_Update(object sender, ImapUpdateEventArgs e)
{
    switch (e.Response.Operation)
    {
        case "EXPUNGE":
            //Remove message(s) from any independently implemented collection
            foreach (ImapMessage msg in e.Messages)
                independentImapMessageCollection.Remove(msg.Uid);
            break;

        case "EXISTS":
            //Retrieve the new messages on a worker thread
            imap1.Start(getMessages, e.Messages);
            break;
    }
}

/// <summary>
/// When the server informs the client of new messages, download the new messages (interrupts IDLE).
/// </summary>
void getMessages(object msgs)
{
    //Executes on a worker thread so UI messages are not interrupted.
    //Cast msgs back to ImapMessage array
    ImapMessage[] messages = msgs as ImapMessage[];

    //Retrieve the new messages from the server. This interrupts IDLE.
    imap1.SelectedMailbox.Get(messages, ImapMessageInfo.Message);

    //Update the independent collection
    foreach (ImapMessage message in messages)
        independentImapMessageCollection.Add(message.Uid, message);

    //Resume Idle to await new updates from server
    imap1.Idle();

    //Idle() returns once the client intiates communication with the server (on a different thread).
    System.Diagnostics.Debug.WriteLine("DONE IDLING");
}
''' <summary>
''' Used here as a stand-in for any collection that should be updated to reflect 
''' the current state of messages on the server, such as a ListView.Items collection.
''' </summary>
Private independentImapMessageCollection As New Dictionary(Of String, ImapMessage)()

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    'Set server and account info
    imap1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session))
    imap1.Session.Username = myUsername
    imap1.Session.Password = myPassword

    'Start a worker thread
    'This allows the function to execute without blocking the UI
    imap1.Start(AddressOf listenForUpdates, Nothing)
End Sub

Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
    'Set server and account info
    smtp1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(mySmtpServer, Smtp.GetDefaultPort(smtp1.Session))
    smtp1.Session.Username = myUsername
    smtp1.Session.Password = myPassword

    'Send a message to trigger an exists update.
    smtp1.Send(myEmail, myEmail, "Test Message", "Test Message")
End Sub

Private Sub listenForUpdates(ByVal state As Object)
    'This function executes on a worker thread
    'Connect and log into the account
    imap1.Connect()
    imap1.Authenticate()

    'Set the current mailbox to INBOX
    imap1.SelectedMailbox = imap1.Mailboxes("INBOX")

    'Retrieve all messages in the mailbox
    imap1.SelectedMailbox.Get()

    'Add the retrieved messages to the independent collection of ImapMessages
    For Each message As ImapMessage In imap1.SelectedMailbox.ToArray()
        independentImapMessageCollection.Add(message.Uid, message)
    Next message

    'Initiate Idle to await new updates from server
    imap1.Idle()

    'Idle() returns once the client intiates communication with the server (on a different thread).
    System.Diagnostics.Debug.WriteLine("DONE IDLING")
End Sub

Private Sub imap1_Update(ByVal sender As Object, ByVal e As ImapUpdateEventArgs) Handles imap1.Update
    Select Case e.Response.Operation
        Case "EXPUNGE"
            'Remove message(s) from any independently implemented collection
            For Each msg As ImapMessage In e.Messages
                independentImapMessageCollection.Remove(msg.Uid)
            Next msg

        Case "EXISTS"
            'Retrieve the new messages on a worker thread
            imap1.Start(AddressOf getMessages, e.Messages)
    End Select
End Sub

''' <summary>
''' When the server informs the client of new messages, download the new messages (interrupts IDLE).
''' </summary>
Private Sub getMessages(ByVal msgs As Object)
    'Executes on a worker thread so UI messages are not interrupted.
    'Cast msgs back to ImapMessage array
    Dim messages() As ImapMessage = TryCast(msgs, ImapMessage())

    'Retrieve the new messages from the server. This interrupts IDLE.
    imap1.SelectedMailbox.Get(messages, ImapMessageInfo.Message)

    'Update the independent collection
    For Each message As ImapMessage In messages
        independentImapMessageCollection.Add(message.Uid, message)
    Next message

    'Resume Idle to await new updates from server
    imap1.Idle()

    'Idle() returns once the client intiates communication with the server (on a different thread).
    System.Diagnostics.Debug.WriteLine("DONE IDLING")
End Sub
See Also

Reference

Imap Class
Imap Members


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic