Alternatively, you can register a FileSystemWatcher to listen on a Created event. Then, this code can copy out the EML file to some safe place before job-email-delivery gets it.
To install the listener:
$destination = 'c:\Windows\Temp\maillogs' # Make sure your powershell user can write here
$dropFolder = 'C:\inetpub\mailroot\Drop' # Drop folder as configured in SharePoint
$filter = '*.eml'
$fsw = New-Object System.IO.FileSystemWatcher($dropFolder, $filter)
$fsw.IncludeSubdirectories = $false
$fsw.NotifyFilter = [System.IO.NotifyFilters]'FileName,LastWrite' # seems to need this to avoid some IOExceptions
$onCreated = Register-ObjectEvent -InputObject $fsw -EventName Created -Action {
Write-Host "$($Event.TimeGenerated) : Incoming email file - $($Event.SourceEventArgs.Name)"
Copy-Item $Event.SourceEventArgs.FullPath -Destination $destination -Force -Verbose
}
To remove the listener:
Unregister-Event -SourceIdentifier $onCreated.Name
$onCreated.Dispose()
Unknowns:
- Does not account for large incoming files that are temporarily locked while being copied
- Not sure if this causes problems if job-email-delivery tries to access (or even remove) file while Copy-Item is being called.
- Not sure if this is guaranteed to happen before job-email-delivery
- Not sure of the performance impact