Send Multiple Textbox Values in Mail Body using SMTP

.net, c#, email, sendmail, winforms

Solution

You can use `StringBuilder`:

StringBuilder sb = new StringBuilder();
sb.AppendLine("Name: " + tbName.Text);
sb.AppendLine("SurName: " + tbSurName.Text);
message.Body = sb.ToString();

or string.Format:

message.Body = string.Format(@"
   Name: {0} 
   SurName: {1}
", tbName.Text, tbSurName.Text);

I prefer `string.Format` version. There are many other ways like Array joining, but this two are all enough.

Edit for HTML (from comment):

message.Body = string.Format(@"
    <html>
      <body>
        <div>
          <span class=""name"">Name:</span> <span class=""value"">{0}</span>
        </div>
        <div>
          <span class=""name"">SurName:</span> <span class=""value"">{1}</span>
        </div>
      </body>
    </html>", tbName.Text, tbSurName.Text);

Problem

Guys I am trying to send mail through SMTP server. I am sending the mail using this code, ``` using(System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage()) { message.To.Add(textBox1.Text); //TextBox1 = Send To message.Subject = textBox2.Text; //TextBox2 = Subject message.From = new System.Net.Mail.MailAddress("email id"); message.Body = textBox3.Text; //TextBox3 = Message Body using(System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient()) { smtp.Host = "smtp.server.com"; smtp.Credentials = new System.Net.NetworkCredential("user", "pass"); smtp.Send(message); } } ``` Now this code works perfectly. Now I want to send a whole form in the body of the mail, like selecting values of multiple textboxes. This is the form, I want to send in the mail : How can I design a message template so that it may send a message with body containing all the values you see in the above form?

Original source

Related problems