Azure 操作手册 |在交易电子邮件中从我的 Azure 存储帐户发送附件

Azure Runbook | Sending attachments from my Azure Storage Account in Transactional Emails

我有一个 Azure Runbook 脚本,用于处理电子邮件、SMTP 服务器详细信息,以及为安全起见而编辑的发件人和收件人电子邮件。

出于某种原因,PowerShell 没有看到我要作为附件发送的文件,有人能看出我哪里出错了吗?

我附上了一张屏幕截图,以证明我拥有正确的文件(我相信它们叫做 'blobs'?)名称、存储帐户名称和容器名称

下面是 Runbook PowerShell 脚本,以及测试电子邮件时的错误消息。

祝一切顺利,

===================RUNBOOK POWERSHELL CODE===========================

$Username ="xxx"

$Password = ConvertTo-SecureString "xxx" -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential $Username, $Password

$SMTPServer = "xxx"

$EmailFrom = "xxx"

[string[]]$EmailTo = "xxx"

$Subject = "xxx"

# Setting the Azure Storage Context,recommedation is to read sastoken from Runbook assets for security purpose.
$context = New-AzureStorageContext -StorageAccountName "expertadvisers" -SasToken "?sv=2019-12-12&ss=bfqt&srt=c&sp=rwdlacupx&se=2021-01-20T17:01:17Z&st=2021-01-20T09:01:17Z&spr=https&sig=PpozvhXnD6k4knwLLpyJvMF0vpnSZATabreYTzr0s2k%3D"

#Get the blob contents from storage container and store it local destination . 
Get-AzureStorageBlob -Container "notificationcontainer" -Blob "test_file.docx" -Context $context | Get-AzureStorageBlobContent -force -Destination .

#Get contents of the file
[string[]] $attachment = "test_file.docx"

$Body = "TEST MESSAGE"
    
Send-MailMessage -smtpServer $SMTPServer -Attachment $attachment -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body -Priority High -DeliveryNotificationOption OnSuccess -BodyAsHtml
Write-Output ("Email sent succesfully.")

===================ERROR MESSAGE===========================

Get-AzureStorageBlob : The remote server returned an error: (403) Forbidden. HTTP Status Code: 403 - HTTP Error 
Message: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly 
including the signature.
At line:19 char:1
+ Get-AzureStorageBlob -Container "notificationcontainer" -Blob "test_f ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureStorageBlob], StorageException
    + FullyQualifiedErrorId : 
StorageException,Microsoft.WindowsAzure.Commands.Storage.Blob.Cmdlet.GetAzureStorageBlobCommand
 
Send-MailMessage : Could not find file 'C:\Temp\pyxr0xjf.rej\test_file.docx'.
At line:32 char:1
+ Send-MailMessage -smtpServer $SMTPServer -Attachment $attachment -Cre ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Send-MailMessage], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerShell.Commands.SendMailMessage

问题请参考以下步骤

  1. 通过 protal 创建 SAS 令牌

  2. 脚本

$Username =""

$Password = ConvertTo-SecureString "" -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential $Username, $Password

$Subject = "test"
$To=""
$Body = "This is an automated mail send from Azure Automation relaying mail using Office 365."
$Context = New-AzureStorageContext -StorageAccountName "andyprivate" -SasToken "<the sas token you created in step1>"
$a=Get-AzureStorageBlobContent -Container "output" -Blob "test.txt" -Context $Context
$attachment = $a.Name
Send-MailMessage `
    -To $To `
    -Subject $Subject  `
    -Body $Body `
    -UseSsl `
    -Port 587 `
    -SmtpServer 'smtp.office365.com' `
    -From $Username `
    -Attachments $attachment `
    -BodyAsHtml `
    -Credential $credential