保存Office 365邮箱中的电子邮件附件



我从这里找到了下面的脚本:

我可以将Office 365中的电子邮件附件保存到与PowerShell共享的文件中吗?

$ewsPath = "C:Program FilesMicrosoftExchangeWeb Services2.2Microsoft.Exchange.WebServices.dll"
Add-Type -Path $ewsPath

$ews = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$cred = (Get-Credential).GetNetworkCredential()
$ews.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $cred.UserName, $cred.Password, $cred.Domain
$ews.AutodiscoverUrl( "user@contoso.com", {$true} )
$results = $ews.FindItems(
"Inbox",
( New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList 100 )
)
$MailItems = $results.Items | where hasattachments

foreach ($MailItem in $MailItems){

$MailItem.Load()

foreach($Attachment in $MailItem.Attachments){
$Attachment.Load()
$File = new-object System.IO.FileStream(("C:AttachmentsDownloads” + $attachment.Name.ToString()), [System.IO.FileMode]::Create)
$File.Write($attachment.Content, 0, $attachment.Content.Length)
$File.Close()
}
}

我可以下载附件,但是当电子邮件作为附件附加时,下载将失败。我不知道下载时。msg文件和。pdf文件之间的区别是什么…我真正需要的是。msg附件。

我得到的错误如下:

Exception calling "Write" with "3" argument(s): "Buffer cannot be null.
Parameter name: array"
At C:DownloadAttachment.ps1:21 char:9
+         $File.Write($attachment.Content, 0, $attachment.Content.Lengt ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException

你能帮帮忙吗?任何帮助将非常感激!

如果你有一个附加的电子邮件,那么文件名通常是空的,所以你需要为这些类型的附件生成一个文件名,并加载Mime内容,这样你就可以把它保存为EML文件,见下面的代码。Msg文件格式(或复合OLE)是Outlook文件格式,所以如果你想要,你需要使用Outlook API(我会尝试Redemption)

$downloadDirectory = "C:AttachmentsDownloads"
$Attachment.Load()
if ($Attachment.ContentType -eq "message/rfc822") {
$mimePropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::MimeContent)
$Attachment.Load($mimePropertySet)
$attachmentData = $attachment.Item.MimeContent.Content
$fiFile = new-object System.IO.FileStream(($downloadDirectory + "" + [GUID]::NewGuid().ToString() + ".eml"), [System.IO.FileMode]::Create)
$fiFile.Write($attachmentData, 0, $attachmentData.Length)
$fiFile.Close()
write-host "Downloaded Attachment : " + (($downloadDirectory + "" + $Attachment.Name.ToString()))
}else{
$Attachment.Load()
$File = new-object System.IO.FileStream(($downloadDirectory + "" + $attachment.Name.ToString()), [System.IO.FileMode]::Create)
$File.Write($attachment.Content, 0, $attachment.Content.Length)
$File.Close()
}

您可以使Outlook自动获取MSG文件附件。下面是从收件箱中获取未读项目并保存附加文件的代码:

Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
.Session.GetDefaultFolder(Outlook
.OlDefaultFolders.olFolderInbox);
Outlook.Items inBoxItems = inBox.Items;
Outlook.MailItem newEmail = null;
inBoxItems = inBoxItems.Restrict("[Unread] = true");
try
{
foreach (object collectionItem in inBoxItems)
{
newEmail = collectionItem as Outlook.MailItem;
if (newEmail != null)
{
if (newEmail.Attachments.Count > 0)
{
for (int i = 1; i <= newEmail
.Attachments.Count; i++)
{
newEmail.Attachments[i].SaveAsFile
(@"C:TestFileSave" +
newEmail.Attachments[i].FileName);
}
}
}
}
}
catch (Exception ex)
{
string errorInfo = (string)ex.Message
.Substring(0, 11);
if (errorInfo == "Cannot save")
{
MessageBox.Show(@"Create Folder C:TestFileSave");
}
}

最新更新