asp.net – System.Net.Mail新MailMessage随机发送重复的电子邮件?

前端之家收集整理的这篇文章主要介绍了asp.net – System.Net.Mail新MailMessage随机发送重复的电子邮件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用下面的代码生成一封电子邮件.出于某种原因,随机发送重复的电子邮件.它不会一直发生,一个月只发生几次.您是否看到了可能导致此问题的代码?当用户单击页面上的提交按钮时会触发它.我可以添加一些东西来防止这种情况发生吗? 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

图像按钮代码

<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" />

解决方法

我之前遇到过同样的问题,所以我想我可能会分享我的解决方案:

这是我按钮的标记代码,以避免再次重新点击它:

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

注意OnClientClick =“this.disabled = true; this.value =’发送…’.

它将禁用您的按钮并在单击后更改其文本.

此外,为了避免在刷新页面时重新保存/重新提交/重新发送数据,我只是回忆起我的表单:

Response.Redirect("~/yourForm.aspx")

猜你在找的asp.Net相关文章