none
Weird issue retrieving mailitems from Inbox RRS feed

  • Question

  • Hi,

    I'm developing an Outlook 2010 addin and I'm facing a strange issue when I try to retrieve mailitems from Inbox. I've been googling for days and I haven't found an answer to my issue, this is why I'm posting here, looking for your help.

    This is the code that I'm using to retrieve mailitems from Inbox:

    public IList<Outlook.MailItem> GetRecentMailList(string _sender, Outlook.NameSpace _nameSpace)
    {
        List<Outlook.MailItem> result = new List<Outlook.MailItem>();

        dynamic _mailFolder = GetMailFolder(_nameSpace);

        _mailFolder.Items.Sort("[ReceivedTime]");
        string filter = "[MessageClass] = \"IPM.Note\"";

        try
        {
    Microsoft.Office.Interop.Outlook.Items _items = _mailFolder.Items.Restrict(filter);
           foreach (var item in _items)
           {
    Outlook.MailItem mail = item as Microsoft.Office.Interop.Outlook.MailItem;
    if (((mail.SenderEmailType == "EX") ? mail.Sender.GetExchangeUser().PrimarySmtpAddress : mail.SenderEmailAddress) == _sender)
    { result.Add(mail); }
           }
           return (result.Count() > 0) ? result.OrderByDescending(it => it.ReceivedTime).ToList() : null;
        }
        catch
        { return null; }
    }

    private dynamic GetMailFolder(NameSpace _nameSpace)
    {
    return _nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
    }

    And this is the call to the method to get mailitems

    Globals.ThisAddIn.Requester.RecentMailList = 
    Globals.ThisAddIn.Requester.GetRecentMailList(Globals.ThisAddIn.Requester.SenderMailAddress, Globals.ThisAddIn.Application.GetNamespace("MAPI"));

    where Globals.ThisAddIn.Requester is a custom object developed by me that has a field of type:
    IList<Outlook.MailItem> _recentMailList;

    and a public property to access that field:
    public IList<Outlook.MailItem> RecentMailList{

                get { return _recentMailList; }
                set { _recentMailList = value; }
            }

    In my Outlook 2010 instance, the instance that I use to debugging, I have configured two profiles, both referencing the same Gmail account.

    Profile a)  is set up manually, i.e. configuring manually all the stuff needed to use a Gmail account with Outlook 2010, as indicated here http://support.google.com/mail/bin/answer.py?hl=en&answer=77689.

    Profile b) is set up using the tool provided by Google, Google Apps Sync for Microsoft Outlook® as indicated here https://tools.google.com/dlpage/gappssync  

    Both profiles are configured with the same Gmail account.

    If I debug my addin using Profile b) everything works well and I get all mailitems that are in Inbox folder. But if I debug my addin using Profile a) no mailitems are retrieved, everything works ok, but no mailitems from Inbox folder are retrieved.

    As I've said, I've been searching for a possible explanation for this behaviour but with no luck. If anyone can help me, I would be really pleased.

    Thanks.

    Wednesday, June 20, 2012 1:48 PM

Answers

  • When you look at the Folder List in Outlook when using the IMAP profile, how many stores (PST files) do you see? I see 2 of them, one is the Gmail IMAP store.
     
    Using this Outlook VBA test code on a Gmail Inbox with 673 items in it produced a Count of 0:
     
    Sub testIMAP()
        Dim oFolder As Outlook.Folder
        Dim oItems As Outlook.Items
        
        Set oFolder = Application.Session.GetDefaultFolder(olFolderInbox)
        Set oItems = oFolder.Items
        MsgBox "Count = " & oItems.Count
    End Sub
     
    However, I figured out a workaround that works in my test on Outlook 2010, and this displays the correct Items count of 673, where Stores.Item(1) is the IMAP store:
     
    MsgBox Application.Session.Stores.Item(1).GetDefaultFolder(olFolderInbox).Items.Count
     
    Use explicit objects in your actual code, this hack with the compound dot operators and various shortcuts was just for testing, not what I'd actually use in production code.
     
    Let me know if this type of workaround works for you.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:612ef7d8-ef92-4172-9bb7-e2416857a6bb...

    Hi Ken
    Thank you very much for your time and effort

    Regards,


    Ken Slovak MVP - Outlook
    • Proposed as answer by Hector Romero Thursday, June 28, 2012 3:13 PM
    • Marked as answer by Hector_Romero Thursday, June 28, 2012 3:15 PM
    Thursday, June 28, 2012 1:32 PM

