You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Msg2txt.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.hsmf;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.io.PrintWriter;
  21. import org.apache.poi.hsmf.MAPIMessage;
  22. import org.apache.poi.hsmf.datatypes.AttachmentChunks;
  23. import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
  24. /**
  25. * Reads one or several Outlook MSG files and for each of them creates
  26. * a text file from available chunks and a directory that contains
  27. * attachments.
  28. */
  29. @SuppressWarnings({"java:S106","java:S4823"})
  30. public class Msg2txt {
  31. /**
  32. * The stem used to create file names for the text file and the directory
  33. * that contains the attachments.
  34. */
  35. private String fileNameStem;
  36. /**
  37. * The Outlook MSG file being processed.
  38. */
  39. private MAPIMessage msg;
  40. public Msg2txt(String fileName) throws IOException {
  41. fileNameStem = fileName;
  42. if(fileNameStem.endsWith(".msg") || fileNameStem.endsWith(".MSG")) {
  43. fileNameStem = fileNameStem.substring(0, fileNameStem.length() - 4);
  44. }
  45. msg = new MAPIMessage(fileName);
  46. }
  47. /**
  48. * Processes the message.
  49. *
  50. * @throws IOException if an exception occurs while writing the message out
  51. */
  52. public void processMessage() throws IOException {
  53. String txtFileName = fileNameStem + ".txt";
  54. String attDirName = fileNameStem + "-att";
  55. try (PrintWriter txtOut = new PrintWriter(txtFileName, "UTF-8")) {
  56. try {
  57. String displayFrom = msg.getDisplayFrom();
  58. txtOut.println("From: " + displayFrom);
  59. } catch (ChunkNotFoundException e) {
  60. // ignore
  61. }
  62. try {
  63. String displayTo = msg.getDisplayTo();
  64. txtOut.println("To: " + displayTo);
  65. } catch (ChunkNotFoundException e) {
  66. // ignore
  67. }
  68. try {
  69. String displayCC = msg.getDisplayCC();
  70. txtOut.println("CC: " + displayCC);
  71. } catch (ChunkNotFoundException e) {
  72. // ignore
  73. }
  74. try {
  75. String displayBCC = msg.getDisplayBCC();
  76. txtOut.println("BCC: " + displayBCC);
  77. } catch (ChunkNotFoundException e) {
  78. // ignore
  79. }
  80. try {
  81. String subject = msg.getSubject();
  82. txtOut.println("Subject: " + subject);
  83. } catch (ChunkNotFoundException e) {
  84. // ignore
  85. }
  86. try {
  87. String body = msg.getTextBody();
  88. txtOut.println(body);
  89. } catch (ChunkNotFoundException e) {
  90. System.err.println("No message body");
  91. }
  92. AttachmentChunks[] attachments = msg.getAttachmentFiles();
  93. if (attachments.length > 0) {
  94. File d = new File(attDirName);
  95. if (d.mkdir()) {
  96. for (AttachmentChunks attachment : attachments) {
  97. processAttachment(attachment, d);
  98. }
  99. } else {
  100. System.err.println("Can't create directory " + attDirName);
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Processes a single attachment: reads it from the Outlook MSG file and
  107. * writes it to disk as an individual file.
  108. *
  109. * @param attachment the chunk group describing the attachment
  110. * @param dir the directory in which to write the attachment file
  111. * @throws IOException when any of the file operations fails
  112. */
  113. public void processAttachment(AttachmentChunks attachment,
  114. File dir) throws IOException {
  115. String fileName = attachment.getAttachFileName().toString();
  116. if(attachment.getAttachLongFileName() != null) {
  117. fileName = attachment.getAttachLongFileName().toString();
  118. }
  119. File f = new File(dir, fileName);
  120. try (OutputStream fileOut = new FileOutputStream(f)) {
  121. fileOut.write(attachment.getAttachData().getValue());
  122. }
  123. }
  124. /**
  125. * Processes the list of arguments as a list of names of Outlook MSG files.
  126. *
  127. * @param args the list of MSG files to process
  128. */
  129. public static void main(String[] args) {
  130. if(args.length <= 0) {
  131. System.err.println("No files names provided");
  132. } else {
  133. for (String arg : args) {
  134. try {
  135. Msg2txt processor = new Msg2txt(arg);
  136. processor.processMessage();
  137. } catch (IOException e) {
  138. System.err.println("Could not process " + arg + ": " + e);
  139. }
  140. }
  141. }
  142. }
  143. }