Prevent emails from being deleted in one specific child folder of the Outlook inbox

时间:2015-10-16 02:13:14

标签: c# visual-studio outlook-addin outlook-2010 outlook-redemption

I'm attempting to figure out how to restrict one folder in Outlook 2010 from having it's mailitems deleted. I have the following code example that works well but only works on the inbox folder(OlDefaultFolders.olFolderInbox). I'm trying to figure out how to restrict one folder and one folder only below the inbox. For example Inbox\ReadMail where I want to prevent users from deleting from ReadMail only. Thank you in advance for any assistance.

 public partial class ThisAddIn
{
    Microsoft.Office.Interop.Outlook.MailItem mail = null;
    Outlook.Inspectors inspectors = null;
    Outlook.Folder fldr = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        inspectors = this.Application.Inspectors;

        // Is there a way to edit the folloing line to point to a certain sub folder of the inbox folder?
        inspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler (Inspectors_NewInspector);

        fldr = (Outlook.Folder)this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

        fldr.BeforeItemMove += new Microsoft.Office.Interop.Outlook.MAPIFolderEvents_12_BeforeItemMoveEventHandler(fldr_BeforeItemMove);

    }

    void fldr_BeforeItemMove(object Item, Microsoft.Office.Interop.Outlook.MAPIFolder MoveTo, ref bool Cancel)
    {
        MessageBox.Show("You are not permitted to delete emails from this folder");
        Cancel = true;
    }

1 个答案:

答案 0 :(得分:2)

替换

fldr = (Outlook.Folder)this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

fldr = (Outlook.Folder)this.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox).Folders["ReadMail"];