Paul L
Paul L

Reputation: 2260

MailMessage sent string as body without newline in outlook

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?

Upvotes: 11

Views: 32453

Answers (10)

Ciro Corvino
Ciro Corvino

Reputation: 2128

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

Upvotes: 0

Baz Guvenkaya
Baz Guvenkaya

Reputation: 1562

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); 

Upvotes: 0

Carlos90210
Carlos90210

Reputation: 31

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

Upvotes: 3

eaglei22
eaglei22

Reputation: 2831

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

Upvotes: 0

Mathieu
Mathieu

Reputation: 1

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.

Upvotes: 0

user1740324
user1740324

Reputation: 51

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;

Upvotes: 3

Savi
Savi

Reputation: 31

Have your string like below

"Your string.\n"

And:

ms.IsBodyHtml = false;

But you may get it in your Junk folder

Upvotes: 3

XikiryoX
XikiryoX

Reputation: 1928

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.

Upvotes: 1

Aliostad
Aliostad

Reputation: 81660

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.

Upvotes: 0

Hans Olsson
Hans Olsson

Reputation: 55009

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

Upvotes: 14

Related Questions