how to give new line in mailmessage.body section - c#

I have sent mail using SMTP (ASP.NET).
I have written text in one line only, but I want to be in next line.
I used \n, but it's not working.

System.Environment.NewLine
If you format your email as HTML you can add a <br /> to it.
To format as HTML use the IsBodyHtml property of the MailMessage class.
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.isbodyhtml.aspx

I solved the problem this way :
Replace \n by \t\n
The tab will not be shown, but the newline will work.

if IsBodyHtml == false, then https://social.msdn.microsoft.com/Forums/vstudio/en-US/82da38ac-ac20-4c9a-a2bd-8512d88fdd41/c-newline-in-email?forum=csharpgeneral suggests:
Use %0D%0A to separate lines.
To simplify, use ā€œ\r\nā€, then call Uri.EscapeDataString for body and
subject before concatenation.
if IsBodyHtml == true, as suggested above, use <br />
You can use
someText.Replace("\n", "<br />") to replace newlines (any remaining \r from \r\n line endings will be ignored). Or use someText.Replace("\r\n", "<br />").Replace("\n", "<br />") to fix both line endings without any \r remainders, if line-endings are intermixed in text.

Related

MailMessage class

I want to add an image into my mail, I have everything working for the most part but my only problem is that in order to have an image I need to set the body to HTML format... which then stops me from having break lines. So I think this is a 2 part question.
Is there a way to have both normal String for the first part of a message body and then the HTML for just the picture? or if not the How can I find and change the break line of a normal String to < br>?
I believe I need to change \n to < br/> in a normal String
body.Replace("\n", "< br/>);
doesn't seem to work...
Try this:
body = body.Replace(Environment.NewLine, "<br />");
the mail format actually is written in the header of the message so its either TEXTformat or HTML format, you cant mix them both in the same message
I am assuming you are using the Mail Libray in .net ? you will need to change your format type from text to HTML with the IsBodyHtml on the mail message
You can just create a html image and use AlternateView to then get the plain text
Sending a mail as both HTML and Plain Text in .net

Line break doesn't work in MailMessage's body

I'm sending an email from ASP.NET using MailMessage and SmtpClient. I want to introduce some line breaks but none of the following worked:
Adding tags sbBody.Append("<table width='100%'><tr><td></br></br>");
Adding \r\n sbBody.Append("<table width='100%'><tr><td>\r\n\r\n");
Appending a new line to the StringBuilder.
sbBody is a StringBuilder wich I use at the end to set MailMessage's body: mailMessage.Body = sbBody.ToString()
What Am I missing here? I'm viewing the emails in Outlook and off course I'm setting IsBodyHtml to true.
Thanks for your time.
EDIT: Solved, it was a syntax error, </br> instead of <br/>
Your br tags are wrong. They should be written as:
<br />

.Replace(Environment.NewLine, "<br />") works on localhost but not when I upload my website to host

I have no idea why is that. Here is my code and it works perfectly when I try it on localhost but when I upload my website my text has no <br />'s. Why this could happen? And how can I fix this issue with new lines? (white-space: pre-line; is not a solution for me, it is not working on IE6 and it is messing with my styles)
#Html.Raw(Html.Encode(Model.Body)
.Replace(Environment.NewLine, "<br />"))<br />
I believe this answer is best:
https://stackoverflow.com/a/8196219/550975
string result = Regex.Replace(input, #"\r\n?|\n", "<br />");
As BuildStarted mentioned in the comments, the browsers might either send \r\n or \n, which will break, if you use Environment.NewLine - and I don't think asp.net will fix that up before running your code.
I'd suggest you use a regular expression to replace linebreaks instead: "\\r?\\n" This should match both cases (I don't expect any browser to actually use '\r' only).
A lot has happened since IE6 (thank god!) so I would just like mention the CSS solution to the problem.
In your C#:
ViewBag.StringText = "some text" + Environment.NewLine + "more text in a new line";
In your CSS:
.newlines {
white-space:pre-line;
}
In your Razor:
<div class="newlines">#ViewBag.StringText</div>
Instead of using Environment.NewLine, try this:
someString.Replace(#"\r\n", "<br/>");

Line breaks ignored when sending mail as plain text

I have a text like " Hi, \r\n this is test \r\n Thanks" I am sending the mail using MailMessage class. I have set the "IsBodyHtml" property to false. The issue is that I am receiving mails without line breaks. Can you let me know what I am missing?
Use Environment.NewLinemsdn instead of \r\n.
We had the same problem, but if you define your message all at once in a String, as opposed to a StringBuilder, you can define your message like this:
string message = string.Format(
#"First Line: {0}
Second Line: {1}
ThirdLine: {2}", firstValue, secondValue, thirdValue);
Defining the message body like this, and setting IsBodyHtml = false, will give you the new lines that you want.
Otherwise, use StringBuilder
var sb = new StringBuilder();
sb.AppendLine("FirstLine");
sb.AppendLine("SecondLine");
This is a feature in Outlook, you can turn it off in Outlook. Go to Options - Mail - and under "Message Format" you uncheck "Remove extra line breaks in plain text messages".
Another solution is to add three spaces at the end of each line, when you send the mail. This seems to get Outlook to accept that it is not an extra line break.
If you are reading your mails from Outlook, it may be Outlook that is removing line breaks, thinking they are extra line breaks. Did you try reading your mails from another software - or maybe a webmail?
To be able to incorporate line breaks, rather than just plain test in your mail, you will need to have the body html set to true, I think.
A more tricky reason this may happen that I just had to deal with:
Your mail server manipulates outgoing messages and adds a html version to your otherwise text only message
I was having a similar problem where most of my line breaks were working but one was not. If I hit reply to the email that wasn't showing the line breaks, the original email text below the reply would show the line break (so this indicates it is an outlook issue). I tried the recommended answer of Environment.NewLine and that DID NOT change anything. In my case, adding a period at the end of the statement where I wanted the new line and then putting in a \r\n did the trick. Per a previous link I posted in this discussion, Outlook is using some rules to filter out line feeds and in the question that started this discussion I notice you do not have a period at the end of your sentence.
I was sending E-Mail notification via PowerShell script and also did not get any line breaks when viewing the mail in Outlook.
For me the solution in the .ps1 file was this:
$emailMessage.Body = "First Line" + "`r`n" + "Second Line"

Problem regarding mail content

I am sending mail to the customers. And the format is like this:
Name: abc. Company:ccc. Address: sde.
So the mail is coming out in continuous format which I don't want.
I want it in this format:
Name: abc.
Company:ccc.
Address: sde.
I'm currently using string.format to populate each of the values and later on replacing '.' with '\n'.
However, it looks like that is not working.
Any suggestions on how to make it look like this?
Use BodyFormat property to format your mails as HTML format, and use <br /> for the line breaks.
MailMessage myMail = new MailMessage();
myMail.BodyFormat = MailFormat.Html;
myMail.Body = "Name: abc.<br />";
myMail.Body += "Company:ccc.<br />";
myMail.Body += "Address: sde.<br />";
Try Environment.NewLine
Have you tried "\n\r"?
Is the body set to be plain text or html?
If you are building a long string, I recommend you use StringBuilder.
StringBuilder sb = new StringBuilder();
sb.AppendLine(string1);
sb.AppendLine(string2):
sb.AppendLine(""); //empty line
mailMessage.Body = sb.ToString();
Edit: I use this to send our text only emails and it works on non-HTML emails.
#Canavar's answer is good, but not all mail clients support html and even those that do may have it turned off for security purposes.
As an alternative, I would say use MailFormat.Text and use the System.Environment.NewLine
If HTML format is ok, you could just insert <br>
If not, and you are using outlook to view the message, try one of these two:
Use \n\n
Keep your period so that the line ends with .\n
I know this seems odd, but it may make outlook happy.

Categories

Resources