Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a multi line body in C# System.Net.Mail.MailMessage

Tags:

c#

email

If create the body property as

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();  message.Body ="First Line \n second line"; 

I also tried

message.Body ="First Line" + system.environment + "second line"; 

Both of these were ignored when I received the message (using outlook).

Any ideas on how to get mutliple lines? I am trying to avoid html encoding so that the email will play nicer with spam filters.

thanks

like image 752
Maestro1024 Avatar asked Feb 16 '11 18:02

Maestro1024


2 Answers

As per the comment by drris, if IsBodyHtml is set to true then a standard newline could potentially be ignored by design, I know you mention avoiding HTML but try using <br /> instead, even if just to see if this 'solves' the problem - then you can rule out by what you know:

var message = new System.Net.Mail.MailMessage(); message.Body = "First Line <br /> second line"; 

You may also just try setting IsBodyHtml to false and determining if newlines work in that instance, although, unless you set it to true explicitly I'm pretty sure it defaults to false anyway.

Also as a side note, avoiding HTML in emails is not necessarily any aid in getting the message through spam filters, AFAIK - if anything, the most you do by this is ensure cross-mail-client compatibility in terms of layout. To 'play nice' with spam filters, a number of other things ought to be taken into account; even so much as the subject and content of the mail, who the mail is sent from and where and do they match et cetera. An email simply won't be discriminated against because it is marked up with HTML.

like image 78
Grant Thomas Avatar answered Oct 31 '22 20:10

Grant Thomas


In case you dont need the message body in html, turn it off:

message.IsBodyHtml = false; 

then use e.g:

message.Body = "First line" + Environment.NewLine +                 "Second line"; 

but if you need to have it in html for some reason, use the html-tag:

message.Body = "First line <br /> Second line"; 
like image 20
Mikael Puusaari Avatar answered Oct 31 '22 21:10

Mikael Puusaari