Duplicate Emails with System.Web.Mail.SmtpMail.Send

C

chuckdfoster

When using this bit of code, it sends two emails most of the time.
Sometimes it doesn't send two, but most of the time it does. I put the
system time (Now) in the Subject and each email has a different time by a
minute or so. I also tried to count the number of emails it is sending by
using the intEmail = intEmail + 1, but the number is always 1.Could anyone
tell me why this is happening or give me advice on how to change it. Thanks
in advance. Help is always greatly appreciated.

If Session("email") <> "" Then
intEmail = intEmail + 1
Dim strBody, strSubject as String
Dim email as New System.Web.Mail.MailMessage
strSubject = "Class Registration Confirmation" & " - " & Now & " - Email
#" & intEmail
strBody = Session("fname") & " " & Session("lname") & ", " & vbcrlf & _
"You registered for the following class." & vbcrlf & _
"Class Name: " & strClass & vbcrlf _
"Class Date: " & dtClass.DayOfWeek.ToString & ", " &
dtClass.ToShortDateString & vbcrlf & _
"Class Length: " & strLength
email.To = Session("email")
email.From = ConfigurationSettings.AppSettings("AdminEmail")
email.BodyFormat = Web.Mail.MailFormat.Text
System.Web.Mail.SmtpMail.SmtpServer =
ConfigurationSettings.AppSettings("MailServer")
System.Web.Mail.SmtpMail.Send(email)

End If
 
G

Guest

I think we have to look deeper, like to where this method is being called
from. If this is called from something fired through the Page_load event, and
you have to click a button to leave the page, then the Page_Load will acually
be 'hit' twice, since the button click will fire Page_Load before the click
event. In the above scenario, the intEmail variable will be reinitialized
unless persisted somehow (like in session) Try looking along those lines.

Hope this helps.
 
C

chuckdfoster

I think we've found the problem. I am calling it in the page_load. I'll
move it to the previous page when they click the submit button. Thank you
so much. If you don't mind another question, can you explain how it is done
twice?

Chuck Foster
 
G

Guest

This explanation is valid only if there is a button, or some other postback
event fired to leave the page... The page will always do the page_load, even
when the post is a postback. Other events are handled after the page_load
event. So, say you click to leave the page... page_load does it's thing, then
the click event handler does it's thing. Any thing in page_load gets done
again on postback.

Some things you could do to work with this behavior are:

1. Wrap the things you want to happen only once when the page loads for the
first time in an if (!IsPostBack) statement.
2. Save a counter to Session on the last load event (I use Page_PreRender)
and then load that varialble first thing in Page_Load on postback

And I'm sure there are more... Hope this helps.

Chip Pettit
 

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