Programmatically encrypt Outlook email using Inspector

旧时模样 提交于 2019-12-02 02:21:23

There's actually a better way to programmatically encrypt, sign, encrypt + sign, or ensure neither. And you can do it without having to display the mail item. The following article shows how, using a property of the mail item:

http://support.microsoft.com/kb/2636465?wa=wsignin1.0

For example, in C#, if mItem is your mail item, then the following code would turn off signing and encryption:

mItem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x6E010003", 0);

Figured it out by trial and error. The main problem seemed to be that I was using the Inspector before I had displayed the MailItem. Adding the call to Display at the beginning solved it. For anyone interested, here is the code that worked for me:

private static void addOutlookEncryption(ref Outlook.MailItem mItem) {
        CommandBarButton encryptBtn;
        mItem.Display(false);
        encryptBtn = mItem.GetInspector.CommandBars.FindControl(MsoControlType.msoControlButton, 718, Type.Missing, Type.Missing) as CommandBarButton;
        if (encryptBtn == null) {
            //if it's null, then add the encryption button
            encryptBtn = (CommandBarButton)mItem.GetInspector.CommandBars["Standard"].Controls.Add(Type.Missing, 718, Type.Missing, Type.Missing, true);
        }
        if (encryptBtn.Enabled) {
            if (encryptBtn.State == MsoButtonState.msoButtonUp) {
                encryptBtn.Execute();
            }
        }
        mItem.Close(Outlook.OlInspectorClose.olDiscard);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!