Code Sample – Parse eml file as SPEmailMessage

 

A while back, we happened to work on an issue with SharePoint Incoming email. We saw that certain types of attachments were causing issues with this set up. In order to check what SharePoint was doing with the eml files in the Drop folder, we first needed to parse them like SharePoint would. Below sample code shows how this is done:

Reference - SPEmailMessage class

[system.reflection.assembly]::LoadWithPartialName('Microsoft.sharepoint')

$fileInfo = new-object System.IO.FileInfo('C:\inetpub\mailroot\DROP\<random_number>.eml')

$fs = $fileInfo.OpenRead()

$fs.Seek(0, [System.IO.SeekOrigin]::Begin)

$message = new-object Microsoft.SharePoint.Utilities.SPEmailMessage( $fs, "account@domain.com")

$message.Attachments | fl 

 

Using the above parsing technique, we can enumerate and inspect all the attachments as .Net objects, and see if anything stands out for the prospective bad attachments.