I\'m using C# with Outlook Object Model (Redemption is not an option for me due to licensing) and I\'m having difficulty programmatically encrypting an email message before send
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);
}