3

I am trying to get the sent folders to display but it shows that the folder has no children in it. All folders are empty except inbox. I am using the following code.

using (var client = new ImapClient())
{
   client.Connect(credentials.incoming_host, (int)credentials.incoming_port, credentials.incoming_ssl); //for SSL
   client.Authenticate(credentials.email, credentials.password);
   client.Inbox.Open(FolderAccess.ReadOnly);

   var sentFolder= client.GetFolder(MailKit.SpecialFolder.Sent);
   var Folders = client.GetFolders(client.PersonalNamespaces[0]);

   client.Disconnect(true);
}

I tried sending an email using the same folder, and then append it like:

var sentFolder = imapclient.GetFolder(SpecialFolder.Sent);
sentFolder.Append(message);

My outlook did detect it and added into the sent folder.

user9821082
  • 31
  • 1
  • 5

2 Answers2

4

From the MailKit README:

If the IMAP server supports the SPECIAL-USE or the XLIST (GMail) extension, you can get ahold of the pre-defined All, Drafts, Flagged (aka Important), Junk, Sent, Trash, etc folders like this:

if ((client.Capabilities & (ImapCapabilities.SpecialUse | ImapCapabilities.XList)) != 0) {
    var drafts = client.GetFolder (SpecialFolder.Drafts);
} else {
    // maybe check the user's preferences for the Drafts folder?
}

In cases where the IMAP server does not support the SPECIAL-USE or XLIST extensions, you'll have to come up with your own heuristics for getting the Sent, Drafts, Trash, etc folders. For example, you might use logic similar to this:

static string[] CommonSentFolderNames = { "Sent Items", "Sent Mail", "Sent Messages", /* 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 == name)
                return folder;
        }
    }

    return null;
}

Using LINQ, you could simplify this down to something more like this:

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

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

    return personal.GetSubfolders (false, cancellationToken).FirstOrDefault (x => CommonSentFolderNames.Contains (x.Name));
}

Another option might be to allow the user of your application to configure which folder he or she wants to use as their Sent folder, Drafts folder, Trash folder, etc.

How you handle this is up to you.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • tq but the things is that in my case, client.GetFolders(client.PersonalNamespaces[0]) gives me 5 empty folders(sent, draft,etc) and inbox with 2k items in it – user9821082 Sep 26 '19 at 01:51
  • i mean i already did the solution and i still didnt get the contents in the sent folder. it still shows empty but the inbox – user9821082 Sep 27 '19 at 01:52
  • Get a protocol log and see what is going on, then... `new ImapClient (new ProtocolLogger ("imap.log"))` – jstedfast Sep 27 '19 at 03:06
  • S: A00000004 OK LIST completed C: A00000005 XLIST "" "*" S: * XLIST (\HasNoChildren \Trash) "/" "Deleted Items" S: * XLIST (\HasNoChildren) "/" "Drafts" S: * XLIST (\HasChildren \Inbox) "/" "Inbox" S: * XLIST (\HasNoChildren) "/" "Inbox/test" S: * XLIST (\HasNoChildren \Spam) "/" "Junk E-Mail" S: * XLIST (\HasNoChildren \Sent) "/" "Sent Items" – user9821082 Sep 27 '19 at 04:30
  • does that mean mailkit isnt saving the emails i sent to sent folder? i tried appending emails to the folder and my outlook did got updated with the new sent email – user9821082 Sep 27 '19 at 04:32
  • That log just shows MailKit getting a list of folders. Where is the log that checks for messages in your Sent Items folder? – jstedfast Sep 27 '19 at 10:53
  • Please add the full log to your question (after scrubbing the AUTH command). – jstedfast Sep 27 '19 at 10:54
  • Please also add your code that checks for messages in your Sent Items folder. – jstedfast Sep 27 '19 at 10:55
0

It is necessary to open the folder otherwise it will appear empty.

                IMailFolder personal = client.GetFolder(client.PersonalNamespaces[0]);

                foreach (IMailFolder folder in personal.GetSubfolders(false, cancellationToken))
                {
                    folder.Open(FolderAccess.ReadOnly);
                    Console.WriteLine($"folder.Name = {folder.Name}");
                    Console.WriteLine($"{folder.Name} messages : {folder.Count}");

                }
m12lrpv
  • 997
  • 2
  • 11
  • 18