Render email messages as HTML, PDF, PNG, and JPEG files
Leave feedback
On this page
GroupDocs.Viewer for Java allows you to render your email messages in HTML, PDF, PNG, and JPEG formats. You do not need to use third-party email clients to view the contents of email files within your Java application (web or desktop).
To start with the GroupDocs.Viewer API, create a Viewer class instance. Pass an email message you want to view to the class constructor. You can load the message from a file or stream. Call one of the Viewer.view method overloads to convert the loaded message to HTML, PDF, or image format. These methods allow you to render the entire message or specific pages.
Create an HtmlViewOptions class instance and pass it to the Viewer.view method to convert an email message to HTML. The HtmlViewOptions class properties allow you to control the conversion process. For instance, you can embed all external resources in the generated HTML file, minify the output file, and exclude specific fonts. Refer to the following documentation section for details: Rendering to HTML.
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.HtmlViewOptions;// ...
try(Viewerviewer=newViewer("sample.eml")){// Create an HTML file for the letter.
HtmlViewOptionsviewOptions=HtmlViewOptions.forEmbeddedResources("output.html");viewer.view(viewOptions);}
The path format for the folder with external resources
The resource URL format
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.HtmlViewOptions;// ...
try(Viewerviewer=newViewer("sample.eml")){// Specify the HTML file name and location of external resources.
// {0} is replaced with the resource name in the output file name.
HtmlViewOptionsviewOptions=HtmlViewOptions.forExternalResources("output.html","output/resource_{0}","output/resource_{0}");viewer.view(viewOptions);}
The result is shown below. External resources are placed in a separate folder.
Render email messages as PDF
Create a PdfViewOptions class instance and pass it to the Viewer.view method to convert an email message to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file, reorder its pages, and specify the quality of document images. Refer to the following documentation section for details: Rendering to PDF.
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.PdfViewOptions;// ...
try(Viewerviewer=newViewer("sample.eml")){// Create a PDF file for the letter.
PdfViewOptionsviewOptions=newPdfViewOptions("output.pdf");viewer.view(viewOptions);}
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.PngViewOptions;// ...
try(Viewerviewer=newViewer("sample.eml")){// Create a PNG image for the letter.
PngViewOptionsviewOptions=newPngViewOptions("output.png");// Set width and height.
viewOptions.setWidth(950);viewOptions.setHeight(550);viewer.view(viewOptions);}
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.JpgViewOptions;// ...
try(Viewerviewer=newViewer("sample.eml")){// Create a JPG image for the letter.
JpgViewOptionsviewOptions=newJpgViewOptions("output.jpg");// Set width and height.
viewOptions.setWidth(950);viewOptions.setHeight(550);viewer.view(viewOptions);}
Specify rendering options
GroupDocs.Viewer supports the EmailOptions class that allows you to specify different options for rendering email messages. To access these options, use the setEmailOptions method for one of the following classes (depending on the output file format):
GroupDocs.Viewer allows you to specify page size for the output file when you convert an email message to HTML, PDF, or image format. Use the EmailOptions.setPageSize method to select one of the predefined page sizes (LETTER, LEDGER, A0, A1, A2, A3, or A4) (see the PageSize enumeration).
The following example specifies page size for the output PDF file:
GroupDocs.Viewer allows you to change how standard fields (such as From, To, Subject, and so on) are displayed in the email message header.
Use the EmailOptions.setFieldTextMap method to specify custom field labels. Static fields of the Field class allow you to access default email header fields, as shown in the example below.
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.Field;importcom.groupdocs.viewer.options.HtmlViewOptions;importjava.util.HashMap;importjava.util.Map;// ...
try(Viewerviewer=newViewer("sample.eml")){// Specify custom field labels.
Map<Field,String>map=newHashMap<>();map.put(Field.FROM,"Sender");map.put(Field.TO,"Recipient");map.put(Field.SENT,"Date");map.put(Field.SUBJECT,"Email subject");// Create an HTML file.
HtmlViewOptionsviewOptions=HtmlViewOptions.forEmbeddedResources("output.html");viewOptions.getEmailOptions().setFieldTextMap(map);viewer.view(viewOptions);}
The following image illustrates the result:
Specify the date and time format and change the time zone
When rendering email messages, GroupDocs.Viewer formats date and time information in the message header based on the system’s date and time settings. If you want to change the default date and time format or specify the time zone offset, use the following methods:
EmailOptions.setDateTimeFormat—Allows you to define a custom date and time format. See the following topics for details:
EmailOptions.setTimeZoneOffset—Specifies the hours and minutes offset from UTC. The offset is displayed with a leading sign. A plus sign (+) indicates time ahead of UTC, and a minus sign (-) indicates time behind UTC.
importcom.groupdocs.viewer.Viewer;importcom.groupdocs.viewer.options.HtmlViewOptions;importcom.groupdocs.viewer.utils.PathUtils;importjava.util.TimeZone;// ...
try(Viewerviewer=newViewer("sample.eml")){// Create an HTML file
HtmlViewOptionsoptions=HtmlViewOptions.forEmbeddedResources("output.html");// Apply a custom format to the date in the email message header.
options.getEmailOptions().setDateTimeFormat("MM d yyyy HH:mm tt zzz");// Specify the time zone offset.
options.getEmailOptions().setTimeZoneOffset(TimeZone.getTimeZone("GMT+1"));viewer.view(options);}
The following image illustrates the result:
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.