MailMessage sent string as body without newline in outlook
Asked Answered
E

10

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?

Erratum answered 14/10, 2010 at 8:32 Comment(0)
C
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

Coquetry answered 14/10, 2010 at 8:34 Comment(3)
But Why Why Why, outlook try to remove line break in plain text by default is beyond me!Erratum
@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.Coquetry
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.Erratum
J
3

Have your string like below

"Your string.\n"

And:

ms.IsBodyHtml = false;

But you may get it in your Junk folder

Justiciar answered 19/7, 2011 at 14:21 Comment(0)
C
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;
Cantatrice answered 28/10, 2012 at 19:53 Comment(1)
Adding the "<br>" worked for me. For background, I was taking a cell value which contains Email body text and replacing Chr(10) with "<br>" for the outlook draft email body to come out properly.Basidium
K
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/>"); 
Koski answered 13/10, 2015 at 19:13 Comment(0)
K
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.

Kagera answered 4/11, 2011 at 18:6 Comment(2)
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 momentKagera
Ah okay. Thanks for the feedback. See my answer below for what I ended up doing to get it to work with Outlook 2013.Biolysis
B
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.

Barbirolli answered 14/10, 2010 at 8:36 Comment(1)
I tried not setting or explicitly set IsBodyHtml to false, doesn't seems working either way.Erratum
U
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.

Undressed answered 4/6, 2015 at 15:9 Comment(0)
B
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");
Biolysis answered 20/7, 2015 at 18:49 Comment(0)
M
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); 
Motch answered 21/7, 2016 at 7:50 Comment(0)
L
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..

Lexicography answered 9/5, 2017 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.