How to troubleshoot : Save Mail Message Activity is corrupting body embedded images

When using Get Outlook Mail Messages and Save Mail Message activities to save an email as ".eml" format, All the body embedded images gets corrupted. On the other hand, if we copy and paste the email directly from from Outlook, it shows the images correctly.

Explanation : 
When the email is saved with UiPath Save Mail Message activity, it saves the email in eml format and the body embedded images are not shown correctly or shown corrupted, while if we directly copy and save the email in the machine, it shows the images correctly and this behavior is due to the emails are now saved as “msg” format but not “eml”.

How to verify :

Scenario 1:
Using UiPath Save Mail Message Activity - This activity saves the emails in eml format and the embedded images are saved as attachments and the body shows corrupted images.

This can also be verified with below powershell script, which saves the email in eml format and the images are still shown as corrupted. Hence this issue is not from UiPath activity but the eml format itself has such issue with embedded images in body, while saving.

----------- Powershell Script for saving email in eml format: -----------

$DestinationPath = “*****Location*"
$EmailAddress = “abcd@outlook.com”
#Removes invalid Characters for file names from a string input and outputs the clean string
Function Remove-InvalidFileNameChars
{
    param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [String]$Name
    )
    return [RegEx]::Replace($Name, “[{0}]” -f ([RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())), ‘-‘)
}
#Add Interop Assembly
Add-type -AssemblyName “Microsoft.Office.Interop.Outlook” | Out-Null
#Type declaration for Outlook Enumerations
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$olSaveType = “Microsoft.Office.Interop.Outlook.OlSaveAsType” -as [type]
$olClass = “Microsoft.Office.Interop.Outlook.OlObjectClass” -as [type]
#Add Outlook Com Object, MAPI namespace, and set folder to the Inbox
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNameSpace(“MAPI”)
#
$folder = $namespace.getDefaultFolder($olFolders::olFolderDrafts)
#$folder = $namespace.getDefaultFolder($olFolders::olFolderDrafts)
#Iterate through each object in the chosen folder
foreach ($email in $folder.Items)
{
    #Get email’s subject and date
    [string]$subject = $email.Subject
    [string]$sentOn = $email.SentOn
    #Strip subject and date of illegal characters, add .msg extension, and combine
    $fileName = Remove-InvalidFileNameChars -Name ($sentOn + “-” + $subject + “.eml”)
     #Combine destination path with stripped file name
    $dest = $DestinationPath + $fileName
    $email.SaveAs($dest, $olSaveType::olEML)  
}


Scenario 2:
When we copy the email directly from outlook to anywhere in the machine, it gets saved as msg format, and msg format does not have same issue and same can be verified even with Powershell script.

----------- Powershell Script for saving email in msg format: -----------

$DestinationPath = "*****Location*

$EmailAddress = “abcd@outlook.com”
#Removes invalid Characters for file names from a string input and outputs the clean string
Function Remove-InvalidFileNameChars
{
    param
    (
        [Parameter(Mandatory=$true, Position=0)]
        [String]$Name
    )
    return [RegEx]::Replace($Name, “[{0}]” -f ([RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())), ‘-‘)
}
#Add Interop Assembly
Add-type -AssemblyName “Microsoft.Office.Interop.Outlook” | Out-Null
#Type declaration for Outlook Enumerations
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$olSaveType = “Microsoft.Office.Interop.Outlook.OlSaveAsType” -as [type]
$olClass = “Microsoft.Office.Interop.Outlook.OlObjectClass” -as [type]
#Add Outlook Com Object, MAPI namespace, and set folder to the Inbox
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNameSpace(“MAPI”)
#
$folder = $namespace.getDefaultFolder($olFolders::olFolderDrafts)
#$folder = $namespace.getDefaultFolder($olFolders::olFolderDrafts)
#Iterate through each object in the chosen folder
foreach ($email in $folder.Items)
{
    #Get email’s subject and date
    [string]$subject = $email.Subject
    [string]$sentOn = $email.SentOn
    #Strip subject and date of illegal characters, add .msg extension, and combine
    $fileName = Remove-InvalidFileNameChars -Name ($sentOn + “-” + $subject + “.msg”)
     #Combine destination path with stripped file name
    $dest = $DestinationPath + $fileName
    $email.SaveAs($dest, $olSaveType::olMSG)
}

Note : Same issue would happen with Exchange server as well and powershell script can be found here


Work-around :
Original email can be forwarded as attachment to any mailbox and use “Save Attachments” activity to download the original mail. This email would be having the body containing correct embedded images.

1 Like