【问题标题】:Can I save an email attachment from Office 365 to a file share with PowerShell?我可以使用 PowerShell 将电子邮件附件从 Office 365 保存到文件共享吗?
【发布时间】:2016-08-30 12:08:06
【问题描述】:

我见过几种使用 PowerShell 自动化本地 Outlook 客户端的解决方案,但我希望它在服务器端运行:使用给定帐户访问服务器,检查未读邮件,将所有附件保存到文件共享并将消息标记为已读。

【问题讨论】:

    标签: email powershell fileshare


    【解决方案1】:

    您在 Exchange 环境中吗?如果是这样,并且 Exchange Web 服务公开给网络,这是一个选项。

    【讨论】:

    • 我们处于混合 Exchange 环境中。我正在尝试阅读 EWS,但如果您有任何快速而肮脏的 PowerShell 来连接并从邮箱中提取电子邮件,那将是一个很大的帮助......
    • 哦,嘿,我发现了一些有效的代码。现在看看我是否可以剥离附件。
    【解决方案2】:

    这需要安装Exchange Managed Services API

    $ewsPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.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@domain.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:\Temp\” + $attachment.Name.ToString()), [System.IO.FileMode]::Create)
            $File.Write($attachment.Content, 0, $attachment.Content.Length)
            $File.Close()
        }
    }
    

    【讨论】:

    • 谢谢。这很好用。您能否将其扩展为如何仅保存具有给定扩展名的文件附件以及在特定日期和时间之后到达的电子邮件(或者说每 15 分钟轮询一次电子邮件)?
    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多