Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImapClient.GetFolder(MailKit.SpecialFolder.Sent) yields null #36

Closed
tichyt opened this issue Apr 14, 2014 · 6 comments
Closed

ImapClient.GetFolder(MailKit.SpecialFolder.Sent) yields null #36

tichyt opened this issue Apr 14, 2014 · 6 comments

Comments

@tichyt
Copy link

tichyt commented Apr 14, 2014

Hi Jeff, for an uknown reason I never get access to the Sent folders, result is null. Inbox works. Any idea?
Thanks,
Tomas

@jstedfast
Copy link
Owner

Yea, the SpecialFolders API will return null for folders that are not defined by the server via the SPECIAL-USE or XLIST extensions (SPECIAL-USE is an official IMAP extension and XLIST is a custom extension that Google uses for GMail).

Exchange probably doesn't implement either extension, so that's probably why it returns null.

What you'll probably need to do is have a configuration setting in your app allowing the user to specify what the name of their "Sent" folder is.

Some mail clients, like Outlook, auto-create a folder on the server called "Sent Mail". Others create "sent-mail", and some just call it "Sent". And some clients create a folder with a translated name.

This is actually why, I think, developers came up with the SPECIAL-USE IMAP extension because it makes things a lot simpler.

One way to check if the IMAP server supports SPECIAL-USE is to check:

if ((client.Capabilities & (ImapCapabilities.SpecilUse | ImapCapabilities.XList)) != 0) {
    // ...
}

Does that help?

@tichyt
Copy link
Author

tichyt commented Apr 14, 2014

Well, it explains the situation, but I remember ImapX did have Sent folder (probably implementation not according to standards I guess?) when I used it with the same Exchange. Nevertheless, what would be the best approach - i'm trying to list all root folders and check for "Sent" names, but probably doing something wrong as

var root = client.GetFolder("");

returns folder that cannot be opened (BAD response). How do I correctly list all root-level folders? From there on I can recursively traverse via GetSubfolders, that works nicely already.
Thanks,
Tomas

@tichyt
Copy link
Author

tichyt commented Apr 14, 2014

OK, just tried Sent fallback to Sent Items and Odeslaná Pošta (local version) , last one worked, so solved I guess. Thank you,
Tomas

@jstedfast
Copy link
Owner

I'm glad you figured it out, but I guess I should give my recommendation anyway, just in case anyone else stumbles onto this sort of problem (and I'll probably try to document it as well).

My recommendation would be to do something like this:

static string[] CommonSentFolderNames = { "Sent Items", "Sent Mail", /* maybe add some translated names */ };

static IFolder GetSentFolder (ImapClient client, CancellationToken cancellationToken)
{
    var personal = client.GetFolder (client.PersonalNamespaces[0]);

    foreach (var folder in personal.GetSubfolders (false, cancellationToken)) {
        foreach (var name in CommonSentFolderNames) {
            if (folder.Name == commonName)
                return folder;
        }
    }

    return null;
}

I guess this could be done simpler with LINQ:

var personal = client.GetFolder (client.PersonalNamespaces[0]);
var sentFolder = personal.GetSubfolders (false).FirstOrDefault (x => CommonSentFolderNames.Contains (x.Name));

@tichyt
Copy link
Author

tichyt commented Apr 15, 2014

yep, done exactly as you suggested Jeff. I think this can be closed now, or?
Thank you for your help,
Tomas

@jstedfast
Copy link
Owner

Yea, I just left it open until I had time to add the above snippets to the documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants