Results 1 to 8 of 8

Thread: Send out an email to more than 500 recipients

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    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.

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,372

    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.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Send out an email to more than 500 recipients

    Quote Originally Posted by cory_jackson View Post
    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.

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Send out an email to more than 500 recipients

    Quote Originally Posted by dbasnett View Post
    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.

  8. #8
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    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
  •  



Click Here to Expand Forum to Full Width