What could be the reason of "net_io_connectionclosed." when sending mail from Powershell

Dan høi 1 Reputation point
2022-02-10T07:54:30.61+00:00

Hello.
I've been trying to figure out how to send out email reminders when password is about to expire. This is done by powershell from our Domain Controller.
So far the results have been inconsistant.
1 out of 10 emails will be sent, and the rest gets the following error:

Send-Mailmessage : Unable to read data from the transport connection: net_io_connectionclosed.
At line:48 char:1
+ Send-Mailmessage -smtpServer $smtpServer -from $from -to ..
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage

What really throws me off here, is the fact that sometimes it works, and sometimes it doesn't.
I've tried with -port 587 aswell, to specify which port to use, but this makes the same failure.
When i try to run this script from my own PC instead of the Domain Controller, there is no errors. Even though they are on the same network.

Success and failure is with identical code:

$Credential = Get-Credential

$smtpServer="smtp.office365.com"
$expireindays = 7
$from = "UsingMyEmail"

Import-Module ActiveDirectory
$users = get-aduser -filter * -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress |where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }
foreach ($user in $users)

{
$Name = (Get-ADUser $user | foreach { $_.Name})
$emailaddress = $user.emailaddress
$passwordSetDate = (get-aduser $user -properties * | foreach { $_.PasswordLastSet })
$PasswordPol = (Get-AduserResultantPasswordPolicy $user)

if (($PasswordPol) -ne $null)

{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge
}

else

{
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
}

$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
$subject="Your windows password will expire in $daystoExpire days"
$body ="Insert text here"

if ($daystoexpire -lt $expireindays)

{
Send-Mailmessage -smtpServer $smtpServer -from $from -to "UsingMyEmail" -Credential $Credential -UseSsl -subject $subject -body $body -bodyasHTML -priority High
}

}

Please note:
I've changed my filled the email spots with "UsingMyEmail" to keep them private.

Thank you in advance.

Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,640 questions
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,537 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,796 Reputation points
    2022-02-10T15:35:09.86+00:00

    It would help to see the entire error record. Check the "InnerException" which may have the reason for the failure.

    The problem may be a simple as a failed connection. Remember, not every connection is successful, and it's the sender's responsibility to retry the transmission. A SMTP server would simply requeue the message -- unless there was a reason not to (e.g., failed TLS negotiation, there's an unknown recipient in the message, etc.). Until you know why it doesn't work you'll never really be able to fix the problem.

    Also note this in the Send-MailMessage description (send-mailmessage:

    Warning

    The Send-MailMessage cmdlet is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage. For more information, see Platform Compatibility note DE0005.


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.