I hope someone will be able to help. I am trying to automatically save emails as .msg files. These emails are requests that are sent by the public via an online portal. The emails will then have an index file created relating to it and then uploaded into an EDM system. I need to save the email in its original format as it contains personal/confidential information that cannot be tampered with. I have done something similar before but saving attachments rather than the actual email.
I had a number of attempts and the script below is the closest I have got but this is searching through “my” default email address and I need it to read though a different one, possibly a number of different ones. Does anyone know if this can be modified to parameterise an email address to search though?
$DestinationPath = "c:\Support\files\" $EmailAddress = "Application.Emails@donny.gov.uk" #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) }