none
Detect filetype in Attachments and ouput MsgBox to user, forcing encryption (PII Prevention, Data Loss Protection) RRS feed

  • Question

  • Working on a project to push to a group of folks that accomplishes the following:

    1.) Detects if the user has an attachment, detect what type of attachment and warn the user that that particular filetype could consist of Personally Identifiable Information and force them to encrypt.

         b.) Possibly even flag  certain keyword in those filetypes to be dangerous and once again MsgBox (or custom Form) to user making them encrypt or try again.

    2.) Detect if a SSN or other flagged keywords were detected in either the Subject line and/or the Body of the message. If found once again, notify the user that the following SSN has been detected and to either purge the information or encrypt.

    The following got us to be able to detect that there was an attachment to begin with, but this didn't allow us to detect what type of file or be able to detect flagged strings in the filename to warn the user of. As far as detecting SSN in either the Body or Subject line, I've got that down. But to implement detecting the file type and possibly searching that filename for certain keywords has me at a loss. I'd appreciate any help! Thanks!

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean) Dim newMail As Outlook.MailItem Dim recip As Outlook.Recipient Dim isExternal As Boolean Dim Msg As Outlook.MailItem Dim m As Variant, em As Variant Dim strBody As String Dim intIn As Long Dim intAttachCount As Integer, intStandardAttachCount As Integer On Error GoTo handleError 'for ssMacro Dim hforewnd As Long Dim x As Long Dim myOlExp As Outlook.Explorer Dim myOlExps As Outlook.Explorers Set myOlExps = Application.Explorers Dim aryStates(1000) As Long Dim itm As Outlook.MailItem Dim vResp As Variant Dim prompt As String

    intStandardAttachCount = 0 strBody = LCase(Item.Body) intIn = InStr(1, strBody, "original message") If intIn = 0 Then intIn = Len(strBody) intIn = InStr(1, Left(strBody, intIn), "attach") intAttachCount = Item.Attachments.Count If intIn > 0 And intAttachCount <= intStandardAttachCount Then m = MsgBox("It appears that you mean to send an attachment," & vbCrLf & "but there is no attachment to this message." & vbCrLf & vbCrLf & "Do you still want to send?", vbQuestion + vbYesNo + vbMsgBoxSetForeground) If m = vbNo Then Cancel = True End If handleError: If Err.Number <> 0 Then MsgBox "Outlook Attachment Reminder Error: " & Err.Description, vbExclamation, "Outlook Attachment Reminder Error" End If If IsMail(Item) Then Set Msg = Item Else ' skip processing Exit Sub End If If Item.Class = olMail Then Set newMail = Item For Each recip In newMail.Recipients If UCase(recip.AddressEntry.Type) = "SMTP" Then isExternal = True Exit For End If Next If isExternal And Msg.Attachments.Count > intStandardAttachCount Then em = MsgBox("You are sending an attachment to an outside email address" & vbCrLf & "Do you want to encrypt this message?" & vbCrLf & vbCrLf & "Click YES to stop sending" & vbCrLf & "If already encrypted or don't need to, click NO to send", vbQuestions + vbYesNo + vbMsgBoxSetForeground) If em = vbYes Then Cancel = True End If End If If isIternal And Msg.Attachments.Count > intStandardAttachCount Then em = MsgBox("You are sending an attachment to an internal email address" & vbCrLf & "Do you want to encrypt this message?" & vbCrLf & vbCrLf & "Click YES to stop sending" & vbCrLf & "If already encrypted or don't need to, click NO to send", vbQuestions + vbYesNo + vbMsgBoxSetForeground) If em = vbYes Then Cancel = True End If Set newMail = Nothing Set recip = Nothing If ufnCheckRegEx(Item.Subject, prompt) Or ufnCheckRegEx(Item.Body, prompt) Then prompt = prompt & vbCrLf & "Are you sure you want to send it?" If MsgBox(prompt, vbYesNo + vbQuestion, "Social Security Warning") = vbNo Then Cancel = True End If End If End Sub Function IsMail(ByVal itm As Object) As Boolean IsMail = (TypeName(itm) = "MailItem") End Function Function ufnCheckRegEx(ByVal str As String, ByRef RetStr As String) As Boolean Dim objRE As New RegExp Dim colMatches As MatchCollection Dim objMatch As Match objRE.Global = True objRE.IgnoreCase = True objRE.MultiLine = True Dim lngCount As Long objRE.Pattern = "(\b[0-8][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]\b)|(\b[0-8][0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]\b)|(\b[0-8][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\b)" If objRE.test(str) = True Then Set colMatches = objRE.Execute(str) RetStr = "The subject or body may contain PII Information:" & vbCrLf For Each objMatch In colMatches If lngCount >= 20 Then RetStr = RetStr & vbCrLf & "Note: There may be too many to include in this warning." Set objRE = Nothing ufnCheckRegEx = True Exit Function End If RetStr = RetStr & objMatch.Value & vbCrLf lngCount = lngCount + 1 Next ufnCheckRegEx = True Else ufnCheckRegEx = False End If Set objRE = Nothing End Function



    Monday, December 9, 2013 10:27 PM

