This solved the issue for me: https://office365itpros.com/2021/01/14/powershell-scripts-fail-exchange-online-tls12/
Error when running script for automated emails in Windows Server 2016
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
+ $smtp.send($message)
+ ~~~~~~~~~~~~~~~~~~~~
**+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException**
**+ FullyQualifiedErrorId : SmtpException**
Exception calling "Send" with "1" argument(s): "Failure sending mail."
At line:1 char:1
+ $smtp.send($message)
+ ~~~~~~~~~~~~~~~~~~~~
**+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException**
**+ FullyQualifiedErrorId : SmtpException**
====================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.com"
$Password = "password"
$to = "@email.com,@email.com,******@email.com"
#$to = "******@email.com"
#$cc = "******@email.com"
$bcc = "******@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.
SOLVED: https://office365itpros.com/2021/01/14/powershell-scripts-fail-exchange-online-tls12/
Windows for home | Other | Apps
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
4 answers
Sort by: Most helpful
-
Anonymous
2022-03-02T20:48:25+00:00 -
Anonymous
2022-03-02T21:15:47+00:00 That worked! Thank you!
-
Anonymous
2022-03-02T20:40:42+00:00 This is also happening to a client of mine, no changes on any side and we are getting the error.
Any updates or link to the thread on the suggested site?
-
DaveM121 891.6K Reputation points Independent Advisor2021-12-27T16:36:55+00:00 Hi LR_KIT
I am Dave, I will help you with this.
I apologize, Community is just a consumer forum, due to the scope of your question (Server 2016) can you please post this question to our sister forum on Microsoft Q&A in the Server 2016 section (linked below)
Over there you will have access to a host of Server 2016 and IT Pro experts and will get a knowledgeable and quick answer to this question.