Convert each email attachment to different format
Leave feedback
To convert each attachment within an email to a different format using GroupDocs.Conversion for Java, you can utilize the EmailLoadOptions class. This class provides various properties to customize the loading and conversion of email documents.
The following code snippet shows how to convert each attachment to a different format based on attachment type:
importcom.groupdocs.conversion.Converter;importcom.groupdocs.conversion.contracts.ConvertOptionsProvider;importcom.groupdocs.conversion.contracts.LoadOptionsProvider;importcom.groupdocs.conversion.contracts.SaveDocumentStreamForFileType;importcom.groupdocs.conversion.filetypes.EmailFileType;importcom.groupdocs.conversion.filetypes.WordProcessingFileType;importcom.groupdocs.conversion.options.convert.ImageConvertOptions;importcom.groupdocs.conversion.options.convert.PdfConvertOptions;importcom.groupdocs.conversion.options.convert.WordProcessingConvertOptions;importcom.groupdocs.conversion.options.load.EmailLoadOptions;importcom.groupdocs.conversion.utils.common.Path;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;publicclassConvertEachEmailAttachmentToDifferentFormat{publicstaticvoidconvert(){finalint[]index={0};LoadOptionsProviderloadOptionsProvider=()->{EmailLoadOptionsemailOpt=newEmailLoadOptions();emailOpt.setConvertOwner(true);emailOpt.setConvertOwned(true);emailOpt.setDepth(2);returnemailOpt;};SaveDocumentStreamForFileTypeconvertedStreamProvider=(fileType)->{StringfileName=Path.combine(outputFolder,String.format("converted_%d.%s",index[0],fileType.getExtension()));index[0]++;try{returnnewFileOutputStream(fileName);}catch(FileNotFoundExceptione){thrownewRuntimeException(e);}};ConvertOptionsProviderconvertOptionsProvider=(sourceFileName,fileType)->{if(fileType==EmailFileType.Eml){returnnewWordProcessingConvertOptions();}if(fileType==WordProcessingFileType.Txt){returnnewPdfConvertOptions();}returnnewImageConvertOptions();};// Initialize the converter with the source email document
try(Converterconverter=newConverter(Constants.SAMPLE_EML_WITH_ATTACHMENT,loadOptionsProvider)){converter.convert(convertedStreamProvider,convertOptionsProvider);}}publicstaticvoidmain(String[]args){convert();}}
with-attachment.eml is sample file used in this example. Click here to download it.
converted_0.docx is converted DOCX document. Click here to download it.
converted_1.pdf is converted attachment PDF document. Click here to download it.
In this example, the Converter class is initialized with the source email document and EmailLoadOptions. Using the convertOptionsProvider, it becomes possible to apply appropriate ConvertOptions to specify the target format for each conversion. Then, the convertedStreamProvider is invoked to handle and save each conversion.
By utilizing these options, you can effectively control the loading and conversion process of email attachments to meet your specific requirements.
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.