0
votes

We are building a VSTO Outlook Add-In that scans an outgoing mail message for attachments to alert users and noticed some unexpected behavior.

Considering the following ways of adding a file to an Outlook mail message:

  1. A file attachment
  2. Screen Shot
  3. Screen Clipping
  4. Mail Signature file

All four are recognized as attachments when the Item Send event fires:

  Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend

In the following code example:

For Each attachment As Outlook.Attachment In Item.Attachments
'do some stuff like check attachment size 
Next

We are checking for small embedded images in a signature file that don't want to notify the user of.

In the following cases:

  • Screen Shot
  • Screen Clipping
  • Mail Signature file

We've noticed that when the added files are embedded images (not attachments), we don't see a correct size property for the image using:

attachment.Size

IE: Say we are sending an Outlook email that has:

  • One Attachment.
  • One Screen Shot.
  • One Signature File with one image in it.

Our code seems to recognize the correct number of attachments, however if we check the attachment size for a screen shot or a signature file image, the attachment size property always evaluates to 0, which we're think is due to the fact that the file doesn't exist on disk and the attached file does.

For Each attachment As Outlook.Attachment In Item.Attachments
   if attachment.size > 755 then
       'ignore the image
   end if
Next

Is there a way to check the image size in VB.Net or do we need to save the file off to a temp directory in order to do this?

EDIT Outlook Spy Troubleshooting steps:

  1. New Mail Message
  2. Inserted screen shot and signature file:

enter image description here

  1. OutlookSpy->IMessage

  2. IMessage window blank (below)

Step 4

  1. Close IMessage window.

  2. Re-Open IMessage Window

  3. Inserted (attached) files appear (below)

Step 7 8. Double Clicked on attachment

Double Click attach/size

  1. Selected Inspector Button

Inspector

  1. Current Item:

Current Item

  1. Browse:

Browse

  1. Attachments:

Attachments

  1. Browse:

Browse 2

  1. IEnumVariant:

enter image description here

I suspect that the differences between steps 4 and 7 may be due to the fact that Outlook may have saved a draft of the email message?

ADDITIONAL EDIT

Code added to save mail message before checking signature/embedded image size:

'convert generic object to Outlook.MailItem object.
Dim objMailItem As Outlook.MailItem = CType(Item, Outlook.MailItem)
'Save message
objMailItem.Save()
'quick check to see if message is saved (it is)
Dim saved As Boolean = objMailItem.Saved()

For Each attachment As Outlook.Attachment In objMailItem.Attachments
    'all items still evaluate to 0.
    If attachment.Size >= 20 Then
        'do some stuff
    End If
Next

Thanks.

2

2 Answers

1
votes

Attachment size (which includes raw binary data as well as the per-attachment properties) is updated by the store provider when the mesage is saved. Make sure you call MailItem.Save first.

0
votes

We ended up using Outlook Spy (awesome tool..) to find the PR_ATTACH_SIZE property:

Then set up using MS schema as follows:

'property access to get attachment sizes
 Const PR_ATTACH_SIZE As String = "http://schemas.microsoft.com/mapi/proptag/0x0E200003"

Also great info from this SO Post.

Then iterated through our attachment collection as follows to find our attachment sizes:

For Each attachment As Outlook.Attachment In Item.Attachments
  attSize = CType(attachment.PropertyAccessor.GetProperty(PR_ATTACH_SIZE), Integer)
  if attSize.size > 755 then
   'ignore the image
   end if
Next

Thanks..