7

I am using the code below to generate an email. For some reason, randomly it will send a duplicate email out. It does not happen all the time, just a couple times a month. Do you see anything with my code that might cause this? It is fired when the user clicks a submit button on the page. Is there something I can add to this to prevent this from happening? TIA

Try
    Dim Attachment As String
    Attachment = path + myUniqueFileName

    Dim mailMessage As MailMessage = New MailMessage
    mailMessage.From = New MailAddress("Test@Test.com")
    mailMessage.Subject = "Report " + " " + myUniqueFileName
    mailMessage.IsBodyHtml = True
    mailMessage.To.Add(New MailAddress(Session("EmailAddress")))
    mailMessage.Attachments.Add(New Attachment(Attachment))
    mailMessage.Body = "Attached is your report"

    Dim smtp As SmtpClient = New SmtpClient

    smtp.Host = "mail.net"

    Dim NetworkCred As System.Net.NetworkCredential = New System.Net.NetworkCredential

    smtp.Credentials = New NetworkCredential("test", "test")
    smtp.UseDefaultCredentials = False
    smtp.Send(mailMessage)

Catch ex As Exception

    Dim message As String = ex.ToString
    Dim sb As New System.Text.StringBuilder()
    sb.Append("<script type = 'text/javascript'>")
    sb.Append("window.onload=function(){")
    sb.Append("alert('")
    sb.Append(message)
    sb.Append("\n")
    sb.Append(String.Format("{0:f2}", Convert.ToDouble(TotalAmount)))
    sb.Append("')};")
    sb.Append("</script>")
    ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", sb.ToString())

End Try

Image Button Code:

 <asp:ImageButton ID="cmdFinish" runat="server" Height="38px" ImageUrl="~/Images/Finish.png" Width="99px" UseSubmitBehavior="false" OnClientClick="this.disabled = true; this.value = 'Sending...';" Text="Send" />
user1342164
  • 1,434
  • 13
  • 44
  • 83
  • 2
    Just a guess, if the user press back button of the browser or refresh the page(after the mail being sent), does this trigger again the postback? – Matteo Umili Jun 30 '16 at 13:06
  • It could also be the Session. When you set `Session["EmailAddress"]`, is there anyway that the User's email is being added more than once? – Kramb Jun 30 '16 at 13:09
  • I think it will trigger another post back if the page is refreshed. Is that where I would add if is not postback? – user1342164 Jun 30 '16 at 13:36
  • 2
    What happens if someone double clicks the submit button? – entropic Sep 08 '16 at 20:21
  • Looks like that might be possibly what is happening. Is there a way to prevent a button from being pressed again? Once the button is pressed, the data is submitted then the page is returned to the home page – user1342164 Sep 08 '16 at 20:24
  • 2
    Sure, add a javascript event handler on the click that immediately disables the button - that way it can't be clicked again. – entropic Sep 08 '16 at 20:29
  • Thanks, I did this all in VB.net I am not familiar with javascripting. Is there an example of this somewhere? Thank you – user1342164 Sep 08 '16 at 20:31
  • 1
    http://stackoverflow.com/questions/3366828/how-to-disable-submit-button-once-it-has-been-clicked – entropic Sep 08 '16 at 20:32
  • Can you show in the question the markup for the button and the code of the event handlers that are attached to it (on both the client and the server side)? – ConnorsFan Sep 08 '16 at 21:19
  • I added my image button code modified with the suggestions below and now when I click on it nothing happens. Any idea? – user1342164 Sep 09 '16 at 12:34

2 Answers2

3

I've experienced the same issue before, so I think I might share my solution:

This is my mark-up code for the button to avoid re-clicking it again:

<asp:Button ID="btnSend" runat="server" CssClass="btn btn-primary" Width="150px" UseSubmitBehavior="false" OnClientClick="this.disabled = true; this.value = 'Sending...';" Text="Send" />

Notice OnClientClick="this.disabled = true; this.value = 'Sending...'.

It will disable your button and change its text after clicking it.

Also, to avoid re-saving/resubmission/resending of data when the page is refreshed, I just recalled my form:

Response.Redirect("~/yourForm.aspx")
Aethan
  • 1,986
  • 2
  • 18
  • 25
  • I added my image button code above modified with the suggestions and now when I click on it nothing happens. Any idea? – user1342164 Sep 09 '16 at 12:35
  • 1
    I suggest that you use `setTimeout` to disable the button asynchronously, as you can see in my answer to this post: http://stackoverflow.com/questions/37955604/disabled-button-doesnt-submit-postback-in-updatepanel/37955693#37955693. Otherwise, the postback will not be performed. – ConnorsFan Sep 09 '16 at 12:46
  • 1
    I think it's not working because it is an `ImageButton`. Correct me if I'm wrong but as I remember I couldn't make it work also using a `LinkButton` so I resorted to using a normal button. – Aethan Sep 09 '16 at 15:08
  • According to my tests on Chrome, the postback does not occur if the button is disabled synchronously. It works on IE and Firefox however. – ConnorsFan Sep 09 '16 at 15:57
  • Thank you everyone – user1342164 Sep 09 '16 at 17:12
  • Happy coding! Cheers! – Aethan Sep 10 '16 at 00:27
2

Firstly, you're using a few objects which should be .Dispose()'d when you're done with them (I prefer Using blocks myself) - not saying this is the cause of the repeat sends, but it may be best to eliminate it as a possibility and it is best practice. Here is a handy article about Usings and alternative strategies which you may find useful.

Secondly, do you have access to the SMTP logs for the server which is being used to send the message? That could be worth looking in to. If you see 2 messages within a short space of time, then you can bet it's a button double-click. Crush Sundae's method for disabling the button when it's clicked should really deal with that problem, but you may find some value in examining the documentation for the OnClientClick property of the button (here).

DrMistry
  • 321
  • 1
  • 13