Problem clearing send error in email program

E

Ed Bitzer

Trying to send groups of email with program using System.Net.Mail. I
do not clear MailMessage but repeatedly loop changing only the Bcc
entries. Works fine if all addresses are valid. As a simple test I
have attempted to send a valid address, then an invalid address which
my ISP (Comcast) will reject immediately as "Not our customer". I
catch the exception, display and then continue looping to send several
additional valid. The balance all are "caught" and display the an
error but they are sent. I have negligible knowledge of exceptions
but suspect I need to clear.

loop through the following changing only the bcc
try
smtpobject.Send(MyMailMsg)
Catch ex as Exception
msgbox(ex.tostring)
end try

Appreciate as usual,

Ed
 
M

Mr. Arnold

Ed Bitzer said:
Trying to send groups of email with program using System.Net.Mail. I do
not clear MailMessage but repeatedly loop changing only the Bcc entries.
Works fine if all addresses are valid. As a simple test I have attempted
to send a valid address, then an invalid address which my ISP (Comcast)
will reject immediately as "Not our customer". I catch the exception,
display and then continue looping to send several additional valid. The
balance all are "caught" and display the an error but they are sent. I
have negligible knowledge of exceptions but suspect I need to clear.

loop through the following changing only the bcc
try
smtpobject.Send(MyMailMsg)
Catch ex as Exception
msgbox(ex.tostring)
ex = null
or
ex = nothing 'don't know about that one for sure

---------------------------------------------------

Or you can do on Error GoTo TrapError ----- at the top of your method


TrapError:

msgbox (err.message)
err.clear

http://www.java2s.com/Tutorial/VB/0080__Statements/OnErrorGoToerrorhandle.htm
 
S

Steve Gerrard

Ed said:
Trying to send groups of email with program using System.Net.Mail. I
do not clear MailMessage but repeatedly loop changing only the Bcc
entries. Works fine if all addresses are valid. As a simple test I
have attempted to send a valid address, then an invalid address which
my ISP (Comcast) will reject immediately as "Not our customer". I
catch the exception, display and then continue looping to send several
additional valid. The balance all are "caught" and display the an
error but they are sent. I have negligible knowledge of exceptions
but suspect I need to clear.

loop through the following changing only the bcc
try
smtpobject.Send(MyMailMsg)
Catch ex as Exception
msgbox(ex.tostring)
end try

Appreciate as usual,

Ed

It looks to me like you need to reset MyMailMsg if there is an exception. The
exception will not persist past the end of the try block.
 
C

Cor Ligthert[MVP]

Ed,

Can you show the full loop, because in my idea can this not be the base of
your problem.

Cor
 
R

