none
C# outlook inbox not refreshing with latest messages RRS feed

  • Question

  • Have a C# program running on a PC that will periodically connect to Outlook default profile to read the inbox for any mail with specific subject lines And attachments.  The search and attachment detection filter works just fine, the problem is the Outlook inbox isn't refreshing using the code that I have.  The mailbox itself is a shared mailbox for a group of people and our authentication to it is by logged in credentials.  Have tried a number of different approaches to forcing the Outlook mailbox to refresh, but none of them are working.

    Have tried this code to no effect:

    OutlookApplication._SyncObject _syncObject = null;
    _syncObject = olNS.SyncObjects[1];
    _syncObject.Start();
    System.Threading.Thread.Sleep(30000);
    _syncObject.Stop();
    _syncObject = null;
    

    And also the code below:

                OutlookApplication._Application olApp = new OutlookApplication.Application();
                OutlookApplication._NameSpace olNS = olApp.GetNamespace("MAPI");
                olNS.Logon(Missing.Value, Missing.Value, false, true);
                Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = olNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                OutlookApplication._Explorer oExp = myInbox.GetExplorer(false);
                // Get the Menu bar - then the other options so we can force a refresh of the mailbox
                Office._CommandBars oCmdBars = oExp.CommandBars;
                Office.CommandBar oCmdBar = oCmdBars["Menu Bar"];
                Office.CommandBarControls oBarCrls = oCmdBar.Controls;
                // Get the Tools menu.
                Office.CommandBarPopup oBPop = (Office.CommandBarPopup)oBarCrls["Tools"];
                oBarCrls = oBPop.Controls;
                // Get the Send/Receive menu.
                Office.CommandBarPopup oSendReceive = (Office.CommandBarPopup)oBarCrls["Send/Receive"];
                // Get the Send and Receive All menu.
                oBarCrls = oSendReceive.Controls;
                //olNS.SendAndReceive(false);
                string ocmd = "Send/Receive &All";
                Office.CommandBarControl oSendReceiveAll = (Office.CommandBarControl)oBarCrls[ocmd];
                // Do the action.
                oSendReceiveAll.Execute();

               And have also tried the statement:  olNS.SendAndReceive(false);   but to no effect. 

    Is there any known way to ensure that the latest messages from the inbox are pushed into the client inbox folder?  If I go into an interactive Outlook client and do the Send/Receive All or Update folder it will bring it into the clients view, and then my program can access it, however in real operation that would not be a feasible approach (program to be run on task scheduler under my normal credentials).

    Tuesday, April 16, 2013 6:30 PM

Answers

  • Try using the NameSpace.SendAndReceive(False) method.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "WmS" <=?utf-8?B?V21T?=> wrote in message news:cf971fca-466f-4ef8-aceb-8660649dd4e5...

    Have a C# program running on a PC that will periodically connect to Outlook default profile to read the inbox for any mail with specific subject lines And attachments.  The search and attachment detection filter works just fine, the problem is the Outlook inbox isn't refreshing using the code that I have.  The mailbox itself is a shared mailbox for a group of people and our authentication to it is by logged in credentials.  Have tried a number of different approaches to forcing the Outlook mailbox to refresh, but none of them are working.

    Have tried this code to no effect:

    OutlookApplication._SyncObject _syncObject = null;
    _syncObject = olNS.SyncObjects[1];
    _syncObject.Start();
    System.Threading.Thread.Sleep(30000);
    _syncObject.Stop();
    _syncObject = null;
    

    And also the code below:

                OutlookApplication._Application olApp = new OutlookApplication.Application();
                OutlookApplication._NameSpace olNS = olApp.GetNamespace("MAPI");
                olNS.Logon(Missing.Value, Missing.Value, false, true);
                Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = olNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                OutlookApplication._Explorer oExp = myInbox.GetExplorer(false);
                // Get the Menu bar - then the other options so we can force a refresh of the mailbox
                Office._CommandBars oCmdBars = oExp.CommandBars;
                Office.CommandBar oCmdBar = oCmdBars["Menu Bar"];
                Office.CommandBarControls oBarCrls = oCmdBar.Controls;
                // Get the Tools menu.
                Office.CommandBarPopup oBPop = (Office.CommandBarPopup)oBarCrls["Tools"];
                oBarCrls = oBPop.Controls;
                // Get the Send/Receive menu.
                Office.CommandBarPopup oSendReceive = (Office.CommandBarPopup)oBarCrls["Send/Receive"];
                // Get the Send and Receive All menu.
                oBarCrls = oSendReceive.Controls;
                //olNS.SendAndReceive(false);
                string ocmd = "Send/Receive &All";
                Office.CommandBarControl oSendReceiveAll = (Office.CommandBarControl)oBarCrls[ocmd];
                // Do the action.
                oSendReceiveAll.Execute();

               And have also tried the statement:  olNS.SendAndReceive(false);   but to no effect. 

    Is there any known way to ensure that the latest messages from the inbox are pushed into the client inbox folder?  If I go into an interactive Outlook client and do the Send/Receive All or Update folder it will bring it into the clients view, and then my program can access it, however in real operation that would not be a feasible approach (program to be run on task scheduler under my normal credentials).


    Ken Slovak MVP - Outlook
    Tuesday, April 16, 2013 7:35 PM
  • Hi Wms,

    You don't need to get "Send/Receive" command by using custom codes, There is existing method to update the latest mail in Outlook object model, you should use the NameSpace.SendAndReceive Method to update latest mail. you can get all objects of Namespace from here:

    NameSpace Object


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. my sample

    Wednesday, April 17, 2013 2:30 AM

