Search code examples
asp-classiccdo.message

Send Multiple Emails from found Recordset using CDOSys in Classic ASP


With my very limited knowledge I have pieced together the following code to send and email to every record found in my database which has an email address using CDOSys.

The emails are send OK but the page returns an error:

CDO.Message.1 error '8004020c'

At least one recipient is required, but none were found.

cdo-sys2.asp, line 42

Line 42 is = objMessage.Send

From what I have read this is to do with the loop eventually finding no record with an email address, one post said something about needing a 0 not a 1 but my knowledge is too limited to figure out where to go from here.

Many thanks to anyone who can help me finish this project.

<%
Set OBJdbConnection = CreateObject("ADODB.Connection") 
OBJdbConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("vfeast4fgrav4q3vfg3a34v12.mdb")
SQLQuery = "SELECT FirstName, Email_Address FROM AddressTable" 
Set Result = OBJdbConnection.Execute(SQLQuery) 
if Not Result.EOF then 
Do While Not Result.EOF 
SendMail Result("FirstName"), Result("Email_Address") 
Result.MoveNext 
Loop 
end if 
OBJdbConnection.Close()
Set OBJdbConnection = Nothing

Sub SendMail(TheName, TheAddress) 
Dim objMessage, Rcpt
smtpServer = "mail.mydomain.com"
body = "Hello World"

Rcpt = Chr(34) & TheName & Chr(34) & "<" & TheAddress & ">" 
set objMessage = Server.CreateObject("CDO.Message")  
set cdoConfig = Server.CreateObject("CDO.Configuration")
cdoConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
cdoConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
cdoConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 
cdoConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="[email protected]"
cdoConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="123456"
cdoConfig.Fields.Update
set objMessage.Configuration = cdoConfig
objMessage.Subject = "This Month's Sales" 
objMessage.From = """Acme Sales"" <[email protected]>" 
objMessage.To = Rcpt
objMessage.HTMLBody = body 
objMessage.Send
End Sub
set objMessage = Nothing
set cdoConfig = Nothing
%>

Solution

  • One approach would be to add an if statement in your Sub SendMail

    Sub SendMail(TheName, TheAddress) 
    If((TheName <> "") AND (TheAddress <> "")) Then
        Dim objMessage, Rcpt
        smtpServer = "mail.mydomain.com"
        body = "Hello World"
    
        Rcpt = Chr(34) & TheName & Chr(34) & "<" & TheAddress & ">" 
        set objMessage = Server.CreateObject("CDO.Message")  
        set cdoConfig = Server.CreateObject("CDO.Configuration")
        cdoConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        cdoConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpServer
        cdoConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 
        cdoConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/sendusername") ="[email protected]"
        cdoConfig.Fields ("http://schemas.microsoft.com/cdo/configuration/sendpassword") ="123456"
        cdoConfig.Fields.Update
        set objMessage.Configuration = cdoConfig
        objMessage.Subject = "This Month's Sales" 
        objMessage.From = """Acme Sales"" <[email protected]>" 
        objMessage.To = Rcpt
        objMessage.HTMLBody = body 
        objMessage.Send
    EndIf
    End Sub
    

    This way it will only process if the parameters TheName and TheAddress have a value.