使用经典ASP中的CDOSys从找到的Recordset发送多封电子邮件

时间:2013-06-23 23:43:39

标签: asp-classic cdo.message

由于我的知识非常有限,我将以下代码拼凑在一起发送并通过电子邮件发送到我的数据库中找到的每个记录,其中包含使用CDOSys的电子邮件地址。

电子邮件发送正常,但页面返回错误:

  

CDO.Message.1错误'8004020c'

     

至少需要一名收件人,但未找到任何收件人。

     

cdo-sys2.asp,第42行

     

第42行= objMessage.Send

从我所看到的,这是关于循环最终找不到带有电子邮件地址的记录,一篇文章说了一些关于需要0而不是1但我的知识太有限,无法弄清楚从哪里开始。

非常感谢能够帮助我完成这个项目的任何人。

<%
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") ="test@mail.mydomain.com"
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"" <test@mail.mydomain.com>" 
objMessage.To = Rcpt
objMessage.HTMLBody = body 
objMessage.Send
End Sub
set objMessage = Nothing
set cdoConfig = Nothing
%>

1 个答案:

答案 0 :(得分:0)

一种方法是在Sub SendMail中添加if语句

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") ="test@mail.mydomain.com"
    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"" <test@mail.mydomain.com>" 
    objMessage.To = Rcpt
    objMessage.HTMLBody = body 
    objMessage.Send
EndIf
End Sub

这样,只有参数TheName和TheAddress具有值时才会处理。