Marty
Marty

Reputation: 1

.Net.Mail new line

I am using .Net.Mail to send text messages but I am having trouble inserting a new line. I have searched the internet, including StackOverflow, for a method to do this. I have found several but non of them work. I am using StringBuilder to build the body of the message but appending a new line or empty line does cause a new line to be inserted into the message. I have also tried "\n" and several other methods but nothing seems to work. I have added my code below. Does anybody know how I can do this. Thank you in advance for any help that may be offered.

string cellPhone = reader.GetString(reader.GetOrdinal("CellPhone"));
string suffix = reader.GetString(reader.GetOrdinal("Suffix"));
StringBuilder body = new StringBuilder();
cellPhone = cellPhone.Trim().Replace(" ", "").Replace("-", "").Replace("(", "").Replace(")", "");
if (!suffix.StartsWith("@"))
{
    suffix = "@" + suffix;
}
string address = cellPhone + suffix;
reader.Close();

EMail email = new EMail("mail.yyy.com");
email.IsHTML = true;
email.Subject = "Sales Lead from yyy.com";
email.Add_ToAddress(address, false);
body.AppendLine(" ");
body.AppendLine("Name: " + this.tbSalesLeadName2.Text + "EMail: mailto: " + this.tbSalesLeadEmail2.Text);

if (!this.chkSalesLeadEmail2.Checked)  //&& (!this.hidCellPhoneProvider.Value.Equals("0", StringComparison.InvariantCultureIgnoreCase)))
{
    body.AppendLine("Phone: " + this.tbSalesLeadPhone2.Text);
    body.AppendLine("Cell Phone: " + this.tbSalesLeadCellPhone.Text);
}

body.AppendLine(" ");
body.AppendLine(" ");
body.AppendLine("Comments: " + this.tbSalesLeadComments2.Text);
body.AppendLine(" ");
body.AppendLine(" ");
body.AppendLine("***To respond start a new text thread with the cell phone number listed above");
email.Body = body.ToString();
email.From = "[email protected]";
email.Send_Mail();

Upvotes: 0

Views: 1563

Answers (4)

Marty
Marty

Reputation: 1

Thank you for your advice. Please ignore my earlier comment. Your suggestion did work I just needed to add "\n" where I wanted a new line. I really appreciate your help.

Upvotes: 0

mrsrizan
mrsrizan

Reputation: 311

As you are using email.IsHTML = true;, You should be able to put
tag in your string builder object.. The code would look like,

body.AppendLine(" <br/>");
body.AppendLine(" <br/>");
body.AppendLine("Comments: " + this.tbSalesLeadComments2.Text + "<br/>");
body.AppendLine(" <br/>");
body.AppendLine(" <br/>");
body.AppendLine("***To respond start a new text thread with the cell phone number listed above<br/>");
email.Body = body.ToString();

Upvotes: 2

Servy
Servy

Reputation: 203821

You've specified that the email is HTML. New lines are ignored in HTML.

To actually render a line break you need to use <br /> or some other equivalent.

If you don't actually have any HTML to render, then simply specify that the email is not an HTML email and new lines will be rendered as new lines.

Upvotes: 1

Heinzi
Heinzi

Reputation: 172220

email.IsHTML = true;

You say that your body is in HTML format, but it isn't.

You have two options to fix that:

  1. Set IsHTML to false, and your line breaks should work.

  2. Format your body as real HTML, i.e., use HtmlEncode for your data and <br> for your line breaks.

Upvotes: 6

Related Questions