All replies

  • i assume that when you log into outlook using profile A then you see your mails in inbox? Try to debug and see if using profile A simple _mailFolder.Items.Count returns proper value (at least more then 0)

    Wednesday, June 20, 2012 1:56 PM
  • Hi DamianD

    Yes, your assumption is correct. When I log into Outlook using both profiles I see all may mails in Inbox folder.

    Related to debugging action, if I debug and make a simple _mailFolder.Items.Count using profile a) the result is 0, but when I make the same with profile b) the result is the correct number of mailitems.

    Thanks for your reply.

    Wednesday, June 20, 2012 2:27 PM
  • Are both profiles identical as far as you can tell?
     
    What about the stores, are both using the same PST file? Are both connecting to google using POP3 or does one profile use IMAP?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector_Romero" <=?utf-8?B?SGVjdG9yX1JvbWVybw==?=> wrote in message news:2374149d-7069-46b3-89ae-f7deb769bb6c...

    Hi DamianD

    Yes, your assumption is correct. When I log into Outlook using both profiles I see all may mails in Inbox folder.

    Related to debugging action, if I debug and make a simple _mailFolder.Items.Count using profile a) the result is 0, but when I make the same with profile b) the result is the correct number of mailitems.

    Thanks for your reply.


    Ken Slovak MVP - Outlook
    Wednesday, June 20, 2012 3:19 PM
  • Hi Ken

    What do you mean by identical?

    Both profiles are referring to same Gmail account, so both profiles contain the same set of mails.

    About the stores, each profile is using its own pst file.

    About connections, both profiles are connecting to google using IMAP, as far as I know. 
    I mean:
    profile a), manually configured profile, for sure, because I configured that profile and I did it using IMAP settings.
    profile b), google apps sync configured profile, I don't know, I suppose that also use IMAP, but I'm searching in documentation provided by Google and I don't see anything about it. Is it possible to know it while debugging??

    Thank you very much guys, for your time & effort

    Wednesday, June 20, 2012 3:52 PM
  • By identical I meant same email accounts, same PST files, same for all user settings in the send/receive group for things such leave messages on the server and which folders to download and whether full items or not.
     
    The properties of the email account will tell you if it's POP3 or IMAP. Open the email account properties and the list of accounts in the profile will say POP/SMTP or IMAP for each.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:87adeabb-904c-4af0-a66d-558a2ae37ee9...

    Hi Ken

    What do you mean by identical?

    Both profiles are referring to same Gmail account, so both profiles contain the same set of mails.

    About the stores, each profile is using its own pst file.

    About connections, both profiles are connecting to google using IMAP, as far as I know. 
    I mean:
    profile a), manually configured profile, for sure, because I configured that profile and I did it using IMAP settings.
    profile b), google apps sync configured profile, I don't know, I suppose that also use IMAP, but I'm searching in documentation provided by Google and I don't see anything about it. Is it possible to know it while debugging??

    Thank you very much guys, for your time & effort


    Ken Slovak MVP - Outlook
    Wednesday, June 20, 2012 7:57 PM
  • Hi Ken

    Profile a) is IMAP/SMTP type

    Profile b) is MAPI type

    Thursday, June 21, 2012 8:13 AM
  • It appears to me that this is the difference in the profiles, and it most likely is why you can get your code to work on profile B (MAPI) and not on A (IMAP).
     
    If you can set the A profile to download full items to your PST store it should be able to work OK, if not then I'd investigate creating another MAPI profile and seeing if that also works. It may be the only solution for you.
     
    You aren't checking for anything related to type of email are you (SMTP, EX and so on)? If so you will most likely see that emails in the Google store aren't SMTP but "GOOG" as the type. I've seen that throw off my own code until I figured it out and compensated for that.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector_Romero" <=?utf-8?B?SGVjdG9yX1JvbWVybw==?=> wrote in message news:c812d604-d0ee-4bdf-9233-3c49b77a255a...

    Hi Ken

    Profile a) is IMAP/SMTP type

    Profile b) is MAPI type


    Ken Slovak MVP - Outlook
    Thursday, June 21, 2012 1:07 PM
  • Hi Ken

    About download, I can assure you that both profiles are completely downloaded. I thought about this possiblity that you say, but when I downloaded the whole Inbox folder I saw that the error was still there.

    About the type of emails, I've debugging the addin but I haven't found the property in mail item that show the type of the mail, can you guide me to check for its type? 

    Is it possible that this issue appears because the coexistence of both profiles in the same machine? 

    I'm in a point that I don't know what to think.
    Thank you very much for your time.

    Regards,
    Hector

    Thursday, June 21, 2012 2:50 PM
  • A computer can have many Outlook profiles configured, but only 1 can be used or run at a time. I have machines with over a dozen Outlook profiles on them.
     
    The type of email sender is in the MailItem.SenderEmailType property, which could be EX or SMTP or GOOG and so on.
     
    What I was suggesting was to create another profile "C" that also is set up as a MAPI profile and see if that also works on that Inbox. I'd also create another one the same way as you created "A" to make sure that "A" isn't just corrupted somehow.
     
    Downloading a profile wasn't what I was referring to, it was downloading complete items as opposed to downloading headers first or exclusively. I'd also do a synch (send/receive) before checking to make sure a recent download had happened.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:8631d091-8271-4180-8b9a-4210c1bf443e...

    Hi Ken

    About download, I can assure you that both profiles are completely downloaded. I thought about this possiblity that you say, but when I downloaded the whole Inbox folder I saw that the error was still there.

    About the type of emails, I've debugging the addin but I haven't found the property in mail item that show the type of the mail, can you guide me to check for its type? 

    Is it possible that this issue appears because the coexistence of both profiles in the same machine? 

    I'm in a point that I don't know what to think.
    Thank you very much for your time.

    Regards,
    Hector


    Ken Slovak MVP - Outlook
    Thursday, June 21, 2012 3:57 PM
  • Hi Ken

    With mail items of profile b), the one that's working well, SenderEmailType is SMTP.

    With mail items of profile a), the one that's working wrong, I can't know it because is not retrieving mailitems, that is the origin of this post..

    I'm gonna try your suggestion about create another set of profiles, GApps Synced and manually configured, and I'll keep you informed.

    About downloading question, I also was referring to same thing, I have both profiles fully downloaded, not headers first or exclusively. About send/receive action, I always do a complete send/receive when I debug with the "wrong" profile.

    Thanks.

    A computer can have many Outlook profiles configured, but only 1 can be used or run at a time. I have machines with over a dozen Outlook profiles on them.
     
    The type of email sender is in the MailItem.SenderEmailType property, which could be EX or SMTP or GOOG and so on.
     
    What I was suggesting was to create another profile "C" that also is set up as a MAPI profile and see if that also works on that Inbox. I'd also create another one the same way as you created "A" to make sure that "A" isn't just corrupted somehow.
     
    Downloading a profile wasn't what I was referring to, it was downloading complete items as opposed to downloading headers first or exclusively. I'd also do a synch (send/receive) before checking to make sure a recent download had happened.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:8631d091-8271-4180-8b9a-4210c1bf443e...

    Hi Ken

    About download, I can assure you that both profiles are completely downloaded. I thought about this possiblity that you say, but when I downloaded the whole Inbox folder I saw that the error was still there.

    About the type of emails, I've debugging the addin but I haven't found the property in mail item that show the type of the mail, can you guide me to check for its type? 

    Is it possible that this issue appears because the coexistence of both profiles in the same machine? 

    I'm in a point that I don't know what to think.
    Thank you very much for your time.

    Regards,
    Hector


    Ken Slovak MVP - Outlook


    Thursday, June 21, 2012 4:12 PM
  • Hi Ken

    I have made the test that you indicated and the result is:

    The new profile created with Google Apps Sync works well

    The new profile created with manual configuration doesn't work.

    I have made another test, create a third profile manually configured but  with POP3 settings instead of with IMAP settings. And the result is that my addin with this profile works perfectly.

    Friday, June 22, 2012 12:34 PM
  • So, I think you have your answer.
     
    Use the Google tools or if you need to manually configure the profiles you would need to use POP3 instead of IMAP.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:f1ae3619-fbf9-41a8-a333-a32880299ba2...

    Hi Ken

    I have made the test that you indicated and the result is:

    The new profile created with Google Apps Sync works well

    The new profile created with manual configuration doesn't work.

    I have made another test, create a third profile manually configured but  with POP3 settings instead of with IMAP settings. And the result is that my addin with this profile works perfectly.


    Ken Slovak MVP - Outlook
    Friday, June 22, 2012 1:42 PM
  • Hi Ken

    I'm developing a commercial addin for Outlook and I can't restrict to future customers the way in which they can/have to/must configure their profiles.

    It's because this that I need to solve this issue. Because the addin has to work well in any type of profile configuration.


    Kind regards

    Hector Romero

    Monday, June 25, 2012 8:08 AM
  • check using MFMAPI or OutlookSpy if those mailitems are there when using MAPI approach.
    Monday, June 25, 2012 8:45 AM
  • Hi DamianD

    I'm gonna check it.

    I'll keep you informed

    Regards
    Hector

    Monday, June 25, 2012 9:51 AM
  • Based on earlier posts in the thread, the POP/IMAP profile "A" didn't work but a new one you created with similar characteristics does work? Is that the case?
     
    If so, try to see how the 2 differ as they should be the same.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:2fbf404a-30f5-4b27-b689-36861d4e3027...

    Hi Ken

    I'm developing a commercial addin for Outlook and I can't restrict to future customers the way in which they can/have to/must configure their profiles.

    It's because this that I need to solve this issue. Because the addin has to work well in any type of profile configuration.


    Kind regards

    Hector Romero


    Ken Slovak MVP - Outlook
    Monday, June 25, 2012 1:32 PM
  • No, Ken 

    Profile A is only based in IMAP settings, and it doesn't work. You said that possibly this profile could be corrupt somehow, and then I created a new profile based in IMAP settings, that it doesn't work too.

    Only to test the possibility, I created another new profile based in POP settings. This new profile does work.

    Monday, June 25, 2012 1:54 PM
  • OK, I think I'm clear on it now. The POP3 and MAPI profiles are the ones that are working, not the original IMAP profile. You then created a new IMAP profile and that also didn't work with your code?
     
    Just to verify, on Outlook 2010 the send/receive settings are set as indicated in the article http://www.msoutlook.info/question/412 ?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:4a041d1b-4303-48a8-9b83-a97ad5f9bc63...

    No, Ken 

    Profile A is only based in IMAP settings, and it doesn't work. You said that possibly this profile could be corrupt somehow, and then I created a new profile based in IMAP settings, that it doesn't work too.

    Only to test the possibility, I created another new profile based in POP settings. This new profile does work.


    Ken Slovak MVP - Outlook
    Monday, June 25, 2012 2:45 PM
  • Hi Ken

    Correct. POP3 and MAPI profiles work well, IMAP profiles don't work.

    I've checked the indicated config item in Outlook 2010, and the selected item is "Download complete items including attachments for subscribed folders"

    Regards
    Hector

    Monday, June 25, 2012 3:51 PM
  • I just got access to a mailbox on a Google Apps Synch setup that I can play with. I won't be able to play today, but tomorrow I'll play with setting up an IMAP account there and see what happens.
     
    I already tested a maibox on that server using a "MAPI" profile and that worked OK as far as using code to get the Count for Inbox.Items.
     
    Next I'll set up an IMAP account manually as you did and see what happens.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:4efa65d1-c169-4d25-8382-dead57f2d799...

    Hi Ken

    Correct. POP3 and MAPI profiles work well, IMAP profiles don't work.

    I've checked the indicated config item in Outlook 2010, and the selected item is "Download complete items including attachments for subscribed folders"

    Regards
    Hector


    Ken Slovak MVP - Outlook
    Tuesday, June 26, 2012 3:40 PM
  • Hi Ken
    Thank you very much for your time and effort

    Regards,

    Tuesday, June 26, 2012 4:19 PM
  • When you look at the Folder List in Outlook when using the IMAP profile, how many stores (PST files) do you see? I see 2 of them, one is the Gmail IMAP store.
     
    Using this Outlook VBA test code on a Gmail Inbox with 673 items in it produced a Count of 0:
     
    Sub testIMAP()
        Dim oFolder As Outlook.Folder
        Dim oItems As Outlook.Items
        
        Set oFolder = Application.Session.GetDefaultFolder(olFolderInbox)
        Set oItems = oFolder.Items
        MsgBox "Count = " & oItems.Count
    End Sub
     
    However, I figured out a workaround that works in my test on Outlook 2010, and this displays the correct Items count of 673, where Stores.Item(1) is the IMAP store:
     
    MsgBox Application.Session.Stores.Item(1).GetDefaultFolder(olFolderInbox).Items.Count
     
    Use explicit objects in your actual code, this hack with the compound dot operators and various shortcuts was just for testing, not what I'd actually use in production code.
     
    Let me know if this type of workaround works for you.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:612ef7d8-ef92-4172-9bb7-e2416857a6bb...

    Hi Ken
    Thank you very much for your time and effort

    Regards,


    Ken Slovak MVP - Outlook
    • Proposed as answer by Hector Romero Thursday, June 28, 2012 3:13 PM
    • Marked as answer by Hector_Romero Thursday, June 28, 2012 3:15 PM
    Thursday, June 28, 2012 1:32 PM
  • Hi Ken

    Thank you, thank you thank you.

    Works very well, moreover, it works well with IMAP profiles, MAPI profiles and Exchange profiles
    I owe you a beer ;)
    Thursday, June 28, 2012 3:13 PM
  • Excellent. I'm glad it worked for you.
     
    Who knows, I might need that hack someday for my own code :)

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "Hector Romero" <=?utf-8?B?SGVjdG9yIFJvbWVybw==?=> wrote in message news:0983c179-ccf1-497c-b9c5-3f2b59444d0f...

    Hi Ken

    Thank you, thank you thank you.

    Works very well, moreover, it works well with IMAP profiles, MAPI profiles and Exchange profiles
    I owe you a beer ;)

    Ken Slovak MVP - Outlook
    Thursday, June 28, 2012 3:27 PM