11

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?

Paul L
  • 2,240
  • 5
  • 36
  • 55

10 Answers10

14

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

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • 1
    But Why Why Why, outlook try to remove line break in plain text by default is beyond me! – Paul L Oct 14 '10 at 20:08
  • @pstar: I think it's to counter the problems with linewrapping. As in if you send an email to me linewrapped at 80 characters and I have the same setting + that it should prefix the response with `> ` my response would wrap your lines again so your original text would end up looking quite ugly. – Hans Olsson Oct 15 '10 at 05:23
  • I have no experience in terms of functionality of text editing application. But seems like to me it trying to fix the problem of handle linewrap and quote the original message by trying to remove extra newline. I think I would rather the email application try to keep my email format information as it is. Hope that they change that behaviour in Outlook 2010. – Paul L Oct 16 '10 at 07:16
3

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;
  • Adding the "
    " worked for me. For background, I was taking a cell value which contains Email body text and replacing Chr(10) with "
    " for the outlook draft email body to come out properly.
    – Vamsi Jan 30 '17 at 07:14
3

//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/>"); 
3

Have your string like below

"Your string.\n"

And:

ms.IsBodyHtml = false;

But you may get it in your Junk folder

Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
Savi
  • 31
  • 1
1

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.

XikiryoX
  • 1,898
  • 1
  • 12
  • 33
  • True - this used to work but now this sends a message with the line breaks missing. In outlook you need to click on the top banner in the message and select restore line breaks. Don't have any solution at the moment – XikiryoX Jul 21 '15 at 04:09
  • Ah okay. Thanks for the feedback. See my answer below for what I ended up doing to get it to work with Outlook 2013. – eaglei22 Jul 21 '15 at 22:13
0

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.

0

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");
josliber
  • 43,891
  • 12
  • 98
  • 133
eaglei22
  • 2,589
  • 1
  • 38
  • 53
0

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); 
Baz Guvenkaya
  • 1,482
  • 3
  • 17
  • 26
0

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.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • I tried not setting or explicitly set IsBodyHtml to false, doesn't seems working either way. – Paul L Oct 14 '10 at 20:06
0

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..

Ciro Corvino
  • 2,038
  • 5
  • 20
  • 33