如何使用X. Unsent将电子邮件另存为.eml:1使用Message.writeTo

时间:2017-12-01 10:52:34

标签: java email outlook

我希望将电子邮件保存为带附件的.eml格式。我能够保存.eml,但是当我打开它时,它应该以草稿格式打开,用户应该能够从,输入,主题和编辑电子邮件正文。 我必须手动编辑保存的.eml文件以添加X-Unsent:1以显示为草稿。我怎样才能在代码中实现这一点,mimeMessage的哪个属性可以帮助我做到这一点?

public static void createMessage(String to, String from, String subject, String body, List<File> attachments) {
try {
    Message message = new MimeMessage(Session.getInstance(System.getProperties()));
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
    message.setSubject(subject);
    // create the message part 
    MimeBodyPart content = new MimeBodyPart();
    // fill message
    content.setText(body);
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(content);
    // add attachments
    for(File file : attachments) {
        MimeBodyPart attachment = new MimeBodyPart();
        DataSource source = new FileDataSource(file);
        attachment.setDataHandler(new DataHandler(source));
        attachment.setFileName(file.getName());
        multipart.addBodyPart(attachment);
    }
    // integration
    message.setContent(multipart);
    // store file
    message.writeTo(new FileOutputStream(new File("c:/mail.eml")));
} catch (MessagingException ex) {
    Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
    Logger.getLogger(Mailkit.class.getName()).log(Level.SEVERE, null, ex);
}

}

1 个答案:

答案 0 :(得分:1)

如何添加此行:

 message.setHeader("X-Unsent", "1");