Frankie
Frankie

Reputation: 2265

How to get list of mailboxes using AE.Net.Mail?

I'm trying to get a list of mailboxes using AE.Net.Mail and I cannot find any documentation. The ListMailboxes method takes in a reference string and a pattern string. I'm not sure what either parameter should be.

using (var imap = new AE.Net.Mail.ImapClient(host, username, password, AE.Net.Mail.ImapClient.AuthMethods.Login, port, isSSL))
{
    List<Mailbox> boxes = imap.ListMailboxes("", ""); // string reference, string parameter
}

Upvotes: 2

Views: 4999

Answers (2)

user1166905
user1166905

Reputation: 2622

I found this to work:

var listMailboxes = imap.ListMailboxes(string.Empty, "*");

foreach (var listMailbox in listMailboxes)
{
    var mailbox = listMailbox.Name;                    
}

Upvotes: 6

M.Babcock
M.Babcock

Reputation: 18965

The ImapClient.ListMailboxes method from AE.Net.Mail is a pretty thin wrapper for the IMAP LIST command.

public Mailbox[] ListMailboxes(string reference, string pattern) 
{
    IdlePause();

    var x = new List<Mailbox>();
    string command = GetTag() + "LIST " + reference.QuoteString() + " " + pattern.QuoteString();
    string reg = "\\* LIST \\(([^\\)]*)\\) \\\"([^\\\"]+)\\\" \\\"?([^\\\"]+)\\\"?";
    string response = SendCommandGetResponse(command);
    Match m = Regex.Match(response, reg);
    while (m.Groups.Count > 1) 
    {
        Mailbox mailbox = new Mailbox(m.Groups[3].ToString());
        x.Add(mailbox);
        response = GetResponse();
        m = Regex.Match(response, reg);
    }
    IdleResume();
    return x.ToArray();
}

Section 6.3.8 of the IMAP RFC contains some examples of how these parameters are generally interpreted by the IMAP server ("Mailbox Name" is the pattern parameter):

Reference     Mailbox Name  Interpretation
------------  ------------  --------------
~smith/Mail/  foo.*         ~smith/Mail/foo.*
archive/      %             archive/%
#news.        comp.mail.*   #news.comp.mail.*
~smith/Mail/  /usr/doc/foo  /usr/doc/foo
archive/      ~fred/Mail/*  ~fred/Mail/*

Though it also says the following regarding the Reference parameter:

Note: The interpretation of the reference argument is implementation-defined. It depends upon whether the server implementation has a concept of the "current working directory" and leading "break out characters", which override the current working directory.

So the examples may or may not work depending on your server implementation.

Upvotes: 3

Related Questions