3

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
  • 1
    Try with the verbose switch and dump the log here. – Colyn1337 Jan 14 '15 at 18:45
  • 1
    Are you sure that myemail@somedomain.com is actually your username, and not just your smtp address? Seems like the credentials do not successfully authenticate you – Mathias R. Jessen Jan 14 '15 at 21:56
  • 1
    I received this error due to my credentials containing an invalid username. Granted this was .NET C# code, but still relevant. Are you sure your $emailCred is valid? – andrewb Jan 15 '15 at 6:44
  • 1
    @leeand00 I updated my comment... smtp.office365.com – john Jan 23 '15 at 15:04
  • 1
    1. technet.microsoft.com/en-us/library/dn554323.aspx is the requirements for using SMTP. In this case your senario seems closest to "Client SMTP Submission". Make sure you meet all the requirements. 2. SSL 3 is not supported in Office 365 anymore because it is too insecure, make sure you OS/Schannel can use TLS 1.0, which Server 2003 should only just be able to do. – Bernie White Jan 26 '15 at 1:51
2

Review the original error message you received:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

The first suggestion (requirement of a secure connection) can probably be ignored due to the fact that you did already specify compliance with such a requirement by using the -UseSSL switch parameter

What we're left with, is the impression that the credentials you provided did not successfully authenticate you.

If that is the case, the SMTP Server will treat your identity as "ANONYMOUS", an identity seldom allowed to submit emails unless the sending host is explicitly whitelisted. In this light, the SMTP response message:

5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

suddenly makes a lot more sense.

My guess is that your SMTP address (myemail@somedomain.com) is not the same as your user principal name (might be myusername@corp.somedomain.com or something similar), which causes authentication to fail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.