When I follow the guidance from this website https://adamtheautomator.com/send-mailmessage/
Why does the test result state that it's unable to find the file, even though the file appears in my Azure File Explorer?
Below is the code:
$Username ="<username>"
$Password = ConvertTo-SecureString "<password" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $Username, $Password
$SMTPServer = "<server>"
$EmailFrom = "<from email>"
[string[]]$EmailTo = "<to email>"
$Subject = "TEST MESSAGE"
# Setting the Azure Storage Context, recommendation is to read sastoken from Runbook assets for security purpose.
$context = New-AzureStorageContext -StorageAccountName "<ACCOUNT NAME>" -SasToken "<SAS TOKEN>"
# Get contents of the file
$attachment = Get-Content "<filename>"
$Body = "<Body Message>"
Send-MailMessage -smtpServer $SMTPServer -Attachments <file_name> -Credential $credential -Usessl -Port 587 -from $EmailFrom -to $EmailTo -subject $Subject -Body $Body -BodyAsHtml
Write-Output ("Email sent succesfully.")
The following error message appears:
Get-Content : Cannot find path 'C:\Temp\4wved2xx.juf\<file name>' because it does not exist.
At line:19 char:15
+ $attachment = Get-Content "<file name>"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\Temp\4wved2xx.juf\<file name>.jpg:String) [Get-Content],
ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
Send-MailMessage : Cannot validate argument on parameter 'Attachments'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:30 char:55
+ ... -MailMessage -smtpServer $SMTPServer -Attachments $attachment -Creden ...
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Send-MailMessage], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SendMailMessage