Pickle
Pickle

Reputation: 1124

System.Net.Mail - Sending two emails?

Using the following function:

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

I call it with

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, "[email protected]", "Name Change Request - " & txtCurrentName.Text, iandambody)

End Sub

It works just fine, however it is sending two emails instead of one. Can anyone figure out why it's sending a duplicate?

Upvotes: 0

Views: 1047

Answers (1)

Cᴏʀʏ
Cᴏʀʏ

Reputation: 107606

I would venture to guess that you have the button click event bound twice: once through an OnClick attribute in the markup:

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

and then again through the code-behind via Handles:

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

I would remove one of them if that's the case. I would keep the latter so you know that the btnSubmit_Click event is properly wired at compile time.

A discussion of this issue.

Upvotes: 3

Related Questions