Question

HI, I am trying to send a simple notification using system.net.mail.mailmessage. I just pass the string as the message body. But problem is even though multi-line message been send have the correct "\r\n" format info in string. The mail opened in outlook will displayed as a long single line. But view source in outlook will display the message with correct new line format.

For example: original message would looks like:

line1
line 2
line 3

But it will displayed in Outlook like:

line1 line 2 line 3

View source from outlook will still display

line1
line 2
line 3

What should I do to make outlook display the message with correct newline information?

Was it helpful?

Solution

Outlook sometimes removes newlines (it usually pops up a comment that it has done it as well), not sure exactly about the rules for when it does this but I'm fairly sure if you add a . (full stop) at the end of each line it won't remove them.

Actually, looking at this article it seems like you can also solve it by sending the emails as HTML or RTF: Line breaks are removed in posts made in plain text format in Outlook

OTHER TIPS

This worked for me

mMailMessage.IsBodyHtml = true;
Literal1.Text = "Dear Admin, <br/>You have recieved an enquiry onyour Website. Following are the details of enquiry:<br/><br/>Name: " + TextBox2.Text + "<br/>Address: " + TextBox3.Text + ", " + TextBox4.Text + "<br/>Phone: " + TextBox5.Text + "<br/>Email: " + TextBox2.Text + "<br/>Query: " + TextBox6.Text+"<br/> Regards, <br/> Veritas Team";
mMailMessage.Body = Literal1.Text;

Have your string like below

"Your string.\n"

And:

ms.IsBodyHtml = false;

But you may get it in your Junk folder

//the email body should be HTML //replaces the new line characters \n\r with the break HTML

mailMsg.IsBodyHtml = true;
mailMsg.Body = this.Body;
mailMsg.BodyEncoding = System.Text.Encoding.ASCII;
mailMsg.Body = mailMsg.Body.Replace(Environment.NewLine, "<br/>"); 

Outlook will always remove line breaks, however, what can be used is the following:

In stead of using: Environment.Newline or \n\r

You should use string MyString += "this is my text" + "%0d%0A"

This %0d%0A will be recognised by outlook as the escape sequence for a new line.

Set the IsBodyHTML to false:

ms.IsBodyHtml = false;

By default it is false but it appears you have set it to true somewhere as outlook thinks it is HTML.

This worked for me:

    mail.Body = String.Format(@"Some text here:
A new line here  
And another line here");

Just be careful to not indent on the 2nd and 3rd line.

I was able to get it to work by adding a few string spaces at the end of line prior to the troubled line.

msg.Append("\n some Message" + "   ");
msg.Append("\n another message");

Necro ansering a question but could come in handy for some as well.

msg.IsBodyHtml = true;
AlternateView av = AlternateView.CreateAlternateViewFromString(msg.Body, new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Html));
av.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
msg.AlternateViews.Add(av);
AlternateView avPlain = AlternateView.CreateAlternateViewFromString(msg.Body, new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Text.Plain));
avPlain.TransferEncoding = System.Net.Mime.TransferEncoding.SevenBit;
msg.AlternateViews.Add(avPlain); 

Try using the verbatim operator "@" before your message:

message.Body = 
@"
line1
line 2
line 3
"

Consider that also the distance of the text from the left margin affects on the real sistance from the email body left margin..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top