0

我真的在努力处理 ASP 中的错误。

我有一个脚本,它从 MS SQL 数据库发送电子邮件,它获取电子邮件地址和实际邮件本身。

脚本循环遍历地址,一次发送 50 个,以免阻塞队列。

问题在于,只要存在无效或非法的电子邮件地址,发送就会停止,并且页面会返回 500 错误。

所以我添加了 On Error Resume Next 标签,现在它似乎发送了重复的电子邮件。

我不知道如何解决这个问题,但它似乎有时会向每个地址发送 2 或 3 封电子邮件。当页码更新时,t 似乎重新发送到已发送的邮件,或者它似乎在第 1 页发送前 50 个,然后在第 2 页发送前 100 个,然后在第 3 页发送前 150 个,而不是在第 1 页发送 0-50,第 51 页-100 在第 2 页,101 - 150 在第 3 页等等。

自然,我只希望每个地址都有一封电子邮件,并且当数据库中有一封带有电子邮件地址的电子邮件时不要停止。

真的很感激一些 hlpe,因为在过去的 6 个小时里我一直在精神上试图将 If Err.Number 放在代码中的不同位置但没有成功。

感谢您的所有帮助。

<% On Error Resume Next
Response.Expires = 0
Response.Buffer=FALSE
Server.ScriptTimeout = 36000 

set cn = createobject("adodb.connection")
cn.open strCon
set rs = cn.execute("select * from mails where mailID = "& request.querystring("mailID") &"") 
    subject = rs("subject")
    mailType = rs("mailType")
    mailBody =  rs("mailBody")

    set rs = cn.execute("Select * from settings")
    strServer = rs("server")
    email = rs("email")

    page = trim(request.querystring("page"))
    If page = "" Then
    page = 1
    else
    page = cint(page)
    End If
    DisplayNum = 0

    Set oCon = Server.CreateObject ("ADODB.Connection")
    Set Rec = Server.CreateObject ("ADODB.Recordset")
    oCon.Open straCon
    Rec.PageSize = 50
    Rec.CursorLocation = adUseClient
    sql = "Select email from dbo.emailTables where emailID Between '1' and '5000'"      
    Rec.Open sql, oCon

    ipage = Rec.PageCount   

    if page = 0 or page > ipage Then
    Rec.AbsolutePage = ipage
    else
    Rec.AbsolutePage = page
    end if

    allEmail = Rec.RecordCount 

    do while not Rec.eof AND DisplayNum < 50

    If Err.Number = 0 Then

    Set ObjSendMail = CreateObject("CDO.Message")
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    ObjSendMail.Configuration.Fields.Update     
    ObjSendMail.To = Rec("email")
    ObjSendMail.Subject = subject
    ObjSendMail.From = sender
    ObjSendMail.CreateMHTMLBody "http://www.urlpage.com"
    ObjSendMail.Send     
    Set ObjSendMail = Nothing       
    DisplayNum = DisplayNum + 1         
    Rec.movenext

    Err.Clear
    End If          

    loop

    If ipage > 1 And page < ipage Then
    strPage = "post.asp?mailID="& request.querystring("mailID")&"&page="& page+1
    strTimer = 30

    else
    strPage = "overview.asp"
    strTimer = 5
    strMsg = strMsg & "<br><br>All mails sent to list. "

    end if

%>

<head>
<title>Newsletter</title>
<link href="style.css" type="text/css" rel="stylesheet">
<meta http-equiv="REFRESH" content="<%=strTimer%>; URL=<%=strPage%>">
</head>
<body>
</body>
</html>
4

1 回答 1

0

我会更改您的代码,以便您仅在发送电子邮件时捕获错误。就像现在一样,此代码中任何地方的任何错误都将被绕过,除了发送电子邮件之外,您将不知道是否存在错误。如果您将代码更改为以下内容,则只会跳过与发送电子邮件相关的错误。

<% 'On Error Resume Next '---removed
Response.Expires = 0
Response.Buffer=FALSE
Server.ScriptTimeout = 36000 

set cn = createobject("adodb.connection")
cn.open strCon
set rs = cn.execute("select * from mails where mailID = "& request.querystring("mailID") &"") 
    subject = rs("subject")
    mailType = rs("mailType")
    mailBody =  rs("mailBody")

    set rs = cn.execute("Select * from settings")
    strServer = rs("server")
    email = rs("email")

    page = trim(request.querystring("page"))
    If page = "" Then
    page = 1
    else
    page = cint(page)
    End If
    DisplayNum = 0

    Set oCon = Server.CreateObject ("ADODB.Connection")
    Set Rec = Server.CreateObject ("ADODB.Recordset")
    oCon.Open straCon
    Rec.PageSize = 50
    Rec.CursorLocation = adUseClient
    sql = "Select email from dbo.emailTables where emailID Between '1' and '5000'"      
    Rec.Open sql, oCon

    ipage = Rec.PageCount   

    if page = 0 or page > ipage Then
    Rec.AbsolutePage = ipage
    else
    Rec.AbsolutePage = page
    end if

    allEmail = Rec.RecordCount 

    do while not Rec.eof AND DisplayNum < 50

    'If Err.Number = 0 Then '---removed

    Set ObjSendMail = CreateObject("CDO.Message")
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "localhost"
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
    ObjSendMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
    ObjSendMail.Configuration.Fields.Update     
    ObjSendMail.To = Rec("email")
    ObjSendMail.Subject = subject
    ObjSendMail.From = sender
    ObjSendMail.CreateMHTMLBody "http://www.urlpage.com"
    On Error Resume Next '---added
    ObjSendMail.Send     
    On Error Goto 0 '---added
    Set ObjSendMail = Nothing       
    DisplayNum = DisplayNum + 1         
    Rec.movenext

    'Err.Clear '---removed
    'End If '---removed         

    loop

    If ipage > 1 And page < ipage Then
    strPage = "post.asp?mailID="& request.querystring("mailID")&"&page="& page+1
    strTimer = 30

    else
    strPage = "overview.asp"
    strTimer = 5
    strMsg = strMsg & "<br><br>All mails sent to list. "

    end if
%>
于 2013-09-23T23:38:01.183 回答