while-loopasp-classicrecordsetcdo.message

ASP Classic, Looping through query and sending emails


I am trying to loop through a database query and send emails to the email addresses I get from the query. Here is my piece of code.

do until rs.EOF
    Set myMail = CreateObject("CDO.Message")
    myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
    myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="relay-hosting.secureserver.net"
    myMail.Configuration.Fields.Update
    myMail.Subject= subject
    myMail.From="something@Something.Something"
    myMail.To = rs("Email")
    myMail.HTMLBody = strMessage & "Some Message"
    myMail.Send
    Set myMail = Nothing
    rs.MoveNext
loop

I have searched and tried different solution without any luck. Following line of the code seems to have an issue, but i could not find what

myMail.To = rs("Email")

Before giving any answer or suggestion please keep in mind,


Solution

  • Well, as others observed in the comments, without the actual error message it`s hard to tell. There are many possible sources:

    The good thing is, you can use classic ASP error handling to find the error like this:

    on error resume next
    
    do until rs.EOF
        Set myMail = CreateObject("CDO.Message")
        myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
        myMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") ="relay-hosting.secureserver.net"
        myMail.Configuration.Fields.Update
        myMail.Subject= subject
        myMail.From="something@Something.Something"
        myMail.To = rs("Email")
        myMail.HTMLBody = strMessage & "Some Message"
        myMail.Send
    
        if err then
            Response.write "<code>" & err.Source & " - " & err.Description & "</code>"
        end if
    
        Set myMail = Nothing
        rs.MoveNext
    loop
    
    on error goto 0
    

    There are of course better ways to handle these errors, even in classic ASP (search SO or see this solution). This is only to help you find the error when you don't have access to the IIS settings.