Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

In my website I am sending email from localhost.
If I specify the email address to whom(e.g.;someone@gmail.com) 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:
C#
var fromAddress = new MailAddress("myid@gmail.com", "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 = "smtp.gmail.com",
               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
                     })
try
{
    smtp.Send(message);
}
Posted
Updated 24-Dec-12 9:10am
v4
Comments
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 :-)
—SA

C#
var smtp = new System.Net.Mail.SmtpClient();
   {
       smtp.Host = "smtp.gmail.com";
       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...
 
Share this answer
 
Comments
[no name] 25-Dec-12 5:01am    
what error ??
C++
public void Messegesend(string email,string firstname,string lastname,string messege)
       {
           try
           {

               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("smtp.gmail.com");
               mail.From = new MailAddress("bizstallbevarage@gmail.com");
               mail.To.Add("dipcse08@gmail.com");
               mail.Subject = "Bizstall Bevarage & Food";
               mail.Body = messegebody;



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

mail.To.Add("dipcse08@gmail.com");
Assuming your users can type, all you have to do is execute the line:
C#
toAddress = new MailAddress(myTextbox.Text);
before you create the new MailMessage.

[edit]Typo - forgot to change fixed string to use textbox content.[/edit]
 
Share this answer
 
v2
Comments
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 code...giving error like...specified addres is not in the required form
OriginalGriff 25-Dec-12 5:01am    
"can you please edit my code"
Certainly!
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900