Search code examples
javajakarta-mailmessagemime-message

MimeMessage change the sendData on existing Mail


try (
            FileInputStream input = new FileInputStream(source);
            FileOutputStream output = new FileOutputStream(target);
    ) {
        byte[] emlBytes = IOUtils.toByteArray(input);
        Message mimeMessage = createMimeMessage(emlBytes);

        String date = "Wed, 16 Oct 2013 11:25:07 +0200";
        mimeMessage.setHeader("Date", date);
        System.out.println(mimeMessage.getSentDate());
        output.write(emlBytes);

    } catch (MessagingException e) {
        e.printStackTrace();

}

I have to change the send date on existing email file. But after the execution the file have the same date as before. It doesnt get the new send date. I think it is because mimeMessage creates a new instance but doesnt change the emlBytes but I have to pass a emlBytes in output.write() I dont know how to pass the emlBytes which was customed by mimeMessage. What can I do?


Solution

  • MimeMessage isn't going to change the original source of the data since it comes from an InputStream. But you can use the MimeMessage.writeTo method to write the updated message to an OutputStream, e.g.,

    mimeMessage.writeTo(output);