Error when running script for automated emails in Windows Server 2016

LRodz 21 Reputation points
2021-12-27T16:38:53.523+00:00

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@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.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. JoeyMcJoeJoe 81 Reputation points
    2022-02-08T19:46:47.137+00:00

    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/

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rich Matheisen 46,476 Reputation points
    2021-12-27T20:11:10.637+00:00

    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
    

  2. Rich Matheisen 46,476 Reputation points
    2021-12-29T22:36:58.96+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.