Answers

  • Hi,

    According to your description, you want to detect the type of file and the flagged strings in the filename.

    There is no property of Attachment Object to get the extension of attachment document directly. But you could use the FileName Property of the Attachment Object to get a string representing the file name of the attachment. By munipulating the string of the file name, you can get the extension of the attachment and the flagged strings.

    Here is a sample for your reference.

    Dim attachments As Outlook.Attachments
    Dim attachment As Outlook.Attachment
    Dim strpath As String
    
    attachments = Item.Attachments
    If Item.Attachments.Count > 0 Then
        For Each attachment In attachments
            strpath = attachment.FileName
        Next
    End If
    

    About more properties and methods of the Attachment Object, you could refer to: Attachment Object


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 10, 2013 1:13 PM
    Moderator
  • Hello letsrollusafa11,

    The MailItem class from the Outlook Object Model provides the Attachments property. The property returns an instance of the Attachments class. It contains all the attachments of the e-mail.  The Count property will return the number of existing attachments. You can use the Item method to get a single attachment by the index. The Attachment class provides the FileName property which can be used for checking the file type.

    Unfortunately the OOM doesn't provide any methods or properties for searching inside the attached files. As a workaround you can save the file to disk (see the SaveAsFile method of the Attachment class) and scan it there. Using the PropertyAccessor class you can get the job done at runtime in memory (not saving the file to the disk).

    Tuesday, December 10, 2013 1:23 PM

All replies

  • Hi,

    According to your description, you want to detect the type of file and the flagged strings in the filename.

    There is no property of Attachment Object to get the extension of attachment document directly. But you could use the FileName Property of the Attachment Object to get a string representing the file name of the attachment. By munipulating the string of the file name, you can get the extension of the attachment and the flagged strings.

    Here is a sample for your reference.

    Dim attachments As Outlook.Attachments
    Dim attachment As Outlook.Attachment
    Dim strpath As String
    
    attachments = Item.Attachments
    If Item.Attachments.Count > 0 Then
        For Each attachment In attachments
            strpath = attachment.FileName
        Next
    End If
    

    About more properties and methods of the Attachment Object, you could refer to: Attachment Object


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 10, 2013 1:13 PM
    Moderator
  • Hello letsrollusafa11,

    The MailItem class from the Outlook Object Model provides the Attachments property. The property returns an instance of the Attachments class. It contains all the attachments of the e-mail.  The Count property will return the number of existing attachments. You can use the Item method to get a single attachment by the index. The Attachment class provides the FileName property which can be used for checking the file type.

    Unfortunately the OOM doesn't provide any methods or properties for searching inside the attached files. As a workaround you can save the file to disk (see the SaveAsFile method of the Attachment class) and scan it there. Using the PropertyAccessor class you can get the job done at runtime in memory (not saving the file to the disk).

    Tuesday, December 10, 2013 1:23 PM
  • In some cases PR_ATTACH_MIME_TAG can be used to try to deduce the file type. For example it might have a value of something like "application/msword". That property on a attachment can be accessed using PropertyAccessor and a DASL property tag of "http://schemas.microsoft.com/mapi/proptag/0x370E001F"

    Ken Slovak MVP - Outlook

    Tuesday, December 10, 2013 3:16 PM
    Moderator