自动将电子邮件另存为msg文件?

时间:2018-09-24 09:01:35

标签: powershell email

我希望有人能够提供帮助。我正在尝试自动将电子邮件另存为.msg文件。这些电子邮件是公众通过在线门户发送的请求。然后,电子邮件将具有与其相关的索引文件,然后被上载到EDM系统中。我需要将电子邮件保存为原始格式,因为其中包含无法篡改的个人/机密信息。 我之前做过类似的事情,但是保存的是附件,而不是实际的电子邮件。

我尝试了很多,下面的脚本是我得到的最接近的脚本,但这是通过“我的”默认电子邮件地址进行搜索的,我需要它读取一个不同的地址,可能是多个不同的地址。有谁知道是否可以修改此参数来设置要搜索的电子邮件地址?

$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)   
}

0 个答案:

没有答案