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

Discussion in 'ASP .Net' started by chuckdfoster, Feb 22, 2005.

  1. chuckdfoster

    chuckdfoster Guest

    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
     
    chuckdfoster, Feb 22, 2005
    #1
    1. Advertising

  2. 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
    >
    >
    >
     
    =?Utf-8?B?Q2hpcCBQZXR0aXQ=?=, Feb 22, 2005
    #2
    1. Advertising

  3. chuckdfoster

    chuckdfoster Guest

    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" <> wrote in message
    news:...
    > 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
    > >
    > >
    > >
     
    chuckdfoster, Feb 23, 2005
    #3
  4. 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" <> wrote in message
    > news:...
    > > 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
    > > >
    > > >
    > > >

    >
    >
    >
     
    =?Utf-8?B?Q2hpcCBQZXR0aXQ=?=, Mar 1, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leonard
    Replies:
    2
    Views:
    649
    Leonard
    Aug 20, 2003
  2. DotNetGruven
    Replies:
    3
    Views:
    1,081
    Steven Cheng[MSFT]
    Apr 27, 2004
  3. Eric van Wijk
    Replies:
    0
    Views:
    2,296
    Eric van Wijk
    May 25, 2005
  4. Nathan Sokalski
    Replies:
    3
    Views:
    1,963
    Larry Lard
    Nov 23, 2005
  5. Replies:
    4
    Views:
    3,603
Loading...

Share This Page