在java中创建一个电子邮件对象并将其保存到文件中

发布于 2024-12-15 21:24:46 字数 490 浏览 3 评论 0原文

我需要备份 PST 文件(Outlook 存储)中包含的电子邮件。 我正在使用 libpst,这是我在网上找到的唯一免费库 ( http://code.google.com/p/java-libpst/

这样我就可以访问每封电子邮件中的所有信息 (主题、正文、发件人 ecc ..),但我需要将它们放在一个文件中

,有人说您可以从“javax.mail.Message”对象创建 EML 文件: 用 Java 创建 .eml(电子邮件)文件

问题是:我如何创建这个消息对象? 我没有服务器或电子邮件会话,只有电子邮件中包含的信息

。 创建 .msg 文件也可以

i need to backup the emails contained in a PST file (outlook storage).
i'm using libpst which is the only free library i found on the 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

here someone said you can create an EML file from a "javax.mail.Message" object:
Create a .eml (email) file in Java

the problem is: how do i create this Message object?
i don't have a server or an email session, just the information contained in the email

p.s.
creating a .msg file would be fine too

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仅冇旳回忆 2024-12-22 21:24:46

下面是使用 java mail api 创建有效 eml 文件的代码。与雷鸟和其他电子邮件客户端配合良好:

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

Here's the code to create a valid eml file with java mail api. works fine with thunderbird and probably other email clients:

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);
    }
}
[旋木] 2024-12-22 21:24:46

您创建一个 Message 对象的方式与创建发送对象的方式相同,
但不是将其发送,而是将其写入文件。您不需要电子邮件
服务器。演示程序中有很多创建消息的示例
包含在 JavaMail 下载 中,以及 JavaMail 常见问题解答。请参阅
Message.writeTo 方法 将消息写入文件(消息是一个部分,
并且 writeTo 位于部分)。

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. 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. See the
Message.writeTo method to write the message to a file (Message is a Part,
and writeTo is on Part).

愛上了 2024-12-22 21:24:46

您可以使用 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);
        }
    }

You can use mimeMessageHelper to create the message , as given below:

    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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文