【问题标题】:Create an email object in java and save it to file在java中创建一个电子邮件对象并将其保存到文件
【发布时间】:2011-12-31 08:11:21
【问题描述】:

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

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

这里有人说您可以从“javax.mail.Message”对象创建一个 EML 文件: Create a .eml (email) file in Java

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

附言 创建一个 .msg 文件也可以

【问题讨论】:

    标签: java email jakarta-mail pst eml


    【解决方案1】:

    创建消息对象的方式与创建消息对象的方式相同, 但不是发送它,而是将它写入文件。您不需要电子邮件 服务器。演示程序中有很多创建消息的示例 包含在JavaMail downloadJavaMail FAQ 中。见 Message.writeTo method 将消息写入文件(消息是一个部分, 并且 writeTo 在 Part)。

    【讨论】:

      【解决方案2】:

      这是使用 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);
          }
      }
      

      【讨论】:

      • 为什么你们中的任何一个 java 回答者都不包含imports?
      • 有什么办法可以去掉生成的Message-ID?
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-14
        • 1970-01-01
        • 2017-02-08
        • 2021-09-02
        • 2021-10-17
        相关资源
        最近更新 更多