locked
Send-MailMessage the server response was: #4.x.2 Too many messages for this session RRS feed

  • Question

  • I have a scheduled task that will check for passwords expiring in 15 days or less and fire off a reminder email to the user that they have x days left to change it.  However, there are usually 70+ addresses in this list and after 10 messages are sent the following error appears:

    The server response was: #4.x.2 Too many messages for this session

    The script will continue and then after 10 more, the same error.  I have even moved the function using send-mailmessage to a separate .ps1 file and call it from the parent .ps1 but the error continues.  I don't want to bump up the concurrent connection limit on my relay, is there any clean way to close the send-mailmessage session?  Otherwise I'll probably just loop the task to catch the error condition.  For what it's worth, the server running this task has .NET 4.0 installed.

    Friday, February 28, 2014 5:01 PM

Answers

  • Hmm... sounds like either the Send-MailMessage cmdlet or the underlying System.Net.Mail classes are doing some sort of session caching, but I'm not sure which (though I would suspect it's the cmdlet).

    I have a version of Send-MailMessage that was rewritten as a PowerShell function (for other reasons) up at http://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d .  You could try downloading that and adding it to your script, and see if you get the same behavior.  If it is, in fact, the cmdlet that's causing this, then using the function should get around that problem.

    • Marked as answer by Dan_754 Friday, March 7, 2014 4:08 PM
    Friday, February 28, 2014 6:12 PM

All replies

  • Hmm... sounds like either the Send-MailMessage cmdlet or the underlying System.Net.Mail classes are doing some sort of session caching, but I'm not sure which (though I would suspect it's the cmdlet).

    I have a version of Send-MailMessage that was rewritten as a PowerShell function (for other reasons) up at http://gallery.technet.microsoft.com/scriptcenter/Send-MailMessage-3a920a6d .  You could try downloading that and adding it to your script, and see if you get the same behavior.  If it is, in fact, the cmdlet that's causing this, then using the function should get around that problem.

    • Marked as answer by Dan_754 Friday, March 7, 2014 4:08 PM
    Friday, February 28, 2014 6:12 PM
  • This looks promising, thanks for the link.

    Update:  David Wyatt's cmdlet using the .Dispose() method allowed all of my automated messages to send without reaching the connection limit.  Thanks again!

    • Edited by Dan_754 Friday, March 7, 2014 4:10 PM Update
    Friday, February 28, 2014 8:12 PM
  • Hi,

    Based on my research, when Send-MailMessage is used it opens a connection to an SMTP server and never closes it (so long as that PowerShell session you are running is open). When Send-MailMessage is run it does not close the connection when it is done until the SMTP server forcibly closes it or the PowerSHhell process is closed.

    Regards,

    Yan Li


    Regards, Yan Li

    Monday, March 3, 2014 9:21 AM