0

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.

Tikhon
  • 947
  • 2
  • 18
  • 32

2 Answers2

1

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.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Added code Item.Save(), however the attachment.Size property is still 0 – Tikhon Feb 04 '19 at 22:07
  • 1
    What do you see in OutlookSpy if you click the IMessage button, go to the GetAttachmentTable tab, double click on the attachments, and look at the PR_ATTACH_SIZE property? How about MailItem? Select Attachments property, click Browse, go to the IEnumVariant tab, double click on attachment, select the Size property. – Dmitry Streblechenko Feb 05 '19 at 00:18
  • We don't have OutlookSpy installed currently. – Tikhon Feb 05 '19 at 13:09
  • So can you try to install it? – Dmitry Streblechenko Feb 05 '19 at 13:33
  • Sure. Did you write this? – Tikhon Feb 05 '19 at 13:41
  • 1
    I did. You can also try MFCMAPI, but it will only show you PR_ATTACH_SIZE, not Attachment.Size. – Dmitry Streblechenko Feb 05 '19 at 13:52
  • Updated post to include the steps you recommended with screen shots – Tikhon Feb 05 '19 at 15:30
  • 1
    You clicked Application button and selected the AttachmentSelection property. You need to click Item or CurrentItem (depending on whether you are in Inspector or Explorer). – Dmitry Streblechenko Feb 05 '19 at 15:35
  • At first, the IEnumVariant tab shows no attachments, however after a few minutes the attachments show up. There is an Outlook setting that saves a draft after a period of time. Ours is set to 3 minutes. I suspect this is why. – Tikhon Feb 05 '19 at 16:07
  • But didn't you say you added a call to MailItem.Save? So the message should lbe saved, right? – Dmitry Streblechenko Feb 05 '19 at 16:22
  • Yes we did add MailItem.save. Added code we used to this post as an edit. Thanks.. – Tikhon Feb 05 '19 at 17:02
  • But then you are saying the attachments are visible only after the message is autosaved, which means Save was not called. – Dmitry Streblechenko Feb 05 '19 at 17:06
  • Since we don't do much Outlook VSTO development work, we're not 100% sure if MailIItem.Save is related to the autosave setting or if the fact that the embedded items are showing up in Outlook Spy as a result of the autosave setting. Thanks again for your assistance on this. – Tikhon Feb 05 '19 at 20:35
0

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..

Tikhon
  • 947
  • 2
  • 18
  • 32