none
How can i encrypt an Office.Interop:Outlook.MailItem in C# RRS feed

  • Question

  • Hello,

    I'm working on a WPF-app with VS2015 and C# in which i can send emails in code-behind.

    Now there is a new request that these emails shall be encrypted but not signed.

    I tried two ways but without success.

    First try: Using the command button of the MailItem for encryption

            private void AddOutlookEncryption(ref Microsoft.Office.Interop.Outlook.MailItem mailItem)
            {
                try
                {
                    CommandBarButton encryptBtn;
                    mailItem.Display(false);
                    encryptBtn = mailItem.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)mailItem.GetInspector.CommandBars["Standard"].Controls.Add(Type.Missing, 718, Type.Missing, Type.Missing, true);
                    }
    
                    encryptBtn.Enabled = true;
    
                    if (encryptBtn.Enabled)
                    {
                        if (encryptBtn.State == MsoButtonState.msoButtonUp)
                        {
                            encryptBtn.Execute();
                        }
                    }
    
                    mailItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olDiscard);
                }
                catch (Exception ex)
                {
                }
            }
    


    In that case i find the correct button but it's disabled and my hardcoded enabling cannot change that.

    Second try: Encrypting the mail by using the MailItem's PropertyAccessor

    // Tries to encrypt the mail
    const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
    long oProp;
    oProp = Convert.ToInt64(mailItem.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS));
    //' You can use this to verify the property's flag compared to
    //' what was set in Outlook.
    string ulFlags = "0";
    ulFlags = "\x1"; //&H1 Add the encrypted flag
    //ulFlags = ulFlags Or & H2 //Add the signed flag
    mailItem.PropertyAccessor.SetProperty(PR_SECURITY_FLAGS, ulFlags);
    


    That code ends at the SetProperty-method with an error.

    Type conflict. (Exception of HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))

    ErrorCode -2147352571

    HResult -2147352571

    What have i done wrong?

    Thanks in advance for any help!


    Mit freundlichen Grüßen, Patrick Pirzer

    Wednesday, February 28, 2018 1:45 PM

Answers

All replies

  • Hello,

    I know that article.

    It was the base for my own C#-code.

    Can You explain me please, why i'm getting a type conflict when i execute the code sample from my second try?


    Mit freundlichen Grüßen, Patrick Pirzer

    Wednesday, February 28, 2018 2:16 PM
  • The code is passing a string value to set the property.  That particular property requires a 32 bit integer value.  That is indicated by the "0003" in the mapi property tag.  See Property Types
    Wednesday, February 28, 2018 3:01 PM
  • So what to do with a code? 
    Wednesday, February 28, 2018 3:19 PM
  • Hello RLWA32.

    Thanks a lot for Your hint!

    Now i was able to correct my code.

    const string PR_SECURITY_FLAGS = "http://schemas.microsoft.com/mapi/proptag/0x6E010003";
    long prop = Convert.ToInt64(mailItem.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS));
    var ulFlags = 0x0;
    ulFlags = (ulFlags | 0x1); //  SECFLAG_ENCRYPTED
    //ulFlags = (ulFlags | 0x2); //  SECFLAG_SIGNED
    mailItem.PropertyAccessor.SetProperty(PR_SECURITY_FLAGS, ulFlags);
    

    The code works, but now i need a new certificate because mine is out of time.

    But that's another story.


    Mit freundlichen Grüßen, Patrick Pirzer

    Wednesday, February 28, 2018 3:35 PM