求推荐可保留样式的Java开源库实现Outlook邮件转HTML/图片
Hey Sam, totally doable with open-source Java tools—here are the best options to preserve Outlook's original styling while ditching the paid Aspose library:
1. Apache POI-HSMF (处理MSG格式邮件)
This is the go-to open-source library for reading Outlook .msg files directly. It can extract both HTML and RTF body content, which is key for preserving styling.
Key Features:
- Pulls the original HTML body if the email was sent in HTML format (most modern Outlook emails are)
- Falls back to RTF body if HTML isn't available (you'll need an extra step to convert RTF to HTML)
- Preserves inline styles, images, and basic formatting that Outlook uses
Quick Code Snippet (读取MSG的HTML内容):
import org.apache.poi.hsmf.MAPIMessage; import org.apache.poi.hsmf.datatypes.StringChunk; import java.io.File; public class MsgToHtml { public static void main(String[] args) throws Exception { MAPIMessage msgFile = new MAPIMessage(new File("path/to/your/email.msg")); // Extract HTML body first (preferred for styling) StringChunk htmlBody = msgFile.getMainChunks().getHtmlBody(); if (htmlBody != null) { String htmlContent = htmlBody.getValue(); // Save to file or pass to image renderer // Example: Files.write(Paths.get("output.html"), htmlContent.getBytes()); } // If only RTF is available, use RTFParserKit to convert to HTML StringChunk rtfBody = msgFile.getMainChunks().getRtfBody(); if (rtfBody != null) { String rtfContent = rtfBody.getValue(); // Use RTFParserKit here to convert RTF to HTML } } }
2. JavaMail API (处理EML/MIME格式邮件)
Outlook can export emails as .eml files (MIME format), and JavaMail is perfect for parsing these. It natively handles HTML content and preserves styling.
Key Features:
- Works with standard MIME emails (EML files)
- Extracts HTML body directly, including inline styles and embedded images
- Integrates easily with other libraries for image conversion
Quick Code Snippet (读取EML的HTML内容):
import javax.mail.Message; import javax.mail.Session; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import java.io.FileInputStream; import java.util.Properties; public class EmlToHtml { public static void main(String[] args) throws Exception { Session session = Session.getDefaultInstance(new Properties(), null); Message email = new MimeMessage(session, new FileInputStream("path/to/your/email.eml")); Object content = email.getContent(); if (content instanceof MimeMultipart) { MimeMultipart multipart = (MimeMultipart) content; for (int i = 0; i < multipart.getCount(); i++) { if (multipart.getBodyPart(i).isMimeType("text/html")) { String htmlContent = (String) multipart.getBodyPart(i).getContent(); // Process the HTML content here } } } else if (email.isMimeType("text/html")) { String htmlContent = (String) content; // Process the HTML content here } } }
3. 转图片:Flying Saucer (XHTMLRenderer)
Once you have the HTML content, use Flying Saucer to render it as an image. It supports most CSS styles (including Outlook's inline styles) to keep the original look.
Quick Code Snippet (HTML转图片):
import org.xhtmlrenderer.simple.Graphics2DRenderer; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class HtmlToImage { public static void main(String[] args) throws Exception { File htmlFile = new File("path/to/your/email.html"); // Render HTML to image (adjust width/height as needed) BufferedImage emailImage = Graphics2DRenderer.renderToImage(htmlFile, 1200, 1800); // Save as PNG ImageIO.write(emailImage, "PNG", new File("email_output.png")); } }
关键注意事项
- Outlook特殊样式: Some Outlook-specific CSS (like conditional comments or proprietary tags) might need cleanup. Use Jsoup to parse and adjust the HTML if rendering looks off.
- RTF转HTML: If you only have RTF content, use
RTFParserKit(another open-source library) to convert it to HTML before rendering. - Embedded Images: Both POI and JavaMail can extract embedded images—make sure to reference them correctly in the HTML before rendering to images.
All these libraries are 100% open-source and free to use, so they fit perfectly within your budget constraints.
内容的提问来源于stack exchange,提问作者user1293071

