-
Apr 19th, 2018, 07:57 AM
#1
Thread Starter
Hyperactive Member
Send out an email to more than 500 recipients
I used code below to send out an email to more than 500 recipients using a loop.
"mailto" is the recipients from a table in which there are more than 500 recipients.
But got an error said that
System.Net.Mail.SmtpException: Service not available, closing transmission channel. The server response was: 4.3.2 The maximum number of concurrent connections has exceeded a limit, closing transmission channel
IT people already increased The maximum number of concurrent connections from 1000 to 5000 in Exchange.
How to fix it?
Dim mail As New MailMessage()
mail.From = New MailAddress("company@company.com")
mail.To.Add(mailto)
mail.Subject = "Order detail"
Dim strBody As String="This is a test"
Dim myMail As New SmtpClient("email.company.com")
myMail.Credentials = New System.Net.NetworkCredential("company", "password")
myMail.Send(mail)
myMail.Dispose()
In real project, more data will be in body like below. Therefore, can't test out of loop.
Dim strBody As String
strBody &= vbCrLf & "Mailto: " & _mailto
strBody &= vbCrLf & "Member: " & _firstname & " " & _lastname
strBody &= vbCrLf & "DOB - Gender: " & _dob & " - " & _gender
strBody &= vbCrLf & "Event Description: " & _eventdes
strBody &= vbCrLf & "Organization: " & _organization
strBody &= vbCrLf & "Date: " & _activitydate
strBody &= vbCrLf & "Time: " & _activitytime
mail.Body = strBody
Last edited by aspfun; Apr 19th, 2018 at 08:23 AM.
-
Apr 19th, 2018, 08:14 AM
#2
Re: Send out an email to more than 500 recipients
try creating myMail only once outside the Loop, then use this single instance to send your 500 mails and only when finished, dispose it.
-
Apr 19th, 2018, 08:25 AM
#3
Re: Send out an email to more than 500 recipients
Another solution would be to create the email once.... send it TO the same person is it FROM and then add the actual recipients as BCC ... then send it once when done.
didn't see the updated info until after the fact... never mind.
-tg
-
Apr 19th, 2018, 12:49 PM
#4
Frenzied Member
Re: Send out an email to more than 500 recipients
A quick and dirt solution might be to slow it down with thread.sleep. Or wait for the connection to close before sending another. Or make a limiter that tracks how many connections you have open and waits if it reaches a limit.
-
Apr 19th, 2018, 11:41 PM
#5
Re: Send out an email to more than 500 recipients
Originally Posted by cory_jackson
A quick and dirt solution might be to slow it down with thread.sleep. Or wait for the connection to close before sending another. Or make a limiter that tracks how many connections you have open and waits if it reaches a limit.
That may or may not be a good idea but nothing else should be done until the code has been modified to create and use only one SmtpClient object and that tested. I strongly suspect that the use of multiple instances of that class is the issue and, even if it isn't, it's silly to use one instance per message when you can create one instance and reuse it for all messages.
-
Apr 20th, 2018, 08:20 AM
#6
Re: Send out an email to more than 500 recipients
If 500 recipients for one message is not working then sending 500 separate messages might, something like this
Code:
Dim myMail As New SmtpClient("email.company.com")
myMail.Credentials = New System.Net.NetworkCredential("company", "password")
Dim mail As New MailMessage()
mail.From = New MailAddress("company@company.com")
mail.Subject = "Order detail"
Dim strBody As New System.Text.StringBuilder
For Each mailto As String In listOfAddr
mail.To.Add(mailto)
'create body
strBody.AppendLine()
strBody.Append("Mailto: ")
strBody.AppendLine(mailto)
strBody.Append("Member: ")
strBody.Append("_firstname")
strBody.Append(" ")
strBody.AppendLine("_lastname")
strBody.Append("etc")
mail.Body = strBody.ToString
strBody.Length = 0
myMail.Send(mail)
mail.To.Clear()
'maybe a sleep here Threading.Thread.Sleep(25)
Next
mail.Dispose()
myMail.Dispose()
Last edited by dbasnett; Apr 20th, 2018 at 08:27 AM.
-
Apr 20th, 2018, 08:38 AM
#7
Re: Send out an email to more than 500 recipients
Originally Posted by dbasnett
If 500 recipients for one message is not working then sending 500 separate messages might
I think that's what's already being done. I believe the red code in post #1 indicates that the body of the message is personalised for each recipient, thus there can only be one recipient per message.
-
Apr 20th, 2018, 10:10 AM
#8
Re: Send out an email to more than 500 recipients
Have you tried setting UseDefaultCredentials = True ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|