Link to home
Start Free TrialLog in
Avatar of Roccat
RoccatFlag for United States of America

asked on

send mail message error

every 17 messages I get an error "Send-MailMessage : Service not available, closing transmission channel. The server response was: Error: too many messages in one session"  I think it is because I have connected to the smtp server to many times and I have not disposed any of the sessions. Do you think this error is because I have not disposed the sessions?  And how would i go about disposing the sessions?


  $Fname = $_.FirstName
  $Lname = $_.LastName
  $EmpID = $_.EID
$MyEmail = "powershell@homelab.com"
$SMTP= "172.24.225.222"
$To = "administrator@homelab.com"
$Subject = "Create Email Script"
$Body = "reply 
ADD NETWORK 	$LogonName	`"$Fname`"	`"`"	`"$Lname`"	`"`"	changenow	`"Requested by:HR \r Created by:Automation`"	$EmpID	`"`"	2	`"$EmailGroup`"	`"cs staff`" 
PUT	USER	$LogonName	1252	0	$LogonName"
Start-Sleep 2
Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body -SmtpServer $SMTP -Port 25 -DeliveryNotificationOption never 

Open in new window

Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

logon to your smtp server 172.24.225.222  and open IIS manager, close  current sessions and then try again
Try to increase the "limit to no of connections"
do you have any open powershell sessions? close if any left

check the smtp properties in IIS manager, -> Messages > check or increase the counts
Avatar of Roccat

ASKER

Thanks. The other admin white listed the email address we are using to send the emails and this seems to have stopped the error message from popping up. But I still have concerns that I am leaving connections open every time I send an email.
You should use the dispose() to close any open sessions :

$SMTPClient = new-object system.net.mail.smtpclient
$MailMessage = new-object system.net.mail.mailmessage

$SMTPClient.Host = $SMTPHost
$MailMessage.From = $Sender
$MailMessage.To.Add($Recipients)
$MailMessage.Subject = "TEXT HERE"
$MailMessage.Body = "BODY TEXT HERE"

$SMTPClient.Send($MailMessage)
$MailMessage.Dispose()
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roccat

ASKER

Thank you