2

Why does sending an email message from PowerShell with the Send-MailMessage command using the flag -Port 587 produce an error.

Command:

Send-Mailmessage -smtpServer mail.server.com -Port 587 -from "[email protected]" -to "[email protected]" -subject "Test" -body "Test" 

Error Message:

Send-Mailmessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first

The PowerShell documentation says adding -UseSSL should specify that a STARTTLS command be sent, but even adding this flag may not resolve your issues.

Command:

Send-Mailmessage -smtpServer mail.server.com -Port 587 -UseSsl -from "[email protected]" -to "[email protected]" -subject "Test" -body "Test" 

Eror message:

Send-Mailmessage : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

3 Answers 3

8

Some SMTP servers may have been hardened to only accept TLS 1.2 for negotiating STARTTLS. In many cases Windows is configured to send TLS 1.0 by default when -UseSSL is specified.

To force Send-MailMessage to use TLS 1.2 it is necessary to add a line to the script before executing the Send-MailMessage:

Either enter:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 

or

[System.Net.ServicePointManager]::SecurityProtocol = 'TLS12' 
Sign up to request clarification or add additional context in comments.

Comments

3

If it helps anybody, this post will be worthwhile. Using Online Exchange via Office 365, this is what I ended up with: (Sanitized)

 $EmailFrom = “[email address]@[FQDN]” $EmailTo = “[email address]@[FQDN]” $Subject = “Test email” $Body = “What do you want your email to say” $Attachment = "C:\sendmail\test.txt" $SMTPServer = “smtp.office365.com” $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $attach = new-object Net.Mail.Attachment($Attachment) $message.Attachments.Add($attach) $SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“[valid/authorised user name]”, “[password]”); $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 

Because I am NOT sending this from an IP Address covered by the SPF record, I have had to authenticate. If the source IP address is covered by the SPF txt record, my understanding is that authentication of user/password would NOT be required (Has not been tested).

Thanks for an awesome article. Was easy to follow, and easy to combine the articles for different requirements in to one, as per the above PowerShell script.

1 Comment

This worked for me, so thanks :-) It does bug me a bit that I can't find a way to make Send-Mailmessage work...
0

My solution to "5.7.3 STARTTLS is required to send mail" was simple. The domain name for network credentials is case sensitive and had to be all CAPS.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.