• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

Email being sent twice

I am having problems with my code. The code sends emails twice. When I step through the debugger I notice this part of the code gets execute twice even though the code only appears once in my code:

                  this.Validate();

                  SendEmail();
                  displaySection1.Visible = false;
                  displayConfirmation.Visible = true;
-----------------------------------------------------------------------------------------------
I am not sure why this occurs because I used the same code from a general email form that works fine. Here is the full code:
private void btnSubmit_Click(object sender, EventArgs e)
            {
                  this.Validate();

                  SendEmail();
                  displaySection1.Visible = false;
                  displayConfirmation.Visible = true;
            }

            private void SendEmail()
            {
                  HtmlPlaceholder emailTo = (HtmlPlaceholder) CmsPosting.Placeholders["EmailTo"];
                  
                  if(emailTo != null && emailTo.Text != string.Empty)
                  {
                        MailMessage objMessage = new MailMessage();
                        objMessage.BodyFormat = MailFormat.Text;
                              
                        StringBuilder sb = new StringBuilder();

                        sb.Append("Name    : " + txtSenderName.Text + "\n");
                        sb.Append("Date of the event   : " + txtEventDate.Text + "\n");
                        sb.Append("Name of the event : " + txtEventName.Text + "\n");
                        sb.Append("Location: " + txtLocation.Text + "\n");
                        sb.Append("Contact Name: " + txtContactName.Text + "\n");
                        sb.Append("Contact Phone: " + txtPhone.Text + "\n");
                        sb.Append("Contact e-mail: " + txtSenderEmail.Text + "\n");
                        sb.Append("Event website: " + txtEventWebsite.Text + "\n");
                        sb.Append("Sponsoring organization: " + txtOrganization.Text);

                        objMessage.To = emailTo.Text;
                        objMessage.From = this.txtSenderEmail.Text.ToLower();
                        objMessage.Subject = "Event Listing";
                        objMessage.Body = sb.ToString();

                        SmtpMail.SmtpServer = SMTPServerIP;
                        SmtpMail.Send(objMessage);
                  }
            }
0
Sanjeet
Asked:
Sanjeet
  • 4
1 Solution
 
Tom KnowltonWeb developerCommented:
First thoughts:

1)  Do you inherit from the class that contains the SendEmail( )  method?


2)  Check your events and make sure you have not wired the btnSubmit_Click(object sender, EventArgs e) twice somehow.

You should only have ONE of these:

this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);

0
 
Tom KnowltonWeb developerCommented:
You should only have ONE of these in your InitializeComponent(  ) method (which is maintained for you by the C# IDE):

this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);
0
 
Bob LearnedCommented:
If you put a break point in the routine, and run, and verify the SendEmail routine is reached twice, then you can look at the call stack, and see where this is coming from.

Bob
0
 
Tom KnowltonWeb developerCommented:
If you put a break point in the routine, and run, and verify the SendEmail routine is reached twice, then you can look at the call stack, and see where this is coming from.

Bob

================

Cool.....didn't realize you could do that.    There is so much I don't know about.
0
 
Tom KnowltonWeb developerCommented:
Thank you.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now