CDO Error when sending Email

I get an error when sending emails using CDO. I know this is not uncommon but I think I have narrowed down the issue. I iterate thorugh a group of users calling a Send_Email function with every loop. This opens a CDO object and sends an email. This is the code:

Public Function Send_Email(ByVal strTO As String, ByVal strFROM As String, ByVal strSUBJECT As String, ByVal strMSG As String)
        If ChkValidEmail(strTO) And ChkValidEmail(strFROM) Then
            'Variable which will send the mail
            Dim obj As System.Web.Mail.SmtpMail

            'Variable to store the attachments
            Dim Attachment As System.Web.Mail.MailAttachment

            'Variable to create the message to send
            Dim Mailmsg As New System.Web.Mail.MailMessage
           
            'Set the properties
            'Assign the SMTP server
            obj.SmtpServer = "127.0.0.1"
           
            'Address of the recipient
            Mailmsg.To = strTO

            'From Address
            Mailmsg.From = strFROM

            'Specify the body format
            Mailmsg.BodyFormat = MailFormat.Html 'Send the mail in HTML Format
         
           'Mail Subject
            Mailmsg.Subject = strSUBJECT

            'Mail Body
            Mailmsg.Body = strMSG

            'Call the send method to send the mail
            obj.Send(Mailmsg)
           
        End If
    End Function

This first time through it runs fine and actually every time through seems to be fine but I receive an error on every attempt after the first:

System.Web.HttpException: Could not access 'CDO.Message' object. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException (0x80040211): The message could not be sent to the SMTP server. The transport error code was 0x800ccc0b. The server response was not available

I think the problem is that possibly in the send method a DOEVENTS is included or something similar that continues my code iteration and attempts to send another email while the cdo is already opened sending the previous. I have come to this conclusion when stepping through the project. Is there a way to stop this or that I do a check to make sure it is not opened before I continue and possibly run my own DOEVENTS...

Scott
LVL 2
sesurbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LacutahCommented:
Found this problem too....

Apparently, the unmanaged CDO email needs a pause between each iteration to work consistently, I used the following during each iteration:
        ...  [Code for one message] ....
        Dim t As Threading.Thread
        t = Threading.Thread.CurrentThread
        t.Sleep(100)

The t.Sleep(100) causes the .NET thread to wait 100 Milli-Seconds (1/10 a second), which seems to be enough on my server for it to not throw an exception.  Play with the value for yours (longer / shorter), it's a pain, but that's what we get for using unmanaged resources with .NET managed I suppose!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.