简体   繁体   English

在java中创建一个email object并保存到文件中

[英]Create an email object in java and save it to file

i need to backup the emails contained in a PST file (outlook storage).我需要备份 PST 文件(Outlook 存储)中包含的电子邮件。 i'm using libpst which is the only free library i found on the web ( http://code.google.com/p/java-libpst/ )我正在使用 libpst,这是我在 web ( http://code.google.com/p/java-libpst/ ) 上找到的唯一免费库

so i can access all the information in each single email (subject, body, sender ecc..), but i need to put them on a file所以我可以访问每个 email 中的所有信息(主题、正文、发件人 ecc..),但我需要将它们放在一个文件中

here someone said you can create an EML file from a "javax.mail.Message" object: Create a.eml (email) file in Java这里有人说你可以从“javax.mail.Message”创建一个 EML 文件 object: Create a.eml (email) file in Java

the problem is: how do i create this Message object?问题是:如何创建此消息 object? i don't have a server or an email session, just the information contained in the email我没有服务器或 email session,只有 email 中包含的信息

ps creating a.msg file would be fine too ps 创建一个.msg 文件也可以

Here's the code to create a valid eml file with java mail api. 这是使用java mail api创建有效eml文件的代码。 works fine with thunderbird and probably other email clients: 与thunderbird和其他电子邮件客户端一起工作正常:

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);
    }
}

You create a Message object the same way you would create one for sending, but instead of sending it you write it to a file. 您创建一个Message对象的方式与创建一个Message对象的方式相同,但不是发送它而是将其写入文件。 You don't need an email server. 您不需要电子邮件服务器。 There's lots of examples of creating messages in the demo programs included with the JavaMail download , and in the JavaMail FAQ . JavaMail下载JavaMail FAQ中包含的演示程序中有很多创建消息的示例。 See the Message.writeTo method to write the message to a file (Message is a Part, and writeTo is on Part). 请参阅Message.writeTo方法将消息写入文件(Message是一个Part,writeTo在Part上)。

You can use mimeMessageHelper to create the message, as given below:您可以使用 mimeMessageHelper 来创建消息,如下所示:

    import org.springframework.mail.javamail.MimeMessageHelper;
    
    public methodTocreateMessageObject(){
    MimeMessage message = mailSender.createMimeMessage();
    
    MimeMessageHelper mh= new MimeMessageHelper(message, true);
    mh.setSubject(subject);
    mh.setTo(toArray(to));
    if (CollectionUtils.isNotEmpty(cc)) {
    mh.setCc(toArray(cc));
    }
    mh.setFrom(from);
    
    mh.setText(body, true);
    
            if (attachmentFilenames != null) {
                for (String filename : attachmentFilenames) {
                    FileSystemResource file = new FileSystemResource(filename);
                    mh.addAttachment(file.getFilename(), file);
                }
            }
    
            if (inlineAttachments != null && contentType!=null) {
                for (Entry<String, byte[]> inlineAttach : inlineAttachments.entrySet()) {
    
                    String cId = inlineAttach.getKey();
                    byte[] attachInByteStream = inlineAttach.getValue();
                    InputStreamSource attachSource = new ByteArrayResource(attachInByteStream);
                    mh.addInline(cId, attachSource, contentType);
    
                }
            }
ByteArrayOutputStream output = new ByteArrayOutputStream();
        message.writeTo(output);

        Date lastUpdatetime = new Date();

        try(OutputStream outputStream = new FileOutputStream("D:/mail2.eml")) {
            output.writeTo(outputStream);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM