Line breaks ignored when sending mail as plain text
Asked Answered
O

8

13

I have a text like " Hi, \r\n this is test \r\n Thanks" I am sending the mail using MailMessage class. I have set the "IsBodyHtml" property to false. The issue is that I am receiving mails without line breaks. Can you let me know what I am missing?

Ops answered 13/7, 2011 at 14:45 Comment(1)
I think this question might be better answered at this link: #248046Standardbearer
T
18

Use Environment.NewLinemsdn instead of \r\n.

Tenderloin answered 13/7, 2011 at 14:47 Comment(0)
T
10

We had the same problem, but if you define your message all at once in a String, as opposed to a StringBuilder, you can define your message like this:

string message = string.Format(
@"First Line: {0}
Second Line: {1}
ThirdLine: {2}", firstValue, secondValue, thirdValue);

Defining the message body like this, and setting IsBodyHtml = false, will give you the new lines that you want.

Otherwise, use StringBuilder

var sb = new StringBuilder();
sb.AppendLine("FirstLine");
sb.AppendLine("SecondLine");
Tallith answered 13/7, 2011 at 14:53 Comment(1)
That tip about IsBodyHtml = false on the MailMessage is key! Any whitespace line breaks will get ignored with IsBodyHtml = true.Speculation
I
5

This is a feature in Outlook, you can turn it off in Outlook. Go to Options - Mail - and under "Message Format" you uncheck "Remove extra line breaks in plain text messages".

Another solution is to add three spaces at the end of each line, when you send the mail. This seems to get Outlook to accept that it is not an extra line break.

Illative answered 3/11, 2017 at 15:4 Comment(0)
D
2

If you are reading your mails from Outlook, it may be Outlook that is removing line breaks, thinking they are extra line breaks. Did you try reading your mails from another software - or maybe a webmail?

Derwin answered 13/7, 2011 at 14:56 Comment(0)
V
0

To be able to incorporate line breaks, rather than just plain test in your mail, you will need to have the body html set to true, I think.

Volvox answered 13/7, 2011 at 14:48 Comment(2)
You can have line breaks, you just can't have 2 line breaks and a line with a period and nothing else. That specific sequence denotes the end of the message and the SMTP server will go cross-eyed.Jospeh
I think it is easier to just send an html email. Someone (most likely from marketing/management) will ask for a pretty email when they see the plain text one and you'll only have to re-do it. However, I understand there are circumstances where only a plain text email can be sent.Funicular
A
0

A more tricky reason this may happen that I just had to deal with:

  • Your mail server manipulates outgoing messages and adds a html version to your otherwise text only message
Administrative answered 18/2, 2013 at 11:1 Comment(0)
S
0

I was having a similar problem where most of my line breaks were working but one was not. If I hit reply to the email that wasn't showing the line breaks, the original email text below the reply would show the line break (so this indicates it is an outlook issue). I tried the recommended answer of Environment.NewLine and that DID NOT change anything. In my case, adding a period at the end of the statement where I wanted the new line and then putting in a \r\n did the trick. Per a previous link I posted in this discussion, Outlook is using some rules to filter out line feeds and in the question that started this discussion I notice you do not have a period at the end of your sentence.

Standardbearer answered 23/1, 2015 at 21:3 Comment(0)
W
0

I was sending E-Mail notification via PowerShell script and also did not get any line breaks when viewing the mail in Outlook. For me the solution in the .ps1 file was this:

$emailMessage.Body = "First Line" + "`r`n" + "Second Line"
Woodall answered 1/6, 2017 at 7:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.