Script module to send out emails

Hi Team,

My issue is with the script module to send out emails.The objective is to use a secure way to call the password in the script module rather than using plain text password.

I have encrypted the plain text password using the command


$Convertpassword = 'XXXXXXXXXXX'| ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "F:\Auto\file\pass.txt"

Then I have called the encrypted password


$username = "xxxxxxxxxx@abc.com"
$password = Get-Content "F:\Auto\file\pass.txt" | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password

When I use the below command in the automation script

Send-MailMessage -smtpServer $smtpServer -from $from -to $email -subject $subject -body $body -bodyasHTML -Port 587 -priority High -ErrorAction Stop -UseSsl -credential $cred -attachment $attachment

Email won't be sent and the error message is:

Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the value of argument "password" is null. Change the value of argument "password" to a non-null value."

$smtpServer = "smtp.office365.com"
$from = "XXXX@abc.com"
$smtpServer = "smtp.office365.com"
$from = "XXXXX@abc.com"
$SmtpUser = "xxxxxxxxxx@abc.com"
$smtpPassword = Get-Content "F:\Auto\file\pass.txt" | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password


Send-MailMessage -smtpServer $smtpServer -from $from -to $email -subject $subject -body $body -bodyasHTML -Port 587 -priority High -ErrorAction Stop -UseSsl -credential $cred -attachment $attachment