I need to validate if a new incoming Mail-Item is signed on Outlook 2010. If an Mail-Item is not signed, it should be moved into a "NOSIG"-folder.
At first I have to say that I have little to no clue about VBA and had to google a lot for that little something of code. While researching, I also found (and sort of confirmed, I guess) that Outlook 2010 appears to modify the MessageClass to always be "IPM.Note", so I tried to use the PropertyAccessor and read the Security-Flags.
It seemed to work, but it doesn't for real... here's my code so far:
Sub TRCR(MAIL_ITEM As MailItem)
Dim PR_SECURITY_FLAGS As Integer
On Error Resume Next
'Security-Flags: 0=none, 1=encrypted, 2=signed, 3=both
PR_SECURITY_FLAGS = MAIL_ITEM.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x6E010003")
'Modulo because, sometimes the flags value is added to a multiple of 32... <unfortunately I lost the source>
If (PR_SECURITY_FLAGS > 32) Then PR_SECURITY_FLAGS = PR_SECURITY_FLAGS Mod 32
If PR_SECURITY_FLAGS = 2 Or PR_SECURITY_FLAGS = 3 Then
'Do all that fancy stuff I want to with that signed Mail
Else
MAIL_ITEM.Move Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders.Item("NOSIG")
End If
End Sub
I use an Outlook-Rule to run that Script on every incoming E-Mail.
When I started testing, it appeared that it "usually" works, but unfortunately sometimes it moves even signed Mails to the NOSIG folder.
In that cases, the PR_SECURITY_FLAGS were both at 0, before and after that Modulo-Codeline. So being 0, the Script worked right but since the Mail was actually signed, the Flag shouldn't have been 0 but 2.
I tried to resend the very same signed mail dozen of times, just to always see the same happening: most of them are treated correctly while few always appeared to show the flag 0 instead of 2 while they still were signed.
I also tried to pause the Script for 1-5 seconds with Application.Wait Now + TimeSerial(0, 0, 1)
thinking that the Script may be too fast for the PropertyAccessor or something, but it seemed that the pause didn't even work. (I couldn't "feel" that 5 seconds delay while processing multiple mails.)
Eventually I start to think that it is an Outlook-Problem (maybe manipulating Security-Flags similar to MessageClass but not everytime... yeah, no), but hopefully anybody out there got an solution for an similar use case.
Thanks for your time and have a nice day! :-)