Archived Forum Post

Index of archived forum posts

Question:

Extract CSS Class within HTML?

Apr 04 '17 at 08:57

I have a question about CSS in HTML E-Mails. Is the a way to get the definition of a CSS Class within the HTML? E.g. if i download an email via IMAP, can I extract the CSS-Class defined within the Email over a Chilkat methode or property?


Answer

It's potentially possible in one of two ways, depending on where the CSS is located. There are two possibilities:

  1. The CSS may be located in a "related item" within the email. This is when the email is multipart/related, and images and potentially CSS files are contained in the related parts. The Email.NumRelatedItems property indicates how many related items exist, and there are various Email object methods for getting related items/data.
  2. The CSS may be embedded within the HTML. In that case, your program would need to get the HTML body, and then the job is to extract the CSS from the HTML. One way to do it is to first convert the HTML to XML. (The HTML may or may not already be valid XML. Converting ensures you have well-formed XML such that you can reliably work with it.) The next step would be to examine the XML and determine how to extract the CSS. The Chilkat.HtmlToXml class can be used to convert to XML.