I am using mailitem.HTMLBody to process the text of an email body.
The entire body of the email can be accessed through mailitem.HTMLBody.
How can I change the font of the entire email body in C#? e.g Set font size as 10.5 and font as Consolas
Outlook.MailItem mailItem = (Outlook.MailItem)selection[1];
mailItem.HTMLBody = "<font size= 10>";
This does not work.
No.
Don't use <font> tags. This tag has been obsolete for a very long time now.
Instead, use a CSS stylesheet. This would be a separate file which you'd include (it can also be embedded in your main page using the <style> tag in your header, but it's better practice to have it as a separate file)
To set the font globally io your page using CSS, you can do this:
body {
font-size: 10px;
}
Hope that helps.
Related
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..
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>"
I am dealing with a situation where I should change the font of a HTML body in C sharp using a stylesheet.
I have added a stylesheet to my project with name Stylesheet1.css which contains the code to change the font of a HTML body.
body {
font-size: 10px;
}
I need to reference this stylesheet in source code, where I am processing the HTML body.
I am processing the HTML body as follows.
if(some condition)
{
mail.HTMLBody= ? ? ? ? ;
}
I need to reference the stylesheet in this part. How can I do this?
I would not use external stylesheets for emails. As alot of email clients do not support it.
See http://groundwire.org/support/articles/css-and-email-newsletters
and
http://www.alistapart.com/articles/cssemail/
As some clients like hotmail remove the 'body' tag all togeather so your example in your question will not work. So you can instead wrap your email in a DIV and use inline styles so you get best support for all email clients.
A list of what is supported by which client is here http://css-discuss.incutio.com/wiki/Style_In_Email
Edit
You should be able to set the font-size like this
<div style="font-size:10px;">
your email content here
<p style="font-size:14px;">
some bigger text
</p>
</div>
I agree with Daveo's answer - you are best off embedding styles directly rather than linking out to an external CSS
There is a very, very extensive matrix of styles & features that are and aren't supported by the popular email apps (outlook/gmail/yahoo mail/etc) at http://www.campaignmonitor.com/css/
http://htmlemailboilerplate.com/ is a really good starting point for getting html and css right in emails.
I'm trying to create a nice looking email for when a user registers on my website however I am not able to display the text over an image the usual way. Instead, the texts are displayed below the image.
I know I can't use StyleSheets, and that I should stick to CSS 1.4..
I'm having a background image which i'm stretching (by setting its width and height) and I would like to put some text on top of this image. In normal html, I can use css methods such as float: left; position: absolute for the div to be floating over the image. But this doesn't work within the C# code because it gets truncated in most email clients.
The image will only display if it is written in the following way:
<div style='width: 580px; font-family: Calibri; font-size:13px;'>
<img style='height: 45px; width: inherit;' src='http://www.somedomain.com/mail/background.png' />
Test
Test2
Test3
</div>
I tried to embed the code above within a style and it simply doesn't work. For example:
<div style='background:url(images/background.png); position: absolute; top:45px; width:550px; padding: 15px;'> SOME TEXT HERE </div>
OR
<div style='background-image:url(images/background.png); position: absolute; top:45px; width:550px; padding: 15px;'> SOME TEXT HERE </div>
OR
<table>
<tr>
<td style="background-image: url(background.png);">Testing Page</td>
</tr>
</table>
If you change "background-image: url(background.png);" to "background:red;" it works and this is confusing!
How do I make this work properly in most email clients as well? (Gmail, Hotmail, Outlook, Thunderbird, Windows Mail, etc.)
Thanks for any help you can provide :)
You should probably check out all of your emails in Outlook 2007. Microsoft really broke the capabilities for HTML in Emails by using the rendering engine of MS Word. To that end background-image is not recognized by Outlook 2007. Here is the MSDN on supported HTML in Outlook. Use outlook as your base line as it's support is the most basic.
Iv had similar issues with images in html and images inside an email. I solved the Image issue by using an inline attachment. The trick is to use a content id to set the image.
Hopefully this code will help:
EDIT: Width and Height settings on the actual image don't seem to work though . . . :/
string contentID = "Image1.gif".Replace(".", "") + "#test";
// create the INLINE attachment
string attachmentPath = Server.MapPath("Images/Image1.gif");
inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = contentID;
inline.ContentType.MediaType = "image/gif";
inline.ContentType.Name = Path.GetFileName(attachmentPath);
//then add in the message body
//stringbuilder to construct the message
sb = new StringBuilder();
sb.Append("<div style=\"font-family:Arial\"> Hello World!<br /><br /><img src=\"##IMAGE##\" alt=\"\" Width=\"250px\" Height=\"250px\"><br /><br /></div>");
//creating the message with from and to and the smpt server connections
mail = new MailMessage("SendersEmail#Address.com", "RecieversEmail#Address.com");
SmtpServer = new SmtpClient("smtp.gmail.com"); //Add SMTP settings into the Web.Config --> ConfigurationManager.AppSettings["MyCustomId"]
mail.Subject = "Testing Emails";
mail.IsBodyHtml = true;
mail.Body = sb.ToString();
mail.Attachments.Add(inline);
// replace the tag with the correct content ID
mail.Body = mail.Body.Replace("##IMAGE##", "cid:" + contentID);
You can't use relative urls (for example "url(background.png)" or "url(images/background.png)" because it's relative to what? the email message? The email message has no folders, file system, etc. as does a web server.
You have two choices: use the complete url syntax as you did in the first example or create a MimeHtml message (MHTML). In MHTML, you can bundle an image in with your email and may be able to reference it in the way you're hoping.
I hate being the bearer of bad news, but you can't cross-platformly (is that a word?) have a background image and/or stacked elements in HTML emails.
You're trying to use CSS features that are not supported by all major email viewers. Try taking a look here to see what's supported and what's not:
http://www.campaignmonitor.com/css/
There's also a sitepoint book which you might be interested in:
http://www.sitepoint.com/books/htmlemail1/
Using MailMessage email = new MailMessage();email.IsBodyHtml = true;
Is it possible to set the font for the message?
Also, maybe it's a quirk but p and br tags come out the same for me? Is this normal?
You can use CSS:
<style>
p {color:#999; font-family:arial;} /*grey*/
</style>
You are limited to what fonts are install on the receivers machine.
I would suggest researching using a style sheet attribute to change the font size.
Not sure what you mean by the 'and tags come out the same for me' part... come out the same as what?
...charles beat me to it
When dealing with HTML email's you will find a multitude of frustrations.
Some issues I can remember
Some mail clients won't render CSS when it is placed outside the <body> element.
Some mail clients won't render CSS at all.
A great resource for HTML email is email-standards.org
You can include css in body of mail. Since mail body is in html format, all html features can be used here.
No, break and paragraph tags are not the same.
Break just starts on a new line, paragraph adds some space around it. They also cause floating objects to behave differently around them.
They can also be styled independently using css.