CDO Error when sending Email

Posted on 2004-10-22
Last Modified: 2012-05-05
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 = ""
            '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
        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...

Question by:sesurb
    1 Comment
    LVL 9

    Accepted Solution

    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

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    931 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now