How do I manage the error produced by CDO (in classic ASP/VBScript) when a receiver email address is incorrect?
Right now if the email address is incorrect and 8004020e is produced. I understand why the problem occurs but I don't know how to handle it. This function is used to send out quite a lot of emails so I want to avoid
The best thing (I think) would be to if CDO could return some kind of indication that the address is incorrect without causing the entire script to fail. However, suggestions are more than welcome.
function(smtp,toEmail,fromEmail,emailHeader,emailBody) dim Mailer, cdoConfig Set Mailer = 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") = smtp cdoConfig.Fields.Update set Mailer.Configuration = cdoConfig Mailer.To = toEmail Mailer.From = fromEmail Mailer.Subject = emailHeader Mailer.TextBody = emailBody Mailer.Send if err.number>0 then sendemail=false else sendemail=true end if Set Mailer = Nothing Set cdoConfig = Nothingend function
You can use ON ERROR RESUME NEXT and return the bad email from your function instead of the True/False.... see code and pseudo code:
function sendemail (smtp,toEmail,fromEmail,emailHeader,emailBody) dim Mailer, cdoConfig On Error Resume Next Set Mailer = 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") = smtp cdoConfig.Fields.Update set Mailer.Configuration = cdoConfig Mailer.To = toEmail Mailer.From = fromEmail Mailer.Subject = emailHeader Mailer.TextBody = emailBody Mailer.Send if err.number <> 0 then sendemail=toEmail else sendemail="" end if Set Mailer = Nothing Set cdoConfig = Nothingend function'ON THE CALLING CODE, YOUR LOOP, YOU WOULD DO SOMETHING LIKE THIS:dim BadEmails, BadEmail'loop emailsdo while.... 'try to send email BadEmail = trim(sendemail(mtp,toEmail,fromEmail,emailHeader,emailBody)) 'buld a list of bad emails if len(BadEmail) > 0 then BadEmails = BadEmails & "," & BadEmail end ifloopif len(trim(BadEmails)) > 0 then response.write (BadEmails)end if
if you just want to capture the errors you could put
on error resume next
so if there is an error then the code will not die but continue processing.
then after the Mailer.Send is called you can check (as you are doing) if there is an error. Then do whatever you want to do for errors (log them or just ignore them).
Then do this:
err.clear
clears out the error code so the next loop (or any other code will not error out if you have error trapping).
if err.number <> 0 then
sendemail=toEmail
Err.Clear
else
sendemail=""
end if
0
An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
I guess if you figure out what the error number is, then you can use that. For instance, if that error number would be 55555555 then you could do something like this:
if err.number <> 0 then
if err.number <> 55555555 then
sendemail=toEmail
Err.Clear
end if
else
sendemail=""
end if
The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.
One of a set of tools we're offering as a way of saying thank you for being a part of the community.
Open in new window