Adding text to email.Body messes with the text formatting - c#

I'm trying to replace some text in the body of the email, but whenever I do it seems to screw up the formatting of the signature text. For example, if I try email.Body.Replace("Info", "Information") then this text:
First Last Title Info
Website: www.stackoverflow.com
turns into:
First Last Title Information Website: HYPERLINK
"http://www.stackoverflow.com/"www.stackoverflow.com
This happens even with just a simple addition to the text like email.Body += "text".
Along with this, it also switches the font of the body from Arial 10 to Calabri 12 (this probably has to do with Calabri 12 being the default email text when Arial 10 is the signiture font).
How would I save the format of the current email body so that when I add text to it, I could set it back or what would be the best way to add text to the body to keep the formatting?
Your help would be welcomed.

MailMessage.Body can be formatted in RTF, which I think it does by default, or in HTML. What the top message you posted actually looks like is something like this:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\colortbl ;\red0\green0\blue255;}
{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sl240\slmult1\lang9\f0\fs22 First Last \par
Title \par
\par
Info \par
Website: {\field{\*\fldinst{HYPERLINK "www.stackoverflow.com"}}{\fldrslt{\ul\cf1 www.stackoverflow.com}}}\f0\fs22\par
}
I'm assuming that appending "text" to the end of that is causing the RTF message to be parsed improperly, resulting in a loss of formatting.
http://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.body.aspx

So, what I ended up doing was using the email.HTMLBody. I created a paragraph block similar to what the HTML already had. This is the block: (my HTML isn't very good so... a lot of if is copy/paste with some editing)
<p class=MsoNormal>
<b>
<span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Replace Text
</span>
</b>
</p>
I set this to a string, say htmlPar, then do a replace:
string htmlReplace = htmlPar.Replace("Replace Text", myNewText");
This will create a paragraph block that I can add to the HTML with whatever my text is. To actually add it to the end of the HTMLBody I do a string.Replace like so:
string temp = email.HTMLBody.Replace("</body></html>", htmlReplace + "</body></html>");
email.HTMLBody = temp;
With this I was able to keep the formatting in the rest of the email. Like I said, my HTML isn't great so I'm not sure if this could work to add text in general or how good of a method doing it this way actually is.
Either way, I hope this can help someone else down the road.

Related

How do you include a linebreak taken from a textarea in a paragraph?

I am using a textarea in my view page to store information in a database, then retrieve it to put in a paragraph. Sometimes I have to include a new line in the textarea, but it is always stored in the database as "a b c", when i retrieve the data to put into a paragraph the new line does not appear. It always shows in a horizontal format like "a b c". How can i show the same style in paragraph format?
After you read the text from the database and before you show it in the paragraph, you will need to replace the newlines with HTML line breaks.
string text = textFromDatabase.Replace(Environment.NewLine, "<br />");
Make the textarea wrapped in pre tag
<pre><textarea>CONTENT
CONTENT
</textarea></pre>
Use Pre tag to display the data this will consider the line breaks as entered in the textarea you dont need to put textarea inside pre tag as mentioned in the answer of #user3820407
<pre>
Your Text
</pre>
see http://www.w3schools.com/tags/tag_pre.asp
<p id="paragraph">
</p>
<script>
var ContentFromTextArea = "Your text area content goes here.";
ContentFromTextArea.replace("\r", "<br />");
ContentFromTextArea.replace("\n", "<br />");
$("#paragraph").html(ContentFromTextArea);
</script>

c# - \r\n string not working

I have this string below:
<List>\r\n <First>\r\n <Second>BlaBla..</Second>\r\n...
But in View (MVC Asp.Net) presenting in one line only.
What can I do to respect the \r\n to broke in a new line?
Thanks
\r\n will format your string in source, it will be visible when you view HTML source of your page. You need to use HTML <br> tag instead of \r\n, so your browser will format your output accordingly.
Apart from the other answers that suggest using <br/>, you can also use the CSS white-space property which can actually make line breaks when it sees \r\n, or the <pre> tag which has this set up by default. See: http://www.w3schools.com/cssref/pr_text_white-space.asp
Assuming that your view is an HTML page rendered in the browser, understand that HTML does not render line break characters. The source code will, but not the HTML. If you need line breaks, you should use a tag structure that renders them, such as <p> or <br>.

Retrieve database content without linebreak

(asp.net C# project)
I am trying to show contact info in my footer, I receive the info from the database, but in my administrator panel I have CKEditor which adds linebreaks. So, what my problem is that I'd like my text in my footer to have no linebreak, is there any way of doing this?
How it looks in my contact page:
Adresse
Zip, City
Phone
Fax
How I would like it to be in my footer:
Adresse, Zip city, phone, fax
I receive the texts using:
<p><asp:Literal ID="LitContent" runat="server" /></p>
LitContent.Text = textService.GetText("ContactInfo");
LitContent.Text = textService.GetText("ContactInfo").Replace("<br />", " ");
but you should check how the BR is formatted from the editor. It may be capitalized or without /
It is also possible that the HTML generated by the editor uses paragraphs instead of br. In this case it becomes more complex to remove them. Basically to answer your question correctly we need more information. What is the actual string that GetText returns.
This looks like a formatting Problem.
There's a string and you can replace the Linebreaks out of a string. The code shown below do this by replacing the Linebreak with a blank. Should be the easiest way of doing this
infoText = textServices.GetText("ContactInfo");
infoText = infoText.Replace(Environment.NewLine, " ");
LitContent.Text = infoText;

How to make string bold in code behind

Is there a way to make a string bolder in code behind using C# .NET?
I tried:
string TypeofDay = "<span style='font-weight: bold'>Type Of Day</span> ";
txtbox.Text = TypeofDay + ": " + "Delivery Day"
I am concatenating TypeofDay(bold) and "Delivery Day" to display in a textbox.
You can't make some bits bold and some bits not bold, in an <asp:TextBox>.
You can't make text bolder by enclosing text value in tags. You must change attribute of a control that displays that text for example by setting its CSS class or changing code-behind property:
txbSendMessageBody.Font.Bold = true;
PS. I am concatenating few bold strings and some other strings to display in a textbox.
A HTML text box does not support this. ASP.NET (usually) generates HTML; if HTML does not support this, you cannot solve it from the server side.
A bit hacky alternative can be to use Unicode bold characters
http://qaz.wtf/u/convert.cgi?text=Type+Of+Day
txtbox.Text = "𝗧𝘆𝗽𝗲 𝗢𝗳 𝗗𝗮𝘆: 𝖣𝖾𝗅𝗂𝗏𝖾𝗋𝗒 𝖣𝖺𝗒"
You could layer a div on top of the textbox. Since the text formatting would change anyway once the user started typing, you can just hide the div once focus is given to the div, thus showing the text box. If nothing is entered and focus is lost, show the div again.
TextBox, no, however you could use a label.
myLabel.text = "<b>bold text</b> normal text <u>underlined text</u> <span style='font-size:Large; color:Red'>Big red text</span>";

show line breaks in output

when i enter text into a text area with linebreaks this is not being output later when i display the text. how do i let it show the line spaces?, instead all the text is cramped together at the moment.
How do you display it? HTML ignores whitespace, including line breaks. You can replace the linebreaks with the <br> element to show the enters in your browser too.
Mind that any subsequent spaces are also ignored. You can use the <pre> element too, to display the exact text, but I think that long lines won't be wrapped in that case.
If you are putting your text into HTML try this just before writing it out to the page:
string myText = "" // your text
myText = myText.Replace(Environment.NewLine, "<br/>");
this worked:
MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))

Categories

Resources