Rad [Visual C# MVP]

Trying to send groups of email with program using System.Net.Mail. I
do not clear MailMessage but repeatedly loop changing only the Bcc
entries. Works fine if all addresses are valid. As a simple test I
have attempted to send a valid address, then an invalid address which
my ISP (Comcast) will reject immediately as "Not our customer". I
catch the exception, display and then continue looping to send several
additional valid. The balance all are "caught" and display the an
error but they are sent. I have negligible knowledge of exceptions
but suspect I need to clear.

loop through the following changing only the bcc
try
smtpobject.Send(MyMailMsg)
Catch ex as Exception
msgbox(ex.tostring)
end try

Appreciate as usual,

Ed

Ed,

Post a copy of the entire loop. There are ways you can structure the loop
and the error handler to be more robust to exceptions
 
E

Ed Bitzer

Here is more detail - feared I had not supplied enough but this
program has sure grown big<g> for an amature.

at start, above first sub
Dim MyMailMsg as New MailMessage
Dim SmtpObj as New System.Net.Mail SmtpObject

after loading MyMailMsg to, from, subject, message, body, assigned the
Smtp Server along with cridentials to to the SmtpObj and finally
calculating numbatches

for x = 1 to numBatches
AssembleABatch (first, last, strAddressesArray) 'grabs one of more
addresses and loads MyMailMsg.bcc
SendABatch ( batchNum, logBatchArray, smtpError) 'the problem sbu
DsiplayResults ( batchNum, numBatches, smtpError) 'display some
additional progress data
PrepforNextBatch (first, last) ' increment first and last
next x

Private Sub SendABatch (byVal batchNum as integer, byRef
logBatchArray() as string, byRef smtpError as boolean)
Dim returnValue as string = ""

Try
smtpError = false
smtpObj.Send(MyMailMsg)
Catch ex as Exception
returnValue = ex.GetBaseException.toString()
msgBox(returnvalue,,"Returned Server Response")
smtpError = true
logBatchArray(batchNum) = returnValue
End Try

Do appreciate,

Ed
 
E

Ed Bitzer

Steve,

Did try (admittedly just flyer with my understanding) shifting my
creation of a new System.Net.Mail.STMPClient just before the send (and
reloading necessary content) and then clearing it (= nothing) after
each send - did not change a thing. I'll wait for Cor and Rad's
comments after seeing my code detail but I could make the loop "big"
and create a New MailMessage each pass.

Ed
 
H

Herfried K. Wagner [MVP]

Mr. Arnold said:
ex = null
or
ex = nothing 'don't know about that one for sure

No, that doesn't make sense. 'Try...Catch' as used in the OP's sample is
OK.
 
M

Mr. Arnold

Herfried K. Wagner said:
No, that doesn't make sense. 'Try...Catch' as used in the OP's sample is
OK.

I disagree with you, because I have used both methods to clear the Exception
to keep an application running when it would have stopped on the try/catch
while it was in a processing loop, particularly when processing many methods
within the processing loop and method calling other methods and all of them
having Try/Catches to prevent it from blowing up on the try/catch above it.

This method was used to look for a particular Exception in a multiple
Exceptions try/catch, and if I got the Exception to report the Exception to
a log, clear the Exception and let the processing flow back to the top of
the loop and continue process, otherwise, throw the Exception in each
method above it until it got to the top Try/Catch and terminate the
application.

I have used the method in C# and VB.Net solution with the try/catch and in
VB.Net with the Error GoTo.
 
E

Ed Bitzer

deleted a bit to the thread to shorten, comment at bottom
I disagree with you, because I have used both methods to clear the
Exception to keep an application running when it would have stopped
on the try/catch while it was in a processing loop, particularly
when processing many methods within the processing loop and method
calling other methods and all of them having Try/Catches to prevent
it from blowing up on the try/catch above it.

This method was used to look for a particular Exception in a
multiple Exceptions try/catch, and if I got the Exception to report
the Exception to a log, clear the Exception and let the processing
flow back to the top of the loop and continue process, otherwise,
throw the Exception in each method above it until it got to the top
Try/Catch and terminate the application.

I have used the method in C# and VB.Net solution with the try/catch
and in VB.Net with the Error GoTo.

I tried and ex = null constant is no longer supported
ex= nothing ok but error repeated even for valid messages which were
sent.

I have included more extensive code as requested - appreciate your
taking a look.

Ed

\
 
M

Mr. Arnold

Ed Bitzer said:
Here is more detail - feared I had not supplied enough but this program
has sure grown big<g> for an amature.

at start, above first sub
Dim MyMailMsg as New MailMessage
Dim SmtpObj as New System.Net.Mail SmtpObject

after loading MyMailMsg to, from, subject, message, body, assigned the
Smtp Server along with cridentials to to the SmtpObj and finally
calculating numbatches


for x = 1 to numBatches
try
AssembleABatch (first, last, strAddressesArray) 'grabs one of more
addresses and loads MyMailMsg.bcc
SendABatch ( batchNum, logBatchArray, smtpError) 'the problem sbu
DsiplayResults ( batchNum, numBatches, smtpError) 'display some
additional progress data
PrepforNextBatch (first, last) ' increment first and last
next x

' SendABatch should have cleared its Exception so that is no ex when it
came back
'so that the non-ex path is taken, and it should stay in the loop.

catch ex as Exception
'do whatever you're going to do here
'for me it would be to throw ex to a higher try/catch
end try

Private Sub SendABatch (byVal batchNum as integer, byRef logBatchArray()
as string, byRef smtpError as boolean)
Dim returnValue as string = ""

Try
smtpError = false
smtpObj.Send(MyMailMsg)
Catch ex as Exception
returnValue = ex.GetBaseException.toString()
msgBox(returnvalue,,"Returned Server Response")
smtpError = true
logBatchArray(batchNum) = returnValue ex = nothing
End Try
end sub

You can abondon the try/catch SendABatch and use On Error Goto to see if
that will clear the error

Private Sub()

On Error goto

SmtpError = false
smtpObj.Send(MyMailMsg)

exit sub

ErrRoutine:

returnValue = err.message
msgBox(returnvalue,,"Returned Server Response")
smtpError = true
logBatchArray(batchNum) = returnValue
err.Clear
exit sub


Also, I would be looking at logBatchArray.Count > 0. If logBatchArray.Count
0 then you had an error, and you do something with a more robust error
messages in the array to a Listview or something.

I know you can clear the Exception and let processing continue, because I
have done it.

You cannot use a try/catch and on On Error Goto in the same Sub(). It's one
or the other.

I am going to say there are no guarantees here, and you might have to play
and manipulate, roll your own.


..
 
H

Herfried K. Wagner [MVP]

Mr. Arnold said:
I disagree with you, because I have used both methods to clear the
Exception to keep an application running when it would have stopped on the
try/catch while it was in a processing loop

If you are catching the exception inside the loop it won't stop the loop.
Setting the exception variable to 'Nothing' doesn't have any influence if
you are using 'Catch ex As said:
This method was used to look for a particular Exception in a multiple
Exceptions try/catch, and if I got the Exception to report the Exception
to a log, clear the Exception

You do not have to "clear" exceptions. They are not bubbled up if they are
caught and not rethrown.
 
M

Mr. Arnold

Herfried K. Wagner said:
If you are catching the exception inside the loop it won't stop the loop.
Setting the exception variable to 'Nothing' doesn't have any influence if


You do not have to "clear" exceptions. They are not bubbled up if they
are caught and not rethrown.

Look man, you can't tell me something that I have done myself. This is
totally ridiculous, particularly when I have done it.
 
H

Herfried K. Wagner [MVP]

Mr. Arnold said:
Look man, you can't tell me something that I have done myself. This is
totally ridiculous, particularly when I have done it.

Well, just show me where the behavior you describe is specified and/or show
me a code sample that is a proof of what you are saying.
 
M

Mr. Arnold

Herfried K. Wagner said:
Well, just show me where the behavior you describe is specified and/or
show me a code sample that is a proof of what you are saying.

Sorry man, it's not my job. I got enough to do on the job. And just because
you say that something cannot happen doesn't make it so. One can think
outside the box and apply something. I have done it on more than a few
occasions to think outside the box have someone comeback and tell that they
didn't know that could be done, which I have been doing programming wise
since 1980. :)

