while-loop - ASP Classic,循环查询并发送电子邮件

标签 while-loop asp-classic recordset cdo.message

我正在尝试循环执行数据库查询并将电子邮件发送到我从查询中获得的电子邮件地址。 这是我的一段代码。

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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86f5e9ebe3f2eeefe8e1c6d5e9ebe3f2eeefe8e1a8d5e9ebe3f2eeefe8e1" rel="noreferrer noopener nofollow">[email protected]</a>"
    myMail.To = rs("Email")
    myMail.HTMLBody = strMessage & "Some Message"
    myMail.Send
    Set myMail = Nothing
    rs.MoveNext
loop

我搜索并尝试了不同的解决方案,但没有任何运气。 以下代码行似乎有问题,但我找不到问题

myMail.To = rs("Email")

在给出任何答案或建议之前,请记住,

  • 当我在 CDO 消息对象的“TO”属性中使用任何静态电子邮件地址时,此代码工作正常
  • 查询给出了准确的结果
  • 当我在 CDO 消息对象的“TO”属性中使用动态电子邮件(来自查询的电子邮件)时,我收到 500 内部服务器错误。我无权访问 IIS 来启用详细的错误消息。

最佳答案

嗯,正如其他人在评论中观察到的那样,如果没有实际的错误消息,就很难判断。有很多可能的来源:

  • 由于某种原因记录集此时已关闭
  • 记录集中没有“电子邮件”字段
  • 电子邮件地址无效
  • ...

好处是,您可以使用经典的 ASP 错误处理来查找错误,如下所示:

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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ffce0e2eafbe7e6e1e8cfdce0e2eafbe7e6e1e8a1dce0e2eafbe7e6e1e8" rel="noreferrer noopener nofollow">[email protected]</a>"
    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

当然有更好的方法来处理这些错误,即使在经典 ASP 中也是如此( search SO 或参见 this solution )。这只是为了帮助您在无法访问 IIS 设置时找到错误。

关于while-loop - ASP Classic,循环查询并发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35349437/

相关文章:

delphi - 如何防止TADORecordset滚动?

Java循环未达到停止条件程序结束

PHP/MySQL : how to loop matching results from a query already inside a while loop

Python Selenium 如何处理 while 循环/if else 语句中的 NoSuchElementException

mysql - ADO 中的参数化查询 - 将数据添加到数据库中

python - 我怎样才能添加apache asp和python(在linux而不是windows上)?

Java - 错误消息未知原因

vbscript - 如何使用存储在应用程序中的类?

ms-access - 连续表格 - 选定记录的开放表格

asp.net - 当DataSource中没有数据时,DetailsView不可见