Exchange

A number of emails failed to be processed by GFI MailEssentials

Recently in not really long time one of our Clients reported that GFI Mail Essentials started reporting that a number of emails failed to be processed by GFI MailEssentials. While normally if it happens once or twice it's not a problem GFI reported there are currently 544 emails in C:\Program Files (x86)\GFI\MailEssentials\EmailSecurity\FailedMails. That is a lot of emails.

Solution

While generally the issue should be resolved with vendor and can be caused by one of antivirus engines breaking things… the real issue has to be fixed as over 500 emails were stopped before being deliverd.

Actually this 500 emails became 1200 in just few hours. Fortunately the fix is pretty simple.

Files .PROP can be freely deleted (you can leave them for gfi to verify things)

Files .TXT are standard .EML emails

So now that we know that .TXT files are your standard emails we can simply pass them to Exchange to be reprocessed. You can do that by renaming file extension from .txt to .eml and putting them in

C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Replay folder. If everything is ok, you should notice the file extension changing from .eml to .tmp finally getting removed. If things go wrong the file extension will change from .eml to .bad. If that happens… well you're out of luck. But do not worry, this usually means the email format is bad and therefore it wouldn't pass to Exchange anyways.

While doing this manually is one way you can actually script this a bit. You can create .cmd file and execute following:

REM Processing Files 
cd C:\Program Files (x86)\GFI\MailEssentials\EmailSecurity\FailedMails
ren *.txt *.eml
pause
del *.prop
pause
move "C:\Program Files (x86)\GFI\MailEssentials\EmailSecurity\FailedMails\*.eml" "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Replay\"
pause

REM This part should be used only to retry reprocessing BAD files. Don't think it will change much but you can always try.

cd C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Replay
ren *.bad *.eml
pause

Please notice use of PAUSE. This makes sure you can see your results before continuing.. you can remove PAUSE if you're sure what you're doing. The only problem with this solution is that when you push out 1200 emails to replay folder you will get a large queue of emails. In efforts to get things a bit more automated and at same time not filling up your queue too fast I've decided to write this powershell script. Keep in mind it's untested as the scenario didn't happen for 3rd time yet. Goal for this script was to set it up as task scheduled and have it running every few hours to fix the failed folder automatically. When GFI freaks out again I will rework this script to be fully tested. At the moment use it on your own risk (or use the script above – that one works).

$FailedFolder = "C:\Program Files (x86)\GFI\MailEssentials\EmailSecurity\FailedMails"
$ReplayFolder = "C:\Program Files\Microsoft\Exchange Server\V15\TransportRoles\Replay"
$LimitBad = 30
$LimitAll = 70
$LimitFailed = 50

function CountProcessedFiles() {
    $ReplayFiles = Get-ChildItem -Path $ReplayFolder | Where-Object {$_.Extension -eq ".TMP"}
    $AllFiles = Get-ChildItem -Path $ReplayFolder 
    if ($AllFiles.Count -gt $LimitAll) {
        $AllFiles.Count
        return $AllFiles.Count
    }
    else {
        return $ReplayFiles.Count
    }    
}
function CountProcessedBadFiles(){
    $BadFiles = Get-ChildItem -Path $ReplayFolder  | Where-Object {$_.Extension -eq ".BAD"}
    return $BadFiles.Count
}

function DoWork() {
    $RemoveFiles = Get-ChildItem -Path $FailedFolder | Where-Object {$_.Extension -eq ".PROP"}
    ForEach ($file in $RemoveFiles) {
        remove-item $file.fullname
    }

    $FailedFiles = Get-ChildItem -Path $FailedFolder | Where-Object {$_.Extension -eq ".txt"}
    ForEach ($file in $FailedFiles) {
        $fullNameOld = $FailedFolder + "\" + $file.Name
        $fullNameNew = $FailedFolder + "\" + $file.Name + ".eml"
        Rename-Item $fullNameOld $fullNameNew
        Move-Item -Path $fullNameNew -Destination $ReplayFolder

        #Pause
        while (CountProcessedFiles -gt $LimitFailed) {
            Start-Sleep -Seconds 1
            $countProcessed = CountProcessedFiles
            $countBad = CountProcessedBadFiles
            Write-Host "Waiting... $countProcessed $countBad" 
        }
    }
}

DoWork

This post was last modified on 15 lutego, 2018 23:06

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

3 tygodnie ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 miesięcy ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 miesięcy ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 miesięcy ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 miesięcy ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 miesięcy ago