Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8357671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:33:22+00:00 2026-06-09T10:33:22+00:00

I have a composite custom control that sends an e-mail. The mail is send

  • 0

I have a composite custom control that sends an e-mail. The mail is send on button click. However, the mail is sent after I have clicked on a button twice. Does anybody know why this is happening and how can I solve it? Here is the code:

namespace EmailControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class ServerControl1 : WebControl,INamingContainer
    {
        public string Text
        {
            get { return ViewState["Text"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["Text"] = value; }
        }

        public string Host
        {
            get { return ViewState["host"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["host"] = value; }
        }

        public string From
        {
            get { return ViewState["from"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["from"] = value; }
        }

        public int Port
        {
            get { return (int) ViewState["port"]; }
            set { ViewState["port"] = value; }
        }

        public string UserName
        {
            get { return ViewState["user"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["user"] = value; }
        }

        public string Password
        {
            get { return ViewState["pass"] as string ?? "[" + this.ID + "]"; }
            set { ViewState["pass"] = value; }
        }

        TextBox txtEmail;
        TextBox txtBody;
        TextBox txtSubject;
        Button btnSend;

        protected override void CreateChildControls()
        {
            txtEmail = new TextBox();
            txtEmail.ID = "txtEmail";
            txtEmail.AutoPostBack = true;
            LiteralControl l1 = new LiteralControl("<br/>");
            txtSubject = new TextBox();
            txtSubject.ID = "txtSubject";
            txtEmail.AutoPostBack = true;
            this.Controls.Add(txtEmail);
            this.Controls.Add(txtSubject);
            LiteralControl l2= new LiteralControl("<br/>");
            txtBody = new TextBox();
            txtBody.ID = "txtBody";
            txtBody.AutoPostBack = true;
            txtBody.TextMode = TextBoxMode.MultiLine;
            txtBody.Width = 200;
            txtBody.Height = 200;
            this.Controls.Add(txtBody);
            btnSend = new Button();
            btnSend.ID = "btnSend";
            btnSend.Width = 100;
            btnSend.Text = "Send";
            btnSend.Click += new EventHandler(OnButtonClick);
            this.Controls.Add(btnSend);
        }

        protected void OnButtonClick(object sender,EventArgs e)
        {
            TextBox txtEmail = (TextBox)this.FindControl("txtEmail");
            MailMessage msg = new MailMessage(From,txtEmail.Text);
            TextBox txtSubject = (TextBox)this.FindControl("txtSubject");
            TextBox txtBody = (TextBox)this.FindControl("txtBody");
            msg.Subject = txtSubject.Text;
            msg.Body = txtBody.Text;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = Host;
            smtp.Port = Convert.ToInt32(Port);
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential(UserName,
                Password);
            smtp.Send(msg);
        }
    }
}

The code in the aspx page is:

protected void Page_Load(object sender, EventArgs e)
{
    ServerControl11.Host = "smtp.gmail.com";
    ServerControl11.Port =25;
    ServerControl11.UserName = "*******************@gmail.com";
    ServerControl11.Password = "************";
    ServerControl11.From = "******************@gmail.com";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:33:24+00:00Added an answer on June 9, 2026 at 10:33 am

    You’re saying that the email is sent every time you click the button? If so, you just need to prevent the second postback. See here:

    How to prevent repeated postbacks from confusing my business layer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom composite control that contains a text box, some validators, as
I have a composite control which emits HTML that looks like this <span id=myControl>
I have a composite drop down calendar user control that consists of a textbox
I have designed a radio button custom control. it works fine on my computer
If I have a master 'composite custom server control', with several child custom controls,
I have created a custom server control that inherits from CompositeControl. In the CreateChildControls
I have a custom server control (composite control having dynamically created dropdown boxes and
I have a custom button control created using ATL. This control is used by
I have a Facelets Composite component that includes, among other things, a commandButton: <?xml
I am creating a custom composite control based off of an asp:Label control. I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.