All replies

  • Try using the NameSpace.SendAndReceive(False) method.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "WmS" <=?utf-8?B?V21T?=> wrote in message news:cf971fca-466f-4ef8-aceb-8660649dd4e5...

    Have a C# program running on a PC that will periodically connect to Outlook default profile to read the inbox for any mail with specific subject lines And attachments.  The search and attachment detection filter works just fine, the problem is the Outlook inbox isn't refreshing using the code that I have.  The mailbox itself is a shared mailbox for a group of people and our authentication to it is by logged in credentials.  Have tried a number of different approaches to forcing the Outlook mailbox to refresh, but none of them are working.

    Have tried this code to no effect:

    OutlookApplication._SyncObject _syncObject = null;
    _syncObject = olNS.SyncObjects[1];
    _syncObject.Start();
    System.Threading.Thread.Sleep(30000);
    _syncObject.Stop();
    _syncObject = null;
    

    And also the code below:

                OutlookApplication._Application olApp = new OutlookApplication.Application();
                OutlookApplication._NameSpace olNS = olApp.GetNamespace("MAPI");
                olNS.Logon(Missing.Value, Missing.Value, false, true);
                Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = olNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                OutlookApplication._Explorer oExp = myInbox.GetExplorer(false);
                // Get the Menu bar - then the other options so we can force a refresh of the mailbox
                Office._CommandBars oCmdBars = oExp.CommandBars;
                Office.CommandBar oCmdBar = oCmdBars["Menu Bar"];
                Office.CommandBarControls oBarCrls = oCmdBar.Controls;
                // Get the Tools menu.
                Office.CommandBarPopup oBPop = (Office.CommandBarPopup)oBarCrls["Tools"];
                oBarCrls = oBPop.Controls;
                // Get the Send/Receive menu.
                Office.CommandBarPopup oSendReceive = (Office.CommandBarPopup)oBarCrls["Send/Receive"];
                // Get the Send and Receive All menu.
                oBarCrls = oSendReceive.Controls;
                //olNS.SendAndReceive(false);
                string ocmd = "Send/Receive &All";
                Office.CommandBarControl oSendReceiveAll = (Office.CommandBarControl)oBarCrls[ocmd];
                // Do the action.
                oSendReceiveAll.Execute();

               And have also tried the statement:  olNS.SendAndReceive(false);   but to no effect. 

    Is there any known way to ensure that the latest messages from the inbox are pushed into the client inbox folder?  If I go into an interactive Outlook client and do the Send/Receive All or Update folder it will bring it into the clients view, and then my program can access it, however in real operation that would not be a feasible approach (program to be run on task scheduler under my normal credentials).


    Ken Slovak MVP - Outlook
    Tuesday, April 16, 2013 7:35 PM
  • Hi Wms,

    You don't need to get "Send/Receive" command by using custom codes, There is existing method to update the latest mail in Outlook object model, you should use the NameSpace.SendAndReceive Method to update latest mail. you can get all objects of Namespace from here:

    NameSpace Object


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. my sample

    Wednesday, April 17, 2013 2:30 AM