PowerTCP Mail for .NET
SelectedMailbox Property
Example 




Gets or sets the selected Mailbox.
Syntax
Public Property SelectedMailbox As Mailbox
Dim instance As Imap
Dim value As Mailbox
 
instance.SelectedMailbox = value
 
value = instance.SelectedMailbox
public Mailbox SelectedMailbox {get; set;}
public: __property Mailbox* get_SelectedMailbox();
public: __property void set_SelectedMailbox( 
   Mailbox* value
);
public:
property Mailbox^ SelectedMailbox {
   Mailbox^ get();
   void set (    Mailbox^ value);
}

Property Value

Returns the Mailbox that is in the selected state; or null if no Mailbox is selected.
Remarks

Use Select to select a mailbox by absolute path.

Set this property to select a Mailbox, causing the Imap component to send a SELECT command to initialize Mailbox.FirstUnseen Mailbox.Marked, Mailbox.Recent, Mailbox.Selectable, and Mailbox.SublevelsAllowed. The server now considers this Mailbox to be in the "Selected" state and messages can be accessed.

The nature of IMAP is that other clients can delete messages on the server at anytime, so using "foreach" to enumerate the messages in a mailbox is not recommended (unsolicited responses may require the removal of the message from the Mailbox before the enumeration completes). Instead, use "foreach" on SelectedMailbox.ToArray(). If, during this enumeration, a message is deleted by another client, MessageBase.Id will be set to 0, so this value can be checked to ensure the existence of the message before acting upon it.

Methods that access messages will update this property to the containing Mailbox.

Set to null to close the selected mailbox and enter the "Authenticated" state. This action expunges all messages that are marked for deletion.

Example
In this example, the Imap component gets messages from a specified mailbox and saves them to disk.
private void getMessages(object sender)
{
    //Configure server and account info
    imap1.Session.RemoteEndPoint = 
        new Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session));
    imap1.Session.Username = myUsername;
    imap1.Session.Password = myPassword;

    //Connect and log into the account
    imap1.Connect();
    imap1.Authenticate();

    //Open the mailbox containing the messages to get
    imap1.SelectedMailbox = imap1.Mailboxes["INBOX"];

    //Specify folder to store messages
    string messageFolder = Application.StartupPath + "\\messages";

    //Download all messages in the account and save them to disk
    foreach (ImapMessage imapMessage in imap1.SelectedMailbox.ToArray())
    {
        imapMessage.Get();
        imapMessage.Message.Save(messageFolder + "\\" + imapMessage.Uid + ".eml");
    }

    //Gracefully logout of the session
    imap1.Close();
}

private void imap1_Progress(object sender, ImapProgressEventArgs e)
{
    //Update progress bar as message is sent
    progressBar1.Value = (e.Final) ? 0 : (int)((e.Position * 100) / e.Length);
}
Private Sub getMessages(ByVal sender As Object)
    'Configure server and account info
    imap1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint(myServer, Imap.GetDefaultPort(imap1.Session))
    imap1.Session.Username = myUsername
    imap1.Session.Password = myPassword

    'Connect and log into the account
    imap1.Connect()
    imap1.Authenticate()

    'Open the mailbox containing the messages to get
    imap1.SelectedMailbox = imap1.Mailboxes("INBOX")

    'Specify folder to store messages
    Dim messageFolder As String = Application.StartupPath & "\messages"

    'Download all messages in the account and save them to disk
    For Each imapMessage As ImapMessage In imap1.SelectedMailbox.ToArray()
        imapMessage.Get()
        imapMessage.Message.Save(messageFolder & "\" & imapMessage.Uid & ".eml")
    Next imapMessage

    'Gracefully logout of the session
    imap1.Close()
End Sub

Private Sub imap1_Progress(ByVal sender As Object, ByVal e As ImapProgressEventArgs) Handles imap1.Progress
    'Update progress bar as message is sent
    progressBar1.Value = If(e.Final, 0, CInt((e.Position * 100) \ e.Length))
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