Add this line to your PowerShell script:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
https://office365itpros.com/2021/01/14/powershell-scripts-fail-exchange-online-tls12/
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This is on Windows Server 2016. Running on PowerShell version 5.1.14393.4583. It was working before. No changes were made to the script or the server. Suddenly the script throws the following error message:
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At line:1 char:1
====================
Here's the script (edited for privacy)===================================
# Internal
$date = Get-Date -format "yyyy-MM-dd"
$Path = "c:\folder\"
$file = Get-ChildItem -Path $Path -File | Sort CreationTime | Select -Last 1
$fileName = $file -replace ".xlsx", ""
write-host $fileName
$SMTPServer = "smtp.office365.com"
$Username = "email@email.com"
$Password = "password"
$to = "email@email.com,email@email.com,email@email.com"
#$to = "email@email.com"
#$cc = "email@email.com"
$bcc = "email@email.com"
$date = Get-Date -format "yyyy-MM-dd"
$Subject = $fileName
$attachment = "c:\folder\" + $file
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
##$message.body = $body
$message.to.add($to)
#$message.cc.add($cc)
$message.bcc.add($bcc)
$message.from = $username
$message.attachments.add($attachment)
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
=======================================================
Please advise.
Thank you.
Add this line to your PowerShell script:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
https://office365itpros.com/2021/01/14/powershell-scripts-fail-exchange-online-tls12/
Try replacing lines 17 - 39 with this:
$PWord = ConvertTo-SecureString -String $Passwd -AsPlainText -Force
$SmtpCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username, $PWord
$props = @{
From = $username
To = "email@email.com", "email@email.com", "email@email.com"
Bcc = "email@email.com"
Subject = (Get-Content $filename)
Attachments = ("c:\folder\" + $file)
UseSSL = $true
SMTPServer = $SMTPServer
Credential = $SmtpCredential
}
Send-MailMessage @props
The error "SmtpException" means the connection has failed. The default is to wait 100 seconds before failing.
Keep in mind that SMTP clients are responsible for retrying a failed connection, or for any other error.
Are you running this code from your LAN? A good idea may be to set up a SMTP server yourself and let it deal with error handling and retries. All Microsoft servers have a SMTP server as part of the IIS software. Your PowerShell code would then send the message to your local server which, in turn, would deliver it to either another SMTP relay (e.g. smtp.office365.com) for delivery to the intended recipient.