By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 1,727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

--
Chuck Foster
Programmer Analyst
Eclipsys Corporation - St. Vincent Health System
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

"chuckdfoster" wrote:
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

--
Chuck Foster
Programmer Analyst
Eclipsys Corporation - St. Vincent Health System

Nov 19 '05 #2

P: n/a
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

"Chip Pettit" <Ch********@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
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.

"chuckdfoster" wrote:
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

--
Chuck Foster
Programmer Analyst
Eclipsys Corporation - St. Vincent Health System

Nov 19 '05 #3

P: n/a
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

"chuckdfoster" wrote:
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

"Chip Pettit" <Ch********@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.com...
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.

"chuckdfoster" wrote:
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

--
Chuck Foster
Programmer Analyst
Eclipsys Corporation - St. Vincent Health System


Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.