5

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 :

enter image description here

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?

Max
  • 11,885
  • 15
  • 70
  • 96
NewbieProgrammer
  • 844
  • 2
  • 17
  • 49

3 Answers3

3

Use StringBuilder and build up a string from all of your textboxes in the desired format and assign it to message.Body

If you want the output to be displayed like your form you can add HTML to the string which will be rendered by the e-mail client, however you cannot use CSS for styling.

As an example:

    private string BuildMessageBody()
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<html>");
        builder.Append("<body>");

        // Custom HTML code could go here (I.e. a HTML table).
        builder.Append(TextBox1.Text); // Append the textbox value

        builder.Append("</body>");
        builder.Append("</html>");

        return builder.ToString();
    }

Then you could do:

message.Body = BuildMessageBody();

Remember to set the IsBodyHtml property to true for your MailMessage:

message.IsBodyHtml = true;

You could get slightly more complicated and iterate over your form for all textbox controls and build the string up this way, which would be a good solution IMO - however this should provide a starting point for what you need to achieve.

Darren
  • 66,506
  • 23
  • 132
  • 141
2

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);
sasjaq
  • 751
  • 11
  • 31
0

If you want to put an HTML file you can just set the MailMessage.BodyFormat property to MailFormat.Html, and then dump the contents of your html file to the MailMessage.Body property:

using (StreamReader reader = File.OpenText(htmlFilePath)) // Path to your 
{                                                         // HTML file
    MailMessage myMail = new MailMessage();
    myMail.From = "from@microsoft.com";
    myMail.To = "to@microsoft.com";
    myMail.Subject = "HTML Message";
    myMail.BodyFormat = MailFormat.Html;

    myMail.Body = reader.ReadToEnd();  // Load the content from your file...
    //...
}

Otherwise you can use StringBuilder to create a body itself. If you click you will find an example and usage of StringBuilder

Anıl Canlı
  • 412
  • 8
  • 20