locked
one click and mail goes 3 times with the same c# code RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    This time I have very strange issue with my c# code.

    Actually I am sending mail using  this code (As I done many times before). But sometimes it behave in very strange manner. Sometimes after execute the code the mail goes 2 or 3 times with  exact same date and time. Even I have disabled the button after single click so that it can't click for second time.

    I am using office365's outlook. and c# the code is below.

    protected void Mail_Approve_Travel()
        {
            MailMessage msgMail = new MailMessage("antara@inspirenetwork.org", lblUserEmail.Text);//
    
          
            msgMail.Subject = "Your travel authorisation has been approved for Travel No. "+ lblTravelID.Text;
    
          
            msgMail.IsBodyHtml = true;
            string Emailbody = "";
            Emailbody += "<style type=text/css>";
            Emailbody += "<!--";
            // html code goes here
            Emailbody += "</html>";
    
    
            string strBody = Emailbody;
            msgMail.Body = strBody;
            msgMail.Priority = MailPriority.Normal;
    
            SmtpClient client = new SmtpClient("smtp.office365.com");
            client.Port = 587;
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
    
            NetworkCredential cred = new System.Net.NetworkCredential("antara@inspirenetwork.org", "password@123");
            client.Credentials = cred;
    
    
    
    
    
    
            //   SmtpClient smtpClnt = new SmtpClient();
            // smtpClnt.Host = "111.111.0.1"; // some IP address
            try
            {
                client.Send(msgMail);

    btnSendmail.Enabled= false; Response.Write("Your Email has been sent sucessfully- Thank You"); } catch (Exception ex) { Response.Write("Send failure: " + ex.ToString()); } }

    Please let me know.

    Thursday, August 4, 2016 4:37 AM

Answers

  • User283571144 posted

    Hi  demoninside9,

    Now I think I should disable button after clicking on client side not server side.

    Am I thinking write for the solution?

    As far as I know, if you want to disable button after clicking on client side not server side. You could use javascript "window.onbeforeunload" method.

    More details, you could refer to follow codes:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script src="Scripts/jquery-1.8.1.js"></script>
    <script type = "text/javascript">
    function DisableButton() {
        document.getElementById("<%=btnSubmit.ClientID %>").disabled = true;
    }
    window.onbeforeunload = DisableButton;
    </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Button ID="btnSubmit" runat="server"  Text="Submit" OnClick="btnSubmit_Click"   />
        </div>
        </form>
    </body>
    </html>

    Code-behind:

        protected void Page_Load(object sender, EventArgs e)
            {
           
            }
    
            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                Thread.Sleep(1000);
                Response.Write("Show i");
            }

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 6, 2016 2:02 PM

All replies

  • User753101303 posted

    Hi,

    How do you disable the button? What if you double click or triple click the button right away? You could also add a trace to make sure if the issue is on the Office 365 side or if it is just that your code is still called multiple times.

    Thursday, August 4, 2016 7:26 AM
  • User1052662409 posted

    How do you disable the button?

    btnSendmail.Enabled= false;
    Thursday, August 4, 2016 8:42 AM
  • User753101303 posted

    So it is done on the server side which prevent the user to use the button once the page is refreshed but shouldn't prevent the user to just double click the button right away. Have you tried to double click the button to reproduce this?

    Thursday, August 4, 2016 8:54 AM
  • User1052662409 posted

    Have you tried to double click the button to reproduce this?

    How can I please suggest.

    Thursday, August 4, 2016 8:57 AM
  • User753101303 posted

    Just show the form and rather than clicking the button just click the button and press again immediatly the mouse button to https://en.wikipedia.org/wiki/Double-click it (or even click-click-click in close succession). Then do you see that the mail has been sent 2 or 3 times? I'm pretty sure this is what some users are doing but it's likely bes tto do a quick check so that you can reproduce the issue, fix the issue, and do the same test again to check it has been properly fixed.

    Thursday, August 4, 2016 9:04 AM
  • User1052662409 posted

    it's likely bes tto do a quick check so that you can reproduce the issue, fix the issue

    How to do checking sir? please suggest.

    Thursday, August 4, 2016 9:46 AM
  • User753101303 posted

    Humm not sure what to say. I'm trying to tell you that you could just click the mouse button twice quickly (which might cause 2 mails being sent) rather than to click and then wait, (which causes 1 mail to be sent). Not sure how to clarify what is a "single click" vs what is a "double click" ?!

    I'm not talking for now about fixing the issue. This is just to make 100% sure this is likely the issue (or at least that this issue could happen).

    Then we'll deal with how to fix this.

    Then you'll be able to do what you tried earlier and see it is fixed.

    Thursday, August 4, 2016 9:54 AM
  • User1052662409 posted

    This is just to make 100% sure this is likely the issue (or at least that this issue could happen)

    As you said this is the only reason. Sometimes out intranet works slow after clicking when it post backs user thinks that it was not clicked or he/she impatiently clicks it many times.

    Now I think I should disable button after clicking on client side not server side.

    Am I thinking write for the solution?

    Thursday, August 4, 2016 10:09 AM
  • User283571144 posted

    Hi  demoninside9,

    Now I think I should disable button after clicking on client side not server side.

    Am I thinking write for the solution?

    As far as I know, if you want to disable button after clicking on client side not server side. You could use javascript "window.onbeforeunload" method.

    More details, you could refer to follow codes:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <script src="Scripts/jquery-1.8.1.js"></script>
    <script type = "text/javascript">
    function DisableButton() {
        document.getElementById("<%=btnSubmit.ClientID %>").disabled = true;
    }
    window.onbeforeunload = DisableButton;
    </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:Button ID="btnSubmit" runat="server"  Text="Submit" OnClick="btnSubmit_Click"   />
        </div>
        </form>
    </body>
    </html>

    Code-behind:

        protected void Page_Load(object sender, EventArgs e)
            {
           
            }
    
            protected void btnSubmit_Click(object sender, EventArgs e)
            {
                Thread.Sleep(1000);
                Response.Write("Show i");
            }

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 6, 2016 2:02 PM