微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Azure 运行手册 |在事务性电子邮件中从我的 Azure 存储帐户发送附件

如何解决Azure 运行手册 |在事务性电子邮件中从我的 Azure 存储帐户发送附件

我有一个 Azure Runbook 脚本,用于处理交易电子邮件、SMTP 服务器详细信息以及出于安全考虑而对发件人和收件人电子邮件进行了编辑。

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

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

以下是 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

Azure Storage Account container screenshot

解决方法

关于问题,请参考以下步骤

  1. 通过 protal 创建 SAS 令牌 enter image description here

  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

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。