Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:
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 24-Dec-12 9:11am
Edited 24-Dec-12 10:10am
v4
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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...
  Permalink  
Comments
Krunal Rohit at 25-Dec-12 5:01am
   
what error ??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

       
 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();
            }
        }
  Permalink  
Comments
symonsarwar at 25-Dec-12 5:11am
   
this toAddress should come from the textbox.text
 
mail.To.Add("dipcse08@gmail.com");
Rate this: bad
good
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]
  Permalink  
v2
Comments
OriginalGriff at 25-Dec-12 3:15am
   
Answer updated
symonsarwar at 24-Dec-12 16:15pm
   
u know user cannt type in the code behind
symonsarwar at 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 at 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)

  Print Answers RSS
0 OriginalGriff 406
1 /\jmot 180
2 Suraj Sahoo | Coding Passion 170
3 Afzaal Ahmad Zeeshan 159
4 BillWoodruff 154
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 27 Feb 2013
Copyright © CodeProject, 1999-2014
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