DarkW1nter
DarkW1nter

Reputation: 2861

format email body - .net 4.0

I'm using the following to send email from a site done in .net 4, c#.

MailMessage nMail = new MailMessage();
nMail.To.Add("[email protected]");
nMail.From = new MailAddress("[email protected]");
Mail.Subject = (" ");
nMail.Body = (" ");                                                   
SmtpClient sc = new SmtpClient("our server");
sc.Credentials = new System.Net.NetworkCredential("login", "pwd");
sc.Send(nMail);

Works fine, only thing I don't know how to do is format the body of the message to have multiple lines and include fields from the page itself. Any pointers?

Upvotes: 0

Views: 9054

Answers (2)

vvk
vvk

Reputation: 823

You can send message as HTML. Use IsBodyHtml property

MailMessage nMail = new MailMessage();
nMail.To.Add("[email protected]");
nMail.From = new MailAddress("[email protected]");
Mail.Subject = (" ");
nMail.Body = ("Line<br/>New line"); 
nMail.IsBodyHtml = true;                                                  
SmtpClient sc = new SmtpClient("our server");
sc.Credentials = new System.Net.NetworkCredential("login", "pwd");
sc.Send(nMail);

Upvotes: 1

jason
jason

Reputation: 241769

This is one way to make a message body with multiple lines.

var bodyBuilder = new StringBuilder();
bodyBuilder.AppendLine("First line.");
bodyBuilder.AppendLine("Second line.");
nMail.Body = bodyBuilder.ToString();

It should be obvious how to pull in values from your form now, too (i.e., the full power of string formatting is at your disposal now).

Upvotes: 3

Related Questions