簡體   English   中英

支持發送 office 365 smtp email 的方式,允許我們附加文件

[英]Supported way to send office 365 smtp email which allow us to attach a file

我有這個代碼發送 email 附件:-

$encpassword = convertto-securestring -String "*****" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "***@***", $encpassword
Connect-PnPOnline -Url $sourceWebURL -Credentials $cred
Send-MailMessage -to "" -from "" -Credentials $cred -bcc "" -Port "587" -UseSSL "true" -Subject "subject" -Body "<b>1</b><br><b>2</b>" -BodyAsHtml -SmtpServer "smtp.office365.com"  -Attachments "C:\s.csv"

但基於文檔的警告,即Send-MailMessage已過時 @ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7.3

同時使用Send-PnPMail不支持發送附件 @ https://pnp.github.io/powershell/cmdlets/Send-PnPMail.html

所以我的問題是我們如何使用 power shell 發送 email,使用允許我們附加文件的支持/推薦方法。

謝謝

現在,這是通過來自Microsoft Graph PowerShell模塊Microsoft.Graph.Users.ActionsSend-MgUserMail完成的。

示例 3:創建帶有文件附件的消息並發送消息

Import-Module Microsoft.Graph.Users.Actions

$params = @{
    Message = @{
        Subject = "Meet for lunch?"
        Body = @{
            ContentType = "Text"
            Content = "The new cafeteria is open."
        }
        ToRecipients = @(
            @{
                EmailAddress = @{
                    Address = "meganb@contoso.onmicrosoft.com"
                }
            }
        )
        Attachments = @(
            @{
                "@odata.type" = "#microsoft.graph.fileAttachment"
                Name = "attachment.txt"
                ContentType = "text/plain"
                ContentBytes = "SGVsbG8gV29ybGQh"
            }
        )
    }
}

# A UPN can also be used as -UserId.
Send-MgUserMail -UserId $userId -BodyParameter $params

Send-MailMessage文檔關於Send-MailMessage已過時的警告沒有提及的是它現在已過時的根本原因。 例如,當您繼續連接到內部部署的 Exchange 服務器時, Send-MailMessage仍然可以正常工作,那么為什么會出現消息?

原因是作為微軟“默認安全”政策的一部分,微軟將於 2022 年 10 月 1 日為所有人永久禁用 Exchange Online 中的基本身份驗證( 基本身份驗證和 Exchange Online - 2021 年 9 月更新 - Microsoft 技術社區)。

這包括 Exchange Web 服務 (EWS)、Exchange ActiveSync (EAS)、POP、IMAP、遠程 PowerShell、MAPI、RPC、SMTP AUTH 和 OAB。 所有客戶端應用程序都需要切換到 Modern Authentication/OAuth 或 Microsoft Graph API 才能繼續到 function。

Send-MailMessage使用基本身份驗證(用戶名和密碼以及使用 SSL 的選項),但並未使用現代身份驗證方法。 它主要設計用於與內部部署的 Exchange 服務器一起使用,並且不必擔心那些討厭的事情,例如 MFA 或使用鍵/值/令牌進行身份驗證的選項。 因此,當嘗試將它與 Exchange Online/O365 服務器一起使用時,它的使用現在已經過時。


O365 支持的方法是使用 Microsoft Graph 發送帶附件的消息 它比普通的Send-MailMessage稍微復雜一些,因為您需要使用 JSON 來創建配置。 您還需要將文件附件轉換為 Base64 編碼的字符串,以便它可以添加到 JSON 消息中。

Import-Module Microsoft.Graph.Users.Actions
Connect-MgGraph

#Convert File to Base64
$filename = "C:\attachment.csv"
$filenameBase64string = [Convert]::ToBase64String([IO.File]::ReadAllBytes($filename))

$params = @{
    Message = @{
        Subject = "Important Attachment"
        Body = @{
            ContentType = "html"
            Content = "Please find <b>Important Document</b> attached"
        }
        ToRecipients = @(
            @{
                EmailAddress = @{
                    Address = "johnGu@contoso.com"
                }
            }
        )
        Attachments = @(
            @{
                "@odata.type" = "#microsoft.graph.fileAttachment"
                Name = "attachment.csv"
                ContentType = "text/plain"
                ContentBytes = $filenameBase64string
            }
        )
    }
}

Send-MgUserMail -UserId johnGu@contoso.com -BodyParameter $params

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM