none
how to access other folder which has email filtered from rules except inbox under email account? RRS feed

  • Question

  • Outlook.MAPIFolder subfolder = oInbox.Folders["Cloud"];

    this code has error , when debug Folders.Count = 0

    int count = 0;
                    try
                    {
                        foreach (Object oMsg3 in oInbox.Items)
                        {
                            try
                            {
                                if (oMsg3 is Outlook.MailItem)
                                {
                                    Outlook.MailItem oMsg2 = (Outlook.MailItem)oMsg3;
                                    if (oMsg2.ReceivedTime.Year == DateTime.Now.Year && oMsg2.ReceivedTime.Month == DateTime.Now.Month && (oMsg2.ReceivedTime.Day <= DateTime.Now.Day && oMsg2.ReceivedTime.Day >= DateTime.Now.Day - 3))
                                    {
                                        Console.WriteLine(oMsg2.ReceivedTime.Day.ToString() + "," + oMsg2.Subject);
                                        //Console.WriteLine(oMsg.SenderName);
                                        //Console.WriteLine(oMsg.ReceivedTime);
                                        //Console.WriteLine(oMsg.Body);
                                        if (oMsg2.Subject.ToLower().Contains("registration"))
                                        {
                                            Console.Write("****************************************");
                                        }
                                    }
                                    //MyMailHandler((Outlook.MailItem)_obj);
                                }
                            }
                            catch(System.Exception ex2)
                            {
                                Console.WriteLine(ex2.Message);
                            }
                        }
                    }
                    catch(System.Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    for (int i=0; i < 500; ++i)
                    {
                        try
                        {
                            if (oMsg.ReceivedTime.Year == DateTime.Now.Year && oMsg.ReceivedTime.Month == DateTime.Now.Month && (oMsg.ReceivedTime.Day <= DateTime.Now.Day && oMsg.ReceivedTime.Day >= DateTime.Now.Day - 3))
                            {
                                Console.WriteLine(oMsg.ReceivedTime.Day.ToString() + "," + oMsg.Subject);
                                //Console.WriteLine(oMsg.SenderName);
                                //Console.WriteLine(oMsg.ReceivedTime);
                                //Console.WriteLine(oMsg.Body);
                                if (oMsg.Subject.ToLower().Contains("registration"))
                                {
                                    Console.Write("****************************************");
                                }
                            }
                            oMsg = (Outlook.MailItem)oItems.GetNext();
                        }
                        catch(System.Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
    


    MEALY

    Tuesday, April 11, 2017 5:47 PM

Answers

All replies

  • Hello,

    You need to check whether a folder has child folders first:

    Outlook.Folders childFolders = folder.Folders;
    if (childFolders.Count > 0)
    {
       //
    }

    See How to: Enumerate Folders for more information.

    Also you need to use the Find/FindNext or Restrict methods of the Items class if you need to get items that correspond to your conditions. Read more about these methods in the following articles:

    How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)

    How To: Use Restrict method to retrieve Outlook mail items from a folder

    Also you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:

     - The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
     - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
     - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
     - You can stop the search process at any moment using the Stop method of the Search class.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, April 11, 2017 6:46 PM
  • I edit the example from official site.

    but there is no subfolder,

    just expect to access Cloud folder

    /********************************** Module Header **********************************\
    * Module Name:  Program.cs
    * Project:      CSAutomateOutlook
    * Copyright (c) Microsoft Corporation.
    * 
    * The CSAutomateOutlook example demonstrates the use of Visual C# code to automate 
    * Microsoft Outlook to log on with your profile, enumerate contacts, send a mail, log 
    * off, close the Microsoft Outlook application and then clean up unmanaged COM 
    * resources. 
    * 
    * This source is subject to the Microsoft Public License.
    * See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
    * All other rights reserved.
    * 
    * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
    * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
    * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
    \***********************************************************************************/
    
    #region Using directives
    using System;
    using System.Collections;
    using System.Runtime.InteropServices;
    using Outlook = Microsoft.Office.Interop.Outlook;
    #endregion
    using System.Speech.Recognition;
    using System.Threading;
    
    
    namespace CSAutomateOutlook
    {
        class Program
        {
            [STAThread]
            static void Main(string[] args)
            {
                AutomateOutlook();
    
    
                // Clean up the unmanaged Outlook COM resources by forcing a garbage 
                // collection as soon as the calling function is off the stack (at which 
                // point these objects are no longer rooted).
    
                GC.Collect();
                GC.WaitForPendingFinalizers();
                // GC needs to be called twice in order to get the Finalizers called - 
                // the first time in, it simply makes a list of what is to be finalized, 
                // the second time in, it actually is finalizing. Only then will the 
                // object do its automatic ReleaseComObject.
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            
    
            static void AutomateOutlook()
            {
                object missing = Type.Missing;
    
                Outlook.Application oOutlook = null;
                Outlook.NameSpace oNS = null;
                Outlook.Folder oCtFolder = null;
                Outlook.Items oCts = null;
                Outlook.MailItem oMail = null;
    
                try
                {
                    // Start Microsoft Outlook and log on with your profile.
    
                    // Create an Outlook application.
                    oOutlook = new Outlook.Application();
                    Console.WriteLine("Outlook.Application is started");
    
                    Console.WriteLine("User logs on ...");
    
                    // Get the namespace.
                    oNS = oOutlook.GetNamespace("MAPI");
    
                    // Log on by using a dialog box to choose the profile.
                    //oNS.Logon(missing, missing, true, true);
                    oNS.Logon("a@a.com", "*****", false, true);
    
    
                    // Alternative logon method that uses a specific profile.
                    // If you use this logon method, change the profile name to an 
                    // appropriate value. The second parameter of Logon is the password 
                    // (if any) associated with the profile. This parameter exists only 
                    // for backwards compatibility and for security reasons, and it is 
                    // not recommended for use.
                    //oNS.Logon("YourValidProfile", missing, false, true);
    
                    Console.WriteLine("Press ENTER to continue when Outlook is ready.");
                    Console.ReadLine();
    
                    // Enumerate the contact items.
    
                    Console.WriteLine("Enumerate the contact items");
                    //Outlook.Recipient myrecipient = oNS.CreateRecipient("Cloud");
                    //myrecipient.Resolve();
                    Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                    //Outlook.MAPIFolder oInbox = oNS.GetFolderFromID(oNS.Folders["Cloud"].Items);
    
    
                    //Outlook.MAPIFolder oInbox = oNS.GetSharedDefaultFolder(myrecipient, Outlook.OlDefaultFolders.olFolderInbox);
                    //Outlook.MAPIFolder subfolder = oInbox.Folders["Cloud"];
                    Outlook.Items oItems = oInbox.Items;
                    //Outlook.Items oItems = oNS.Folders["Cloud"].Items;
                    Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst();
                    //Output some common properties.
                    //Console.WriteLine(oMsg.Subject);
                    //Console.WriteLine(oMsg.SenderName);
                    //Console.WriteLine(oMsg.ReceivedTime);
                    //Console.WriteLine(oMsg.Body);
                    //Outlook.MailItem msgitem = (Outlook.MailItem)oItems.GetFirst();
                    
                    int count = 0;
                    try
                    {
                        foreach (Object oMsg3 in oInbox.Items)
                        {
                            try
                            {
                                if (oMsg3 is Outlook.MailItem)
                                {
                                    Outlook.MailItem oMsg2 = (Outlook.MailItem)oMsg3;
                                    if (oMsg2.ReceivedTime.Year == DateTime.Now.Year && oMsg2.ReceivedTime.Month == DateTime.Now.Month && (oMsg2.ReceivedTime.Day <= DateTime.Now.Day && oMsg2.ReceivedTime.Day >= DateTime.Now.Day - 3))
                                    {
                                        Console.WriteLine(oMsg2.ReceivedTime.Day.ToString() + "," + oMsg2.Subject);
                                        //Console.WriteLine(oMsg.SenderName);
                                        //Console.WriteLine(oMsg.ReceivedTime);
                                        //Console.WriteLine(oMsg.Body);
                                        if (oMsg2.Subject.ToLower().Contains("registration"))
                                        {
                                            Console.Write("****************************************");
                                        }
                                    }
                                    //MyMailHandler((Outlook.MailItem)_obj);
                                }
                            }
                            catch(System.Exception ex2)
                            {
                                Console.WriteLine(ex2.Message);
                            }
                        }
                    }
                    catch(System.Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    for (int i=0; i < 500; ++i)
                    {
                        try
                        {
                            if (oMsg.ReceivedTime.Year == DateTime.Now.Year && oMsg.ReceivedTime.Month == DateTime.Now.Month && (oMsg.ReceivedTime.Day <= DateTime.Now.Day && oMsg.ReceivedTime.Day >= DateTime.Now.Day - 3))
                            {
                                Console.WriteLine(oMsg.ReceivedTime.Day.ToString() + "," + oMsg.Subject);
                                //Console.WriteLine(oMsg.SenderName);
                                //Console.WriteLine(oMsg.ReceivedTime);
                                //Console.WriteLine(oMsg.Body);
                                if (oMsg.Subject.ToLower().Contains("registration"))
                                {
                                    Console.Write("****************************************");
                                }
                            }
                            oMsg = (Outlook.MailItem)oItems.GetNext();
                        }
                        catch(System.Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                    
                    Console.Write("");
                    
    
                    oCtFolder = (Outlook.Folder)oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                    oCts = oCtFolder.Items;
    
                    // Enumerate the contact items. Be careful with foreach loops. 
                    // See: http://tiny.cc/uXw8S.
                    for (int i = 1; i <= oCts.Count; i++)
                    {
                        object oItem = oCts[i];
    
                        if (oItem is Outlook.ContactItem)
                        {
                            Outlook.ContactItem oCt = (Outlook.ContactItem)oItem;
                            Console.WriteLine(oCt.Email1Address);
                            // Do not need to Marshal.ReleaseComObject oCt because 
                            // (Outlook.ContactItem)oItem is a simple .NET type 
                            // casting, instead of a COM QueryInterface.
                        }
                        else if (oItem is Outlook.DistListItem)
                        {
                            Outlook.DistListItem oDl = (Outlook.DistListItem)oItem;
                            Console.WriteLine(oDl.DLName);
                            // Do not need to Marshal.ReleaseComObject oDl because 
                            // (Outlook.DistListItem)oItem is a simple .NET type 
                            // casting, instead of a COM QueryInterface.
                        }
    
                        // Release the COM object of the Outlook item.
                        Marshal.FinalReleaseComObject(oItem);
                        oItem = null;
                    }
                    
                    // Create and send a new mail item.
                    /*
                    Console.WriteLine("Create and send a new mail item");
    
                    oMail = (Outlook.MailItem)oOutlook.CreateItem(
                        Outlook.OlItemType.olMailItem);
    
                    // Set the properties of the email.
                    oMail.Subject = "Feedback of All-In-One Code Framework";
                    oMail.To = "a@a.com";
                    oMail.HTMLBody = "<b>Feedback:</b><br />";
    
                    // Displays a new Inspector object for the item and allows users to 
                    // click on the Send button to send the mail manually.
                    // Modal = true makes the Inspector window modal
                    oMail.Display(true);
                    */
                    // [-or-]
                    // Automatically send the mail without a new Inspector window.
                    //((Outlook._MailItem)oMail).Send();
    
                    // User logs off and quits Outlook.
    
                    Console.WriteLine("Log off and quit the Outlook application");
                    oNS.Logoff();
                    ((Outlook._Application)oOutlook).Quit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("AutomateOutlook throws the error: {0}", ex.Message);
                }
                finally
                {
                    // Manually clean up the explicit unmanaged Outlook COM resources by  
                    // calling Marshal.FinalReleaseComObject on all accessor objects. 
                    // See http://support.microsoft.com/kb/317109.
    
                    if (oMail != null)
                    {
                        Marshal.FinalReleaseComObject(oMail);
                        oMail = null;
                    }
                    if (oCts != null)
                    {
                        Marshal.FinalReleaseComObject(oCts);
                        oCts = null;
                    }
                    if (oCtFolder != null)
                    {
                        Marshal.FinalReleaseComObject(oCtFolder);
                        oCtFolder = null;
                    }
                    if (oNS != null)
                    {
                        Marshal.FinalReleaseComObject(oNS);
                        oNS = null;
                    }
                    if (oOutlook != null)
                    {
                        Marshal.FinalReleaseComObject(oOutlook);
                        oOutlook = null;
                    }
                }
            }
        }
    }


    MEALY



    Tuesday, April 11, 2017 7:08 PM
  • Try to use any property explorer utility such as MFCMAPI or OutlookSpy. At runtime you can explore the folder structure and see what steps should be done to get the folder.

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    Tuesday, April 11, 2017 10:19 PM
  • Hello,

    I think you need to specify the user when access the folder.

    E.g.

    Outlook.MAPIFolder Couldbox = oNS.Folders["a@a.com"].Folders["Cloud"] 

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 12, 2017 9:44 AM
    Moderator
  • i solved by move filtered folders to under Inbox

    so make inbox has subfolders


    MEALY

    Monday, April 17, 2017 5:45 AM