image (3).png (1.4 MB)
While convert mail message to pst format html markup is broken
@Viktoriia,
Thank you for posting the query. Please check the issue using the latest version of Aspose.Email. If the issue persists, please share the following:
Hi Andrey
I updated library to latest version and it doesn’t help meArchive.zip (43.3 KB)
File targetFile = new File(String.format("%s.pst", PST_SCRIPTS));
PersonalStorage storage = PersonalStorage.create(targetFile.getAbsolutePath(), 0);
InputStream targetStream = new FileInputStream(MBOX_FILE);
FolderInfo folder = storage.getRootFolder().addSubFolder(NodeNameUtils.getDownloadNodeName(new Date()));
String mailBody = IOUtils.toString(targetStream, StandardCharsets.UTF_8.name());
var mailMessage = new MailMessage();
mailMessage.setSubject("Test");
mailMessage.setBodyEncoding(StandardCharsets.UTF_8);
mailMessage.setHtmlBody(mailBody);
mailMessage.setDate(new Date());
folder.addMessage(MapiMessage.fromMailMessage(mailMessage, MapiConversionOptions.getUnicodeFormat()));
storage.close();
@Viktoriia,
Thank you for the additional data. You should use MboxrdStorageReader
class to migrate email messages from the MBOX file to the PST file as shown below:
MboxLoadOptions loadOptions = new MboxLoadOptions();
MboxrdStorageReader mboxReader = new MboxrdStorageReader(mboxFilePath, loadOptions);
MapiConversionOptions conversionOptions = MapiConversionOptions.getUnicodeFormat();
MailMessage mailMessage;
while ((mailMessage = mboxReader.readNextMessage()) != null)
{
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage, conversionOptions);
pstFolder.addMessage(mapiMessage);
}
API Reference: MboxrdStorageReader class
But there is something wrong with your MBOX file, the final PST becomes empty. So I added a ticket with ID EMAILJAVA-34930 in our tracking system. Our development team will investigate this case. We will inform you of any progress.
the implementation you describe is not suitable for my process because i get message body from cloud storage in bytes
it can slow down the process if i convert each file to mbox and then to pst
var message = localMailRepository.getObject(downloadObject);
var mailMessage = new MailMessage();
mailMessage.setSubject(message.getSubject());
mailMessage.setTo(getMailAddressCollection(message.getTo()));
mailMessage.setFrom(new MailAddress(message.getFrom()));
mailMessage.setDate(message.getCreatedAt().getTime());
mailMessage.setBodyEncoding(StandardCharsets.UTF_8);
try (final StorageObjectInputStream inputStream = storageService.get(
StorageHelper.makeObjectKey(context.getAccount().getId(),
context.getUser().getId(),
GoogleServiceType.GOOGLE_MAIL.toString(),
message.getContentId()))
) {
String mailBody = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
mailMessage.setHtmlBody(mailBody);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
stateServerService.objectProcessingError(ex, message);
}
folder.addMessage(MapiMessage.fromMailMessage(mailMessage, MapiConversionOptions.getUnicodeFormat()));
});
try {
zip.put(pst, pst.getName());
zip.finish();
} catch (IOException e) {
log.error(e.getMessage(), e);
log.error("Failed to finish actions chain: close zip > upload zip > markup uploaded files > cleanup", e);
}finally {
storage.close();
}
@Viktoriia,
In your first code snippet, the targetStream
object contains a full EML message including headers. You cannot use it as a mail body only. If the ‘inputStream’ object in the second code snippet is the same case, you can add the message to the PST file as shown below:
MailMessage mailMessage = MailMessage.load(inputStream);
folder.addMessage(MapiMessage.fromMailMessage(mailMessage, MapiConversionOptions.getUnicodeFormat()));
API Reference: MailMessage class
More examples:
Loading and Saving Message
Creating and Setting Contents of Emails
Extracting Message Contents from Emails
thanks a lot