Reputation: 139
I'm really struggling with handling errors in ASP.
I have a script that is sending out emails from a MS SQL database where it gets email address and the actual mail itself.
The script loops through the addresses, sending 50 at a time so as not to block the queue.
The problem is that whenever there is an invalid or illegal email address the sending stops and the page returns a 500 error.
So I added the On Error Resume Next tag and now it seems that instead it sends out duplicate emails.
I'm not sure how to resolve this but it seems it sends sometimes 2 or sometimes 3 emails to each address. As t seems to resend to already sent mails when the page number updates or it seems to send ther first 50 on page 1 then the first 100 on page 2 then the first 150 on page 3 instead of sending 0-50 on page 1, 51-100 on page 2, 101 - 150 on page 3 etc etc.
Naturally I just want one email to each address and not to stop when there's an email with an email address in the database.
Would really appreciate some hlpe as I've been going mental now for the past 6 hours trying to place If Err.Number at different places in the code without success.
Thanks for any and all help guys.
<% 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>
Upvotes: 0
Views: 765
Reputation: 10752
I would change your code so that you only trap for errors on sending of the email. As it is now, any errors anywhere in this code will be bypassed and you won't know if there are errors other than sending the email. If you change your code to the following it will only skip errors relating to sending the email.
<% '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
%>
Upvotes: 0