如何处理ASP中的Err.Number?

时间:2013-09-23 16:23:42

标签: sql-server-2005 asp-classic vbscript

我真的很难处理ASP中的错误。

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

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

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

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

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

当然,我只想要一封电子邮件发送到每个地址,而不是在数据库中有电子邮件地址的电子邮件时停止。

我真的很感激一些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

%GT;

<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>

1 个答案:

答案 0 :(得分: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
%>