How to check if an Outlook mailItem is encrypted in a timely fashion?

I want to check if a mailItem with Outlook 2007 is encrypted or not.

I am pretty sure "THE" way of doing this would come from  Microsofts code here.

   
Dim oProp As Long
      oProp = CLng(Item.PropertyAccessor.GetProperty(PR_SECURITY_FLAGS))

Open in new window


The thing is that this code takes seconds to process and given that I am using outlook I must do it on the UI thread meaning everything is locked up.

I have tried accessing a property of the mailitem such as subject which will throw an error if the email is encrypted, but this also takes seconds to throw the error.

So is there some property, something else in the Outlook 2007 object model that will throw an exception if the mailItem is encrypted, but not lock up for seconds?

In case you are wondering what I want to do. I am trying to add a new Context button when the ItemContextMenuDisplay event is called and this button depends on whether the item is encrypted or not.
LVL 20
darbid73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy DownsOWNERCommented:
Maybe you could adapt this macro. he was getting an error when he tried to copy.  Perhaps you could copy with VB.Net & trap the error for encrypted messages.  Maybe it's faster than your current method & won't lock up.

macro fails at
"Item.UnRead = False". I get a message that says "Run-time error
'-2147217660 (80040F04)':" / "Method 'UnRead' of object 'MailItem' failed - See more at: http://www.knowoutlook.com/view/206201-error-with-item-unread-false-for-encrypted-messages.html#sthash.6zuKmgyS.dpuf

Public WithEvents myOlItems2 As Outlook.Items

Public Sub Application_Startup()
Set myOlItems2 = Outlook.Session.Folders("Personal
Folders").Folders("Inbox").Folders("Sent (2007)").Items
End Sub

Public Sub myOlItems2_ItemAdd(ByVal Item As Object)

Dim myOlAppSub As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim mySentFolder As Outlook.MAPIFolder

Set myOlAppSub = CreateObject("Outlook.Application")
Set myNameSpace = myOlAppSub.GetNamespace("MAPI")
Set mySentFolder = myNameSpace.Folders("Personal
Folders").Folders("Inbox").Folders("Sent (2007)")

Item.UnRead = False
End Sub - See more at: http://www.knowoutlook.com/view/206201-error-with-item-unread-false-for-encrypted-messages.html#sthash.6zuKmgyS.dpuf

Open in new window

0
darbid73Author Commented:
Thanks for the reply. The Unread property of a mailItem can be read on any email irrespective of encryption status.  So there is no error to catch.  

The guy in the post is in a world of confusion. Not sure why he needs all those objects in the ItemAdd method.  My guess why he is getting an error is because "item" can be any Outlook item but the property Unread might not be a property on all Outlook items.  I noticed no body answered the real forum question (not knowoutlook which is some scraper of forums) but my guess is that he needs to check the item type/class is a mailitem first.
0
Randy DownsOWNERCommented:
How about this? It's VBA but you might detect Mime types with .Net if the approach is valid.

If Item.MessageClass = "IPM.Note.SMIME" or you can check the named
property ContentType which would begin with something like
"application/x-pkcs7-mime". You would need to use CDO or Extended MAPI
or Redemption to get at the named property though.


.Net 4
public static class MimeExtensionHelper
    {
        static object locker = new object();
        static object mimeMapping;
        static MethodInfo getMimeMappingMethodInfo;

        static MimeExtensionHelper()
        {
            Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
            if (mimeMappingType == null)
                throw new SystemException("Couldnt find MimeMapping type");            
            getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
            if (getMimeMappingMethodInfo == null)
                throw new SystemException("Couldnt find GetMimeMapping method");
            if (getMimeMappingMethodInfo.ReturnType != typeof(string))
                throw new SystemException("GetMimeMapping method has invalid return type");
            if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
                throw new SystemException("GetMimeMapping method has invalid parameters");
        }
        public static string GetMimeType(string filename)
        {
            lock (locker)
                return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
darbid73Author Commented:
The solution is to check the MessageClass. In my case an encrypted email is "IPM.Note.SMIME"

But this is only a solution for Office 2007 / 2010.  This is not shown in 2003 and I have not tested it on 2013.

Thanks for making me check this again. I had done this during office 2003 but had forgotten to try it again now for 2007 / 2010.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.