My script is meant to suck up errors in scheduled tasks that don't complete correctly and sends them to my email address via Office365:
$reportHTML = Get-Content C:\windows\tasks\SchedLgU.Txt | Select-String -Pattern "an exit code of \(1\)" -context 2
$reportHTML = Out-String -InputObject $reportHTML
#TODO Add an if statement
$emailUsername = "myemail@somedomain.com"
#TODO: Change this to a file....
#$emailPassword = cat "C:\ps\emailCred.txt" | convertto-securestring
$emailPassword = ConvertTo-SecureString "somepassword" -AsPlainText -Force
$emailCred = new-object -typename System.Management.Automation.PSCredential($emailUsername, $emailPassword)
Send-MailMessage -To "myemail@somedomain.com" -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl -SmtpServer 'smtp.office365.com' -From 'myemail@somedomain.com' -Credential $emailCred
But when I run it, I get the following error message:
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server respons
e was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
At C:\ps\FailedJobNotificiation.ps1:14 char:17
+ Send-MailMessage <<<< -To "myemail@somedomain.com" -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl -SmtpServ
er 'smtp.office365.com' -From 'myemail@somedomain.com' -Credential $emailCred
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
I cannot specify a port either, but it seems to me that this may be another issue since I am hearing back from the server, is there some reason I can't send this email out? I found some things on setting up an SMTP relay, but I don't manage that.
If I use the domain we use for mail instead (somedomain.com
), I get a different error messsage about authentication:
Send-MailMessage : The remote certificate is invalid according to the validation procedure.
At C:\ps\FailedJobNotificiation.ps1:16 char:17
+ Send-MailMessage <<<< -To "myemail@somedomain.com" -Body $reportHTML -Subject 'Lois Job(s) Failed' -UseSsl -SmtpServ
er 'mail.somedomain.com' -From 'myemail@somedomain.com' -Credential $emailCred
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], Authentica
tionException
+ FullyQualifiedErrorId : AuthenticationException,Microsoft.PowerShell.Commands.SendMailMessage
With Verbose I get this:
Send-MailMessage : The remote certificate is invalid according to the validation procedure.
At C:\ps\FailedJobNotificiation.ps1:17 char:17
+ Send-MailMessage <<<< -Verbose -To "myemail@somedomain.com" -Body $reportHTML -Subject 'Job(s) Failed' -UseSsl
-SmtpServer 'mail.somedomain.com' -From 'myemail@somedomain.com' -Credential $emailCred
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], Authentica
tionException
+ FullyQualifiedErrorId : AuthenticationException,Microsoft.PowerShell.Commands.SendMailMessage