6

I am trying to retrieve the subfolders and messages from the inbox but i was only able to retrieve the parent subfolders, also tried with PersonalNamespaces[0]

var inbox = client.Inbox;
inbox.Open (FolderAccess.ReadWrite);

Debug.WriteLine ("Total messages: {0}", inbox.Count);
//client.Inbox.Status(StatusItems.Unread);
//Debug.WriteLine("Recent messages: {0}", inbox.Unread);
//Debug.WriteLine("Recent messages: {0}", inbox.FirstUnread);

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

foreach (var folder in inbox.GetSubfolders(false))
{
        Console.WriteLine("[folder] {0}", folder.Name);
        folder.Open(FolderAccess.ReadOnly);
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3430435
  • 71
  • 1
  • 3
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders May 25 '15 at 18:21

1 Answers1

4

Not all IMAP servers will even allow subfolders of the INBOX folder. If you are sure that your IMAP account's INBOX folder has subfolders, you would use the following code snippet to get them:

foreach (var folder in client.Inbox.GetSubfolders (false)) {
    Console.WriteLine ("[folder] {0}", folder.Name);
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Hi thanks for the reply, I tried that before but doesnt work either, my IMAP account has subfolders and they can be seen in outlook and webmail, i also made them subscribed but result is the same, is there any other way? Thanks – user3430435 May 26 '15 at 10:38
  • If MailKit isn't finding any subfolders of INBOX, then there aren't any. To confirm this, you can take a look at the protocol log by following these instructions: https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog – jstedfast May 26 '15 at 12:37
  • Here you can see the subfolders in outlook: http://prntscr.com/79ldh4 is there anything incorrect? – user3430435 May 26 '15 at 14:05
  • What does the protocol log say? – jstedfast May 26 '15 at 16:50
  • 1
    I think I found a bug that might cause this. The problem is likely due to the fact that the INBOX folder name is case-insensitive and ImapFolder.GetSubfolders() filters out folders returned by the server that do not start with the ImapFolder's name in order to work around a server bug: https://github.com/jstedfast/MailKit/issues/149 – jstedfast May 27 '15 at 00:55
  • Yes its smartermail so what is the way around the problem? thank you – user3430435 May 27 '15 at 10:29
  • I've committed a patch to fix the problem, but I probably won't have a chance to make a new release until this weekend. You can use GitHub and build from source in the meantime, however. – jstedfast May 27 '15 at 12:17
  • thanks a lot for your work i will wait for the release – user3430435 May 27 '15 at 13:10
  • in mimekit.internetaddress i can only get name, how about the email address ? thank you – user3430435 May 27 '15 at 14:53
  • What you'll need to do is cast the InternetAddress to a MailboxAddress and then use the Address property. InternetAddress is the abstract base class for MailboxAddress and GroupAddress. – jstedfast May 27 '15 at 20:50
  • Thanks again for the help :) let me know when you make the release in nuget if possible, at the moment i have one problem i am trying to move a message from inbox to deleted items folder but keep getting error folder not open (but i user folder.Open(ReadWrite) in both folders), only deleted items stays open which is the one that i opened last – user3430435 May 28 '15 at 12:23
  • The way IMAP works is that only the last folder to be opened remains open. Previously opened folders are automatically closed. The destination folder for the APIs that copy and move messages does not need to be open, only the source folder needs to be open. – jstedfast May 28 '15 at 14:42
  • I tried that way too but keep getting the same error: Additional information: Unexpected token in IMAP response: [atom: 0] Here is my code: if (inbox.IsOpen == true) { inbox.MoveTo(uid, folder); } After checking the webmail the message appears in inbox and also recreates a new one in deleted items – user3430435 May 28 '15 at 14:57
  • Can you paste a protocol log into a new bug report in the https://github.com/jstedfast/MailKit issue tracker? See https://github.com/jstedfast/MailKit/blob/master/FAQ.md#ProtocolLog for help getting a protocol log. As far as the message showing up in 2 places, see this: https://github.com/jstedfast/MailKit/issues/160 – jstedfast May 28 '15 at 15:54
  • I've made a new release of MailKit to NuGet. – jstedfast Jun 01 '15 at 14:15