• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1069
  • Last Modified:

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
0
sesurb
Asked:
sesurb
1 Solution
 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now