MSG to PDF, set locale for the mail header (from, sent, to, subject...)

Hello guys,

I am working with Aspose Email + Aspose Words (Java) to render a .msg file to a .pdf file. I’m using exaclty the code given by the doc and it works pretty well, the render in PDF looks almost perfect ! However, I have a problem with the “header” of the email which contains the “From, Sent, To, Subject” fields and they are in English and i couldn’t write them in French in the PDF.

image.png (5.7 KB)

It’s the same for the date of sending by the way.

I tried many things like setting the Locale and the CurrentThreadLocale like it’s done here but nothing works:'(

Does anybody have any idea to solve this problem ? Maybe it’s not possible at all ?

Thanks in advance for your help !

@Loyke

Could you please ZIP and attach your input MSG and problematic output MHTML file here for testing? We will investigate the issue and provide you more information on it.

Hi @tahir.manzoor,

Thanks for helping me, you will find attached a zip file containing :

  • An example .msg file
  • The corresponding .mhtml
  • The corresponding .pdf rendering

example msg.zip (93.9 KB)

And also here is my code to generate the PDF file from the .msg InputStream :

public AsposeFileDTO msgToPdf(MultipartFile file) throws Exception {
    Locale.setDefault(new Locale("fr", "FR"));
    CurrentThreadSettings.setLocale("fr-FR");

    try(MailMessage msg = MailMessage.load(file.getInputStream())) {
        ByteArrayOutputStream emlStream = new ByteArrayOutputStream();
        msg.save(emlStream, SaveOptions.getDefaultMhtml());
        Document doc = new Document(new ByteArrayInputStream(emlStream.toByteArray()));

        ByteArrayOutputStream foStream = new ByteArrayOutputStream();
        doc.save(foStream, SaveFormat.PDF);

        String filename = msg.getSubject().replaceAll("[^a-zA-Z]+", " ").trim();
        return new AsposeFileDTO(filename + ".pdf", "application/pdf", foStream.toByteArray());
    }
    catch (Exception e) {
        throw FonctionnelException.format(GedErrorEnum.AUX_500_FILE_CONVERT_ERROR);
    }
}

Thanks you in advance,
Antoine C.

@Loyke

We have logged this problem in our issue tracking system as EMAILJAVA-35039. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi,

I wanted to know if you had any news to give me about the issue.
Also, we have the paid license, maybe we should go through the paid support?

Thanks in advance for your answer!
Best regards,
Antoine C.

@Loyke

You can use MhtSaveOptions.FormatTemplates property as shown below to write headers in French:

CurrentThreadSettings.setLocale("fr-FR");

MailMessage msg = new MailMessage("from@host.com", "to@host.com", "subject", "body");

MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.getFormatTemplates().set_Item(MhtTemplateName.DATE_TIME, "dddd d MMMM yyyy HH:mm");
mhtSaveOptions.getFormatTemplates().set_Item(MhtTemplateName.FROM,
        mhtSaveOptions.getFormatTemplates().get_Item(MhtTemplateName.FROM).replace("From:", "De :"));
mhtSaveOptions.getFormatTemplates().set_Item(MhtTemplateName.SENT,
        mhtSaveOptions.getFormatTemplates().get_Item(MhtTemplateName.SENT).replace("Sent:", "Envoyé :"));
mhtSaveOptions.getFormatTemplates().set_Item(MhtTemplateName.TO,
        mhtSaveOptions.getFormatTemplates().get_Item(MhtTemplateName.TO).replace("To:", "À :"));
mhtSaveOptions.getFormatTemplates().set_Item(MhtTemplateName.CC,
        mhtSaveOptions.getFormatTemplates().get_Item(MhtTemplateName.CC).replace("Cc:", "Copie :"));
mhtSaveOptions.getFormatTemplates().set_Item(MhtTemplateName.SUBJECT,
        mhtSaveOptions.getFormatTemplates().get_Item(MhtTemplateName.SUBJECT)
                .replace("Subject:", "Objet :"));

msg.save("test.mhtml", mhtSaveOptions);

Output:

De:     from@host.com
Envoyé: lundi 11 avril 2022 09:00
À:      to@host.com
Objet:  subject

body
1 Like

Hi,

Amazing ! It works perfectly well !! Thanks you so much for your help :slightly_smiling_face:

Best regards,
Antoine C.