1

On the line

const string subject = "hello" + textBox1.Text;

it doesn't print the value of textBox1.Text but i print the word "textBox1.Text". How can I fix it?

var fromAddress = new MailAddress("samuimark@gmail.com", "samui mail");
var toAddress = new MailAddress("teeramail@gmail.com", "To Name");
const string fromPassword = "t429";
const string subject = "hello" + textBox1.Text  ;
const string body = "you have a mail";

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
})
{
    smtp.Send(message);
}
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123

3 Answers3

4

Instead of

const string subject = "hello" + textBox1.Text  ;

Use

string subject = "hello" + textBox1.Text;
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • Other guys said the same thing technically. But according to type of question I realized copy/paste matters here. Happy it worked for you. – Xaqron Apr 14 '13 at 02:35
0

textBox1.Text is not constant expression. assing its value to string not const string

string subject = "hello" + textBox1.Text;
0

Insted of using

Subject = subject,
Body = body

I recommend you to change it too like

Subject = subject.Text,
Body = body.Text

Just change "subject.Text" and "body.Text" to whatever your text boxes are named But you must keep the .Text