rejected recipients - CDO email message

  • Thread starter Thread starter MBurns
  • Start date Start date
M

MBurns

Hello, I have a process that loops through email addresses in a table and
sends out letters using CDO and exchange. This generally works fine but if
there is an error in the recipient address then an error message comes back
and the whole process crashes. I have to remove or correct the address and
restart the process.

Is there a way to catch these errors and deal with them without crashing the
process?

Thanks for any advice.
 
Below is the code I'm using. It doesn't have any error handling at the
moment.I call it from another function which passes all of the variables.

Public Function SendTKEmailTo(strMailFrom, strMailTo, strMailCC, strSubject,
strMailBody)



Set myMailApp = CreateObject("CDO.Message")
myMailApp.Subject = strSubject
myMailApp.Sender = strMailFrom
myMailApp.To = strMailTo
myMailApp.CC = strMailCC


myMailApp.TextBody = strMailBody


myMailApp.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

myMailApp.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "our
company's smtp server"
myMailApp.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

myMailApp.Configuration.Fields.Update

myMailApp.Send
Set myMailApp = Nothing

End Function
 
Try something like

Public Function SendTKEmailTo(strMailFrom, strMailTo, strMailCC, strSubject,
strMailBody)
On Error GoTo Error_Handler

Set myMailApp = CreateObject("CDO.Message")
myMailApp.Subject = strSubject
myMailApp.Sender = strMailFrom
myMailApp.To = strMailTo
myMailApp.CC = strMailCC


myMailApp.TextBody = strMailBody


myMailApp.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2

myMailApp.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "our
company 's smtp server"
myMailApp.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25

myMailApp.Configuration.Fields.Update

myMailApp.Send
Set myMailApp = Nothing

If Err.Number = 0 Then Exit Function

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: SendTKEmailTo" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function
End Function

Now run it again and pay attention to the error number that is raised so you
can trap it by switching the

Error_Handler:
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: SendTKEmailTo" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function

With something like

Error_Handler:
If Err.Number = 1111 Then
Resume Next
Else
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf &
"Error Number: " & _
Err.Number & vbCrLf & "Error Source: SendTKEmailTo" & vbCrLf & "Error
Description: " & _
Err.Description, vbCritical, "An Error has Occured!"
Exit Function
End If
Where you replace 1111 with your error code and you tell access to simple
continue with the routine anyways.

Try it and post back if you still have problems.
--
Hope this helps,

Daniel Pineault
http://www.cardaconsultants.com/
For Access Tips and Examples: http://www.devhut.net
Please rate this post using the vote buttons if it was helpful.
 
Back
Top