:)
 
H

Herfried K. Wagner [MVP]

Mr. Arnold said:
Sorry man, it's not my job. I got enough to do on the job. And just
because you say that something cannot happen doesn't make it so. One can
think outside the box and apply something. I have done it on more than a
few occasions to think outside the box have someone comeback and tell that
they didn't know that could be done, which I have been doing programming
wise since 1980. :)

Well, that's nothing more than a proof that you are older than me, but it's
not a proof that you are right.

I am really wondering what you want to archieve by setting the variable 'ex'
to 'Nothing' if the exception is already caught and not rethrown. This
won't prevent it from bubbling up because it doesn't bubble up in this case
at all.
 
E

Ed Bitzer

Herfried K. Wagner said:
Well, that's nothing more than a proof that you are older than me,
but it's not a proof that you are right.

I am really wondering what you want to archieve by setting the
variable 'ex' to 'Nothing' if the exception is already caught and
not rethrown. This won't prevent it from bubbling up because it
doesn't bubble up in this case at all.

I have been testing this afternoon with the loop described and shown
here again with the additional line setting the return value to ""
Try
smtpError = false
smtpObj.Send(MyMailMsg)
Catch ex as Exception
returnValue = ex.GetBaseException.toString()
msgBox(returnvalue,,"Returned Server Response")
smtpError = true
logBatchArray(batchNum) = returnValue
returnValue = ""
End Try

I have carefully stepped through the code and monitored the MyMailMsg
structure as I attempted to send a valid address, then a invalid and
then another valid and the addresses have appeared in MyMailMsg.bcc
when I check just after the line SmtpObj.Send(MyMailMsg) yet the third
address, definitely a valid address, activates the Catch. So setting
returnValue = "" has not solved my problem.

Ed
 
M

Mr. Arnold

Herfried K. Wagner said:
Well, that's nothing more than a proof that you are older than me, but
it's not a proof that you are right.

I am really wondering what you want to archieve by setting the variable
'ex' to 'Nothing' if the exception is already caught and not rethrown.
This won't prevent it from bubbling up because it doesn't bubble up in
this case at all.

Yes, I remember now. I was throwing the Exception all the way back to the
top to the try/catch that was in the loop.
Then I looked at it with catches in the try, and if it was that one I wanted
to ignore, I just cleared the Exception and didn't throw it and just let the
program stay in the loop. Otherwise, the other catch was caught, and I throw
it there to make it come back to the top try/catch to log the message and
stop the program.

Yes, I caught it where it happened to log a message, and then I would throw
the Exception and brought it all the way back to the top, to look at it. It
was a long time ago back in 2005 when I did it to let the Windows service
application to continue processing or make the service stop.
 
E

Ed Bitzer

I apologize to the group. The original problem was failure to clear
an array and while I thought I was sending one address at a time I was
progressively adding to MailMessage's bcc array/structure (actually
called something else which escapes me). Therefore only the first
batch was clean, the second failed logically but actaully was sending
a good and a bad, and the third failed even though a good address
becuase the bcc array now inclued two good and one bad. However to
further confuse me I kept changing my code attempting to create new
object to insure "cleanliness" and tried setting my
System.Net.Mail.SmtpClient to nothing. When that did not work I got
caught up in the suggestion to introduce the ex = nothing code and my
testing proved nothing because I left a line clearing the
System.Net.Mail.Smtp Client elsewhere. I just found the mistake, the
loop is working fine and I further tested, with or without the line of
ex = nothing (in my case returnValue = "") and it is not needed.

I really had panicked after spending so many hours building this
program for the community and all of a sudden finding what appeared to
be an unsolvable flaw.

Ed
 
M

Mr. Arnold

Herfried K. Wagner said:
I am really wondering what you want to archieve by setting the variable
'ex' to 'Nothing' if the exception is already caught and not rethrown.
This won't prevent it from bubbling up because it doesn't bubble up in
this case at all.

As I don't recall all the way, I had multiple catches in the try/catch with
one the last catch(Exception as ex). I think I had to clear the Exception
that was caught on the Exception above the last one which was just the
general catch any exception.

I know I had to do something, because it was being caught and thrown when I
wanted it to fall through. I never use the Finally as that has caused other
problems in some cases.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top