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.
Related
I need to convert a well-formatted HTML string to a PDF document.
I found this DLL that should do what I need, but it isn't working fine on formatting.
That's the HTML code I'm trying to convert, and viewing it on browser works fine (I've used Bootstrap CSS, that's been correctly referenced as cdn).
But once converted to PDF this is the result:
And that's the code I'm using to convert it:
string html = "";
if (File.Exists(pathIN))
{
html = File.ReadAllText(pathIN);
}
PdfDocument pdfDocument = new PdfDocument();
PdfDocument pdf = PdfGenerator.GeneratePdf(html, PageSize.A4, 60);
pdf.Save(pathOUT);
Does anyone have any suggestion?
I also had issues with this when using HtmlRenderer/PdfSharp with Bootstrap controlling the layout.
Although it goes against the grain, I resorted to using tables for the layout. Given that the destination (pdf) was a obviously a fixed width, being responsive was not a requirement.
Try using https://wkhtmltopdf.org, works well for bootstrap pages.
I know its a little late but this can help someone
The problem with bootstrap is that to align the columns use float: left and pdfsharp cannot read this property instead use display: inline-block and define the width in pixels.
To avoid useless effort to other people, it doesn't work for Bootstrap like the Table even using display: inline-block to define the width. Right side of table is always trimmed, the size unfitting the letter size in my case.
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 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.
I'm making a list of recent news. So, it will show something like this:
- Take a look at the new Volks...
- John Doe is looking for a jo...
- Microsoft is launching the n...
So, the list above only shows me the title of the news and the length of each news is limited in 25 characters. But, this is not working well... for example, if you type 25 M's, it will explode my div.
I've been told that there is a way to calculate the length of the string and make it fit in a div automatically.
Does anyone know how to do it?
thanks!!
"text-overflow: ellipsis" is what you want but not everybody supports it. More info here...
I think you talking about is using the System.Drawing.Gaphics class's MeasureString() method.
However, this requires making a Graphics object which matches the font characteristics of your web page. But, your server process shouldn't know anything about the style elements of the web page, which should be handled by the CSS sheet.
I think you want to use css for this.
word-wrap:break-word;
should do it
One very simple way to prevent "exploding the div" is to use a css style to set the overflow of the div to scroll or hide the extra text instead of stretching to accomodate it.
I don't think there is an easy way to do this that works with all browsers and fonts.
The best way is just making sure your layout don't break if someone enters 25*m.
An useful thing to do is to split words that are more than X letter.
I the word-wrap css don't work that well on all browers.
This is not really a server-side problem, as the server shouldn't know what fonts people are using. You can do it using Ajax - post the font to the server, calculate the width (as James Curran mentioned), and return the right strings. However, the server may ont have the same fonts installed, and you have to calculate padding and margins on the server side.
I can think of several options on the client side:
Wrap every line with a span. A span would expand automatically to the width of the line. Using jQuery or your favorite javascript you can remove characters until the width is ok. (you can do a sort of binary search, where at every stage you add the ellipsis and checks the width)
Easy - Wrap every line with a fixed-width div and set it overflow:hidden, and add the ellipsis after the div. This will cut through letters though, and when you get a short text it'll still show the ellipsis.
Too easy - Use a fixed width font (they're mostly ugly).
As others have mentioned you can measure strings in thick client applications using System.Drawing.Graphics.MeasureString, but since you mention you want to fit it in an HTML div tag it would be perferable to let the browser handle the user interface using CSS.
<html>
<head>
<title>C# - How can I cut a string at its end to fit in a div? </title>
<style type="text/css">
.ellipsis li
{
display: block;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 166px;
}
</style>
</head>
<body>
<ul class="ellipsis">
<li>Take a look at the new Volksxxxxx</li>
<li>John Doe is looking for a joxxxxx</li>
<li>Microsoft is launching the nxxxxx</li>
</ul>
</body>
</html>
I used the unordered list tag (UL) instead of div since your sample list begins with a bullet character. Similar CSS would apply to DIV tags. And although all browser can be made to clip the content, not all browsers support the non-standard text-overflow: ellipsis style.
We have used the redcloth and bluecloth wiki renderer's with Ruby, basically you can do something like this...
html = RedCloth.to_html(wiki_content)
and poof, you get back HTML.
Is there something out there for C#/.NET ?
try http://wikiplex.codeplex.com/
There are some wiki rendering engines but the names escape me right now. Perhaps check out some of these open-source options? I've previously reviewed MindTouch from that list for an application and it was quite rich, but it did much more than I needed to do.
If you just need something to turn text into HTMLcontent, I use Halide which lets people type in a textarea then it'll HTML-ify links, remove dangerous content, add <p></p> and <br />, etc. Very simple but no built-in formatting options.
SO uses a custom version of Markdown for their text editor and HTML content rendering. Search google for Markdown.NET for a number of ports.