Email is send twice with on click

Discussion in 'ASP.NET 2.0' started by larisaf, Aug 23, 2007.

  1. Hi!
    I have a funny problem. I have a simple code that sends email from the form. When I click on the button "Submit" I get 2 emails instead of 1. Could somebody help me to see where I made a mistake?
    Here is the code in code behind



    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim objEmail As New MailMessage


    objEmail.To.Add(New MailAddress("[email protected]"))


    objEmail.From = New MailAddress("[email protected]")


    objEmail.Subject = "Question from website"


    objEmail.Body = FName.Value & vbCrLf & " " & LName.Value & vbCrLf & " Phone: " & Phone.Value & vbCrLf & " Email: " & Email.Value & vbCrLf & " " & Comments.Value


    objEmail.Priority = MailPriority.High


    Dim SmtpMail As New SmtpClient


    SmtpMail.SmtpServer = "localhost"


    Try


    SmtpMail.Send(objEmail)


    lblMessage.Text = "Your Question has been send"


    Catch exc As Exception


    Response.Write("Send failure: " + exc.ToString())


    End Try


    End Sub


    Here is HTML:


    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" CommandName="Submit" Text="Send Your Question" ValidationGroup="AskMaria" />


    Any suggestions?
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    The code you posted should not send 2 messages.


    I suggest you check the emails header and see if they have the same messageID. If they do, then the message is simply getting downloaded twice. If not, somewhere else in the code is sending this again. May be in the onload method?


    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Hi, Bruce,


    Thanks for the reply. I checked the messages and the header is not the same.


    I took off from form tag


    method="post"


    and I got only one message, as God intended.


    Though I cannot explain why :)


    Thanks again


    Larisa
     
  4. Actually, I was too fast. I still have 2 emails. Here is my full code behind:



    Partial Class AskMaria


    Inherits System.Web.UI.Page


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim objEmail As New MailMessage


    objEmail.To.Add(New MailAddress("[email protected]"))


    objEmail.From = New MailAddress("[email protected]")


    objEmail.Subject = "Question from website"


    objEmail.Body = FName.Value &amp; vbCrLf &amp; " " &amp; LName.Value &amp; vbCrLf &amp; " Phone: " &amp; Phone.Value &amp; vbCrLf &amp; " Email: " &amp; Email.Value &amp; vbCrLf &amp; " " &amp; Comments.Value


    objEmail.Priority = MailPriority.High


    Dim SmtpMail As New SmtpClient


    'SmtpMail.SmtpServer = "localhost"


    Try


    SmtpMail.Send(objEmail)


    lblMessage.Text = "Your Question has been send"


    Catch exc As Exception


    Response.Write("Send failure: " + exc.ToString())


    End Try


    End Sub


    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click


    Response.Redirect("http://www.marsoskincare.com/askmaria.aspx")


    End Sub


    End Class


    My HTML





    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="AskMaria.aspx.vb" Inherits="AskMaria" %>





    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


    <html xmlns="http://www.w3.org/1999/xhtml" >


    <head>


    <link href="http://www.marsoskincare.com/style/marso.css" rel="stylesheet" type="text/css" />


    <link href="http://www.marsoskincare.com/style/table.css" rel="stylesheet" type="text/css" />


    <link href="http://store.artofappearance.com/merchant2/form.css" rel="stylesheet" type="text/css" />


    <title>Ask Maria</title>





    </head>





    <table class="table"><tr><td>


    <img border="0" src="http://store.artofappearance.com/merchant2/graphics/00000001/MARSO_BANNER.jpg" width="760" height="102" /></td></tr></table>


    <table class="table"><tr>


    <td id="nav" colspan="2">


    <ul>


    <li><a href="http://www.marsoskincare.com/aboutmaria.HTM">About Maria</a></li>


    <li><a href="http://www.marsoskincare.com/skinanalysis.aspx">Skin Analysis</a></li>


    <li><a href="http://www.marsoskincare.com/testimonials.htm">Testimonials</a></li>


    <li><a href="http://www.artofappearance.com/gift.htm">Gift Ideas</a></li>


    <li><a href="http://www.artofappearance.com/articles.htm">Articles</a></li>


    <li><a href="http://www.artofappearance.com/articles.htm">Ask Maria</b></a></li></ul>


    </td></tr><tr><td>


    <table id="TABLE1" style="width: 782px">


    <tr>


    <td align="left" valign="top">


    <asp:Label id="lblMessage" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Medium" ForeColor="#C00000"></asp:Label>





    <form runat="server" >


    <table >


    <tr>


    <th >First Name</th>


    <td><input id="FName" name="FName" type="text" value="" size="8" maxlength="50" runat="server" style="width:90px" />


    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FName"


    ErrorMessage="Please enter first name" Style="position: relative" ValidationGroup="AskMaria" SetFocusOnError="True">Required</asp:RequiredFieldValidator></td>


    </tr>


    <tr>


    <th >Last Name</th>


    <td><input id="LName" name="LName" value="" size="8" maxlength="50" runat="server" style="width:90px" />


    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="LName"


    ErrorMessage="Please input last name" Style="position: relative" ValidationGroup="AskMaria" SetFocusOnError="True">Required</asp:RequiredFieldValidator></td>


    </tr>


    <tr>


    <th scope="row"><label>Email</label></th>


    <td><input id="Email" name="Email" type="text" value="" size="8" maxlength="50" runat="server" style="width:90px" />


    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="Email"


    ErrorMessage="Please input valid email address" Style="position: relative" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"


    ValidationGroup="AskMaria" SetFocusOnError="True">Required</asp:RegularExpressionValidator>


    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="Email"


    ErrorMessage="Please enter email address" Style="position: relative" ValidationGroup="AskMaria"></asp:RequiredFieldValidator></td>


    </tr>


    <tr>


    <th scope="row">Phone(Optional)</th>


    <td><input id="Phone" name="Phone" type="text" value="" size="8" maxlength="50" runat="server" style="width:90px" /></td>


    </tr>


    <tr>


    <th scope="row">Your Question </th>


    <td>


    <textarea id="Comments" name="Comments" runat="server" cols="30" rows="3"></textarea>


    </td>


    </tr>


    <tr><td colspan="2">


    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" CommandName="Submit" Text="Send Your Question" ValidationGroup="AskMaria" />


    <asp:Button ID="Button2" OnClick="Button2_Click" runat="server" Text="Reset" />





    </td></tr>


    </table>


    <asp:ValidationSummary ID="ValidationSummary1" runat="server" Style="position: relative"


    ValidationGroup="AskMaria" HeaderText="Please fill in all required fields!" ShowMessageBox="True" ShowSummary="False" />


    </form>


    </td>


    <td width="249" align="center" valign="top" rowspan="2" bgcolor="#F8E4EE">


    <img border="0" src="images/Maria1.jpg" width="174" height="180" style="border: 1px solid #800080"><p style="text-align: left">


    <font face="Verdana" size="1">&amp;quot;I am constantly searching for new


    formulations.&amp;quot; says Maria. &amp;quot;I am looking for the most advanced


    technology with the highest quality ingredients. This approach


    helps me solve problems more quickly and achieve better results.


    These treatments have the strength of a powerful therapy that


    revitalizes your skin and spirit. The skin feels soft, fresh,


    vibrant and youthful - your soul is clear and free of stress. &amp;quot;</font></td>


    </tr>


    </table>





    </td></tr>


    </table>





    </body>





    </html>


    Any thoughts?
     
  5. Was the SMTP serverset? I see the following commented in your code:



    'SmtpMail.SmtpServer = "localhost"


    Also,I see that some of your asp.net controls are not within the form tags.All server controls should appear within the form tags.


    In general your aspxpage shouldbe like;


    <form id="form1" runat="server">All server+html controls here</form></form></body>


    The code within Button1_Click looks fine.There is no reason for it to send the email twice.

    Vikram

    DiscountASP.NET
    www.DiscountASP.NET
     
  6. Vvsharma!


    Thanks for looking!


    I have an error message BC30456: 'SmtpClient' is not a member of 'System.Net.Mail.SmtpClient'. when I have the line on
    'SmtpMail.SmtpServer = "localhost"


    Otherwise I did all you said still have 2 messages instead of 1

    Please advise how to set up smtpServer!
    Larisa
     
  7. Aristotle,

    Thanks, that helped!

    I still have 2 messages, though.
    Please help me, I am out of my wits

    [​IMG]
     
  8. Aristotle,


    It worked like a charm, I just took off the "Handles Button1_Click" and I get 1 message now.


    Thanks!
     
  9. The cause may be due to the fact that you have the Handles Button1.Click in the Sub and an OnClick property in the control. This is probably triggering it twice. Use one or the other.

    Aristotle

    DiscountASP.NET
    www.DiscountASP.NET
     

Share This Page