Convert each email attachment to different format

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:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.contracts.ConvertOptionsProvider;
import com.groupdocs.conversion.contracts.LoadOptionsProvider;
import com.groupdocs.conversion.contracts.SaveDocumentStreamForFileType;
import com.groupdocs.conversion.filetypes.EmailFileType;
import com.groupdocs.conversion.filetypes.WordProcessingFileType;
import com.groupdocs.conversion.options.convert.ImageConvertOptions;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.options.convert.WordProcessingConvertOptions;
import com.groupdocs.conversion.options.load.EmailLoadOptions;
import com.groupdocs.conversion.utils.common.Path;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ConvertEachEmailAttachmentToDifferentFormat {
    public static void convert() {
        final int[] index = {0};

        LoadOptionsProvider loadOptionsProvider = ()->{
              EmailLoadOptions emailOpt =  new EmailLoadOptions();
              emailOpt.setConvertOwner(true);
              emailOpt.setConvertOwned(true);
              emailOpt.setDepth(2);
              return emailOpt;
        };

        SaveDocumentStreamForFileType convertedStreamProvider = (fileType)->{
            String fileName = Path.combine(outputFolder, String.format("converted_%d.%s", index[0], fileType.getExtension()));
            index[0]++;
            try {
                return new FileOutputStream(fileName);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        };

        ConvertOptionsProvider convertOptionsProvider = (sourceFileName, fileType) -> {
            if (fileType == EmailFileType.Eml)
            {
                return new WordProcessingConvertOptions();
            }

            if (fileType == WordProcessingFileType.Txt)
            {
                return new PdfConvertOptions();
            }

            return new ImageConvertOptions();
        };

        // Initialize the converter with the source email document
        try(Converter converter = new Converter(Constants.SAMPLE_EML_WITH_ATTACHMENT, loadOptionsProvider))
        {
            converter.convert(convertedStreamProvider, convertOptionsProvider);
        }
    }

    public static void main(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.