azuresmtpazure-runbookrunbook

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


I have an Azure Runbook script set up for transaction emails, the SMTP server details, & the from and to emails redacted for security.

For some reason, PowerShell is not seeing the file with which I want to send as an attachment, can anyone see where I am going wrong?

I have included a screenshot as proof that I have the correct file (I believe they're called 'blobs'?) name, storage account name and container name

Below is the Runbook PowerShell script, along with the error messages upon testing the email.

All the best,

===================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


Solution

  • Regarding the issue, please refer to the following steps

    1. Create SAS token via protal enter image description here

    2. Script

    $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