Adding HTML property in C# when dealing with email - c#

I am facing a situation where I need to process an incoming email which is in plain text format and then display the email as virtual plain text(Font Size =10.5 and Font type=Consolas) to the user.
My code till now
string Text = "<html><body><basefont size=3 font face= consolas>" + mailItem.HTMLBody + "</font></body></html>";
mailItem.HTMLBody = Text;
//Process the email and display
This works good, but during display this shows virtual plain text of font size 10.0 instead of needed 10.5. I tried changing the basefont size to 4,5 etc. this does not change the display in any way.
other options tried:
1.Adding a CSS stylesheet. This will not work good as most email clients don't support it.
2.Adding a div tag to the code like this
string Text = "<html><div style=font-size:10.5px; font-face:consolas;>" + mailItem.HTMLBody + "</font></body></div></html>"
this also does not work.
My main objective is to display a virtual plain text email with Font Size 10.5 and font type Consolas to the user after the processing has been done.

Your second approach seems to be allright - you just have a </font> and a </body> too much + you should change "font-face" to "font-family".
Also throw in double quotes - that will output one quote:
string Text = "<html><div style=""font-size:10.5px; font-family:Consolas;"">" + mailItem.HTMLBody + "</div></html>"

Related

Plain Text not showing when sending an email with an attached file in C#

When I send an html email with an attached file, Plain Text on Thunderbird doesn't show anything, especially when an attached file is docx, pdf, or etc. txt file was okay when I tested though.
It works well if I don't attach any files, Thunderbird correctly shows Html Text and Plain Text if I switch its view setting.
using(var mail = new MailMessage()){
// ...omit
var alternateView = AlternateView.CreateAlternateViewFromString("<h1>TEST</h1>", Encoding, MediaTypeNames.Text.Html);
alternateView.TransferEncoding = TransferEncoding.SevenBit;
mail.AlternateViews.Add(alternateView);
mail.Attachments.Add(new Attachment("path to a file"));
// ...omit
}
I tried https://stackoverflow.com/a/2828438/366049 but no luck.
--EDITED--
As Caius Jard suggested, I tried the following and the results are below.
setting it as the main body + the HTML as an alternateview
It works, but this means I have to prepare plain text and html text. If I don't attach any files, Thunderbird shows plain text which is from html text removing tags. I want this behavior all the time since input text is made by our users. Otherwise I need to retrieve plain text from html.
setting both the text and the HTML as alternate views, no main body
Both Plain Text and Html Text settings in Thunderbird show the text alternate view.
setting the text as both the main body and a text alternateview as well as having an HTML alternateview
Thunderbird shows the text alternate view even if I switch its view setting.
According to these experiments, a plain alternate view is superior than an html alternate view and a main body.
Though your question doesn't seem to contain example code that demonstrates how you set the plaintext, try
setting it as the main body + the HTML as an alternateview
setting both the text and the HTML as alternate views, no main body
setting the text as both the main body and a text alternateview as well as having an HTML alternateview
I'd be interested to know which of these works out for thunderbird..

How can I change the font style of a string?

I have a string that I am sending to an e-mail Server to be the body of an e-mail. I'm trying to control what font it will end up in the e-mail. Here is the string:
#"Dear " + txtName.Text + ",<br/><br/>Thank you for submitting. <br/><br/>In the event of any changes, please submit again here" + link + ". <br><br>The following is a summary for your records.<br/><br/>"
How would I make the font, say, Times New Roman?
It seems that all the information out there about font changing always involves using an ASP control but that's out of the picture in this case.
To change the font-family, you must use CSS.
string mail = #"Dear " + txtName.Text + ",<br/><br/>Thank you for submitting. <br/><br/>In the event of any changes, please submit again here" + link + ". <br><br>The following is a summary for your records.<br/><br/>"
for example with Time New Roman:
mail = "<p><span style=\"font-family: Time New Roman;\">" + mail + </span></p>"
You should know that you can use different font, if the client did not have the first one :
font-family: Times, "Times New Roman", Georgia, serif;
If you want more tips about mail formatting :
Using CSS in HTML Emails: The Real Story by Chris Coyier
How to Create Great HTML Emails with CSS by Christian MacAuley

Adding text to email.Body messes with the text formatting

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.

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

Categories

Resources