Sending Email

Discussion in 'ASP.NET 2.0' started by kishorepalle, Nov 8, 2010.

  1. Dear Members,

    I am trying to send an email. And this is what the error message I am hitting,

    The specified string is not in the form required for an e-mail address.

    My code works with no problems when the To address is hard coded as below,

    mail.To.Add("[email protected]");

    But I have a text box from which the "to" email address should be picked up. That is when the above error (in bold) appears.

    Any help is very much appreciated!

    Here is the code,

    ASPX Page:

    Code:
    To Email Address: <asp:TextBox ID="toemai_Box" runat="server"></asp:TextBox>
        <p></p>
        <asp:Button ID="email_btn" runat="server" Text="Send Email" onclick="email_btn_Click"></asp:Button>
    
    Event Handler in Code Behind:

    Code:
    protected void email_btn_Click(object sender, EventArgs e)
            {
                try
                {
                    string to_Email = this.toemai_Box.ToString();
                    //create the mail message
                    MailMessage mail = new MailMessage();
                    //set the addresses
                    //mail.From = new MailAddress("[email protected]");
                    mail.From = new MailAddress("[email protected]", "Company");
                    //mail.To.Add("[email protected]");
                    mail.To.Add(to_Email);
                    //set the content
                    mail.Subject = "Welcome to Company";
                    mail.Body = "Hello user, we welcomes you!";
                    //send the message
    
                    SmtpClient smtp = new SmtpClient("localhost");
                   // SmtpClient smtp = settings.Smtp.Network.Host.ToString();
                    smtp.Send(mail);
                    this.lblMessage.Visible = true;
                    lblMessage.Text = "Mail Sent";
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
    
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    should this line

    string to_Email = this.toemai_Box.ToString();

    be

    string to_Email = this.toemai_Box.Text.ToString();
     

Share This Page