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:
- A file attachment
- Screen Shot
- Screen Clipping
- 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:
- New Mail Message
- Inserted screen shot and signature file:
OutlookSpy->IMessage
IMessage window blank (below)
Close IMessage window.
Re-Open IMessage Window
Inserted (attached) files appear (below)
8. Double Clicked on attachment
- Selected Inspector Button
- Current Item:
- Browse:
- Attachments:
- Browse:
- IEnumVariant:
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.