Solved

Managing incorrect email address in CDO

Posted on 2008-06-20
7
450 Views
Last Modified: 2008-06-24
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 = Nothing
end function

Open in new window

0
Comment
Question by:Alfahane
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 10

Accepted Solution

by:
Dxpert earned 200 total points
ID: 21833955
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 = Nothing
end function
 
 
 
'ON THE CALLING CODE, YOUR LOOP, YOU WOULD DO SOMETHING LIKE THIS:
 
dim BadEmails, BadEmail
 
'loop emails
do 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 if
	
 
loop
 
if len(trim(BadEmails)) > 0 then
	response.write (BadEmails)
end if

Open in new window

0
 
LVL 16

Assisted Solution

by:brad2575
brad2575 earned 50 total points
ID: 21834119
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).  
0
 
LVL 10

Expert Comment

by:Dxpert
ID: 21834299
Oh yeah.. Err.Clear :-D


if err.number <> 0 then
                sendemail=toEmail
                Err.Clear
else
                sendemail=""
end if
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Alfahane
ID: 21834683
Great! Thanks!! =)
0
 

Author Comment

by:Alfahane
ID: 21834734
Just realized:

This means that all errors will be supposed to be a problem with email validity.

I remember there's one error that has to be with the size of the email.

Is there no way that confirm that the error is in fact an email validity issue?

0
 
LVL 10

Expert Comment

by:Dxpert
ID: 21854891
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
0
 
LVL 10

Expert Comment

by:Dxpert
ID: 21854907
to figure out what that error is, you can, send a very long email, then use this IF statement instead:


if err.number <> 0 then
      response.write("Error Number: " & err.number & "<br />")
      response.write("Error Description: " & err.description & "<br />")
      response.end
end if
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Record locking on classic ASP 3 40
SQL Query Returns Records in SSMS but not Classic ASP 5 26
GitHub 1 10
isNaN issue on the Table input text 2 8
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question