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
leeand00
  • 4,869
  • 15
  • 69
  • 110
  • 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
  • That's correct. I'm not posting it out here. – leeand00 Jan 15 '15 at 01:38
  • Do you need a relay to do this? – leeand00 Jan 15 '15 at 05:15
  • 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 06:44
  • Have you checked that your host is able to verify the certificate chain? – john Jan 22 '15 at 20:52
  • @john No, how do I do that? – leeand00 Jan 23 '15 at 14:55
  • @leeand00 I used IE... not sure if there's a better way. Browse to smtp.office365.com and sign in. You'll get an error page but you can still see the certificate status in the address bar. – john Jan 23 '15 at 15:01
  • https://stmp.office365.com/ ? I get a "this page can't be displayed" – leeand00 Jan 23 '15 at 15:02
  • 1
    @leeand00 I updated my comment... smtp.office365.com – john Jan 23 '15 at 15:04
  • If by certificate chain you mean the `Certification Path` it says `This certificate is OK.` when I click the lock at the top. – leeand00 Jan 23 '15 at 15:47
  • I'd settle for using gmail smtp at this point. – leeand00 Jan 23 '15 at 16:40
  • 1
    1. https://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 01:51
  • @MathiasR.Jessen Yup that was it. Thank you! You can add the answer if you like and I'll pick it as the right one. – leeand00 Feb 04 '15 at 03:33
  • Well actually it worked from Windows 7. There is no port you can specify for SSL on Server 2003. – leeand00 Feb 04 '15 at 13:12

1 Answers1

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

Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95