System.Net.Mail - 发送两封电子邮件?

时间:2012-08-24 20:22:48

标签: asp.net vb.net smtp

使用以下功能:

Public Sub SendMail(ByVal SendFrom As String, ByVal SendTo As String, ByVal Subject As String, ByVal Body As String)

    Dim client As New SmtpClient
    Dim message As New MailMessage

    message.Body = Body
    message.Subject = Subject
    message.From = New MailAddress(SendFrom)
    message.To.Add(New MailAddress(SendTo))

    client.Port = "25"
    client.Host = "smtp.myserver.com"

    client.Send(message)

End Sub

我用

打电话
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click

    Dim iandamsb As New StringBuilder

    iandamsb.AppendLine("Please make the following changes:")
    iandamsb.AppendLine("")
    iandamsb.AppendLine("Current name:" & txtCurrentName.Text)
    iandamsb.AppendLine("New name:" & txtNewName.Text)
    iandamsb.AppendLine("New username:" & txtNewUsername.Text)
    iandamsb.AppendLine("Applications:" & txtOtherApplications.Text)

    Dim iandambody As String = iandamsb.ToString

    SendMail(txtRequesterEmail.Text, "ayockel@mydomain.com", "Name Change Request - " & txtCurrentName.Text, iandambody)

End Sub

它工作正常,但它发送两封电子邮件而不是一封。任何人都可以弄明白为什么它会发送副本吗?

1 个答案:

答案 0 :(得分:3)

我冒昧地猜测你有按钮点击事件绑定两次:一次通过标记中的OnClick属性:

<asp:Button OnClick="btnSubmit_Click" runat="server" ... />

然后再通过代码隐藏Handles

Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) 
    Handles btnSubmit.Click
如果是这样的话,我会删除其中一个。我会保留后者,因此您知道btnSubmit_Click事件在编译时已正确连接。

A discussion of this issue