Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Net.Mail New MailMessage randomly sending duplicate emails?

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("[email protected]")
    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" />
like image 987
user1342164 Avatar asked Jun 30 '16 13:06

user1342164


People also ask

Why am I getting multiple duplicate emails?

Mail forwarding loops Most often, a mail forwarding loop prevents you from receiving any mail, causing mail sent to you to bounce back to the sender. However, if you receive many copies of every email message, a forwarding loop may be the cause of this as well.

How do I stop double emails in Outlook?

Fix: Uncheck the "Leave Messages on the Server" box. To do this, click on Tools>Accounts. You will see a box open (named Internet Accounts), click "Mail" tab, click on the mail account, click on Properties>Advanced. Remove the check in the box "Leave a copy of messages on server."


2 Answers

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")
like image 93
Aethan Avatar answered Nov 15 '22 19:11

Aethan


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).

like image 41
DrMistry Avatar answered Nov 15 '22 20:11

DrMistry