Click here to Skip to main content
13,087,235 members (67,672 online)
Rate this:
Please Sign up or sign in to vote.
See more:

In my website I am sending email from localhost.
If I specify the email address to whom(e.g.; I want to send email it succeeds, but I would like to retreive the (var toAddress) value from the text box control.
If i do this it gives the error:
"The specified string is not in the form required for an e-mail address."

The code to send a email looks like this:
var fromAddress = new MailAddress("", "sarwar");
var toAddress = new MailAddress("GetValueFrom The Textbox","name");// Here I would like to get value from the text box.
const string fromPassword = "password"; 
const string subject = "lokman";
const string body = "i have done it";
var smtp = new SmtpClient
               Host = "",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
using (var message = new MailMessage(fromAddress, toAddress)
                         Subject = subject,
                         Body = body
Posted 24-Dec-12 8:11am
Updated 24-Dec-12 9:10am
Sergey Alexandrovich Kryukov 24-Dec-12 14:56pm
In what line? Anyway, this is not the proper question. You as a user supply wrong string. Write a right one :-)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

var smtp = new System.Net.Mail.SmtpClient();
       smtp.Host = "";
       smtp.Port = 587;
       smtp.EnableSsl = true;
       smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
       smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
       smtp.Timeout = 20000;
   // Passing values to smtp object
   smtp.Send(fromAddress, toAddress, subject, body);

Try this code, If its not working, let me know...
Krunal Rohit 25-Dec-12 5:01am
what error ??
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

 public void Messegesend(string email,string firstname,string lastname,string messege)
                string messegebody = "Dear Admin  " + "\n" + messege + "\n Thanks \n " + firstname + " " + lastname + " \n" + email;
                System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
                SmtpClient SmtpServer = new SmtpClient("");
                mail.From = new MailAddress("");
                mail.Subject = "Bizstall Bevarage & Food";
                mail.Body = messegebody;

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("sender_email", "sender_password");
                SmtpServer.EnableSsl = true;
            catch (Exception ex)
                string error = ex.ToString();
symonsarwar 25-Dec-12 5:11am
this toAddress should come from the textbox.text

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Assuming your users can type, all you have to do is execute the line:
toAddress = new MailAddress(myTextbox.Text);
before you create the new MailMessage.

[edit]Typo - forgot to change fixed string to use textbox content.[/edit]
OriginalGriff 25-Dec-12 3:15am
Answer updated
symonsarwar 24-Dec-12 16:15pm
u know user cannt type in the code behind
symonsarwar 25-Dec-12 4:04am
its not working, can you please edit my error like...specified addres is not in the required form
OriginalGriff 25-Dec-12 5:01am
"can you please edit my code"
Just wrap your computer up securely, and send it DHL or UPS to me, and I'll do what I can...

I think it will probably be quicker if you edit it yourself...:laugh:

Remember I can't see you code from here, so you have to think as well here - where is your textbox? What is it called? Where is your method? And what is in the textbox?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100