{"id":1643,"date":"2019-12-11T21:52:30","date_gmt":"2019-12-11T12:52:30","guid":{"rendered":"https:\/\/esoro.jp\/?p=1643"},"modified":"2019-12-11T21:52:30","modified_gmt":"2019-12-11T12:52:30","slug":"msg%e5%bd%a2%e5%bc%8f%e3%81%ae%e3%83%a1%e3%83%bc%e3%83%ab%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92eml%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ab%e5%a4%89%e6%8f%9b%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/esoro.jp\/?p=1643","title":{"rendered":"msg\u5f62\u5f0f\u306e\u30e1\u30fc\u30eb\u30d5\u30a1\u30a4\u30eb\u3092eml\u30d5\u30a1\u30a4\u30eb\u306b\u5909\u63db\u3059\u308b"},"content":{"rendered":"<p>\u3000\u6570\u5e74\u524d\u306bjavamail\u3067\u4f5c\u3063\u305f\u30e1\u30fc\u30eb\u3092\u53d7\u4fe1\u3057\u305f\u5f8c\u306e\u696d\u52d9\u3092\u81ea\u52d5\u5316\u3059\u308b\u30c4\u30fc\u30eb\u3092\u3001\u4eca\u3067\u3082\u3044\u308d\u3044\u308d\u6a5f\u80fd\u8ffd\u52a0\u3092\u3057\u3066\u3044\u308b\u306e\u3067\u3059\u304c\u3001\u6a5f\u80fd\u3092\u8ffd\u52a0\u30fb\u5909\u66f4\u3059\u308b\u6642\u306b\u3001\u5229\u7528\u8005\u304b\u3089\u5bfe\u8c61\u306e\u30e1\u30fc\u30eb\u3092eml\u3067\u63d0\u4f9b\u3057\u3066\u3082\u3089\u3063\u3066\u30c6\u30b9\u30c8\u3057\u3066\u3044\u307e\u3059\u3002<br \/>\n\u3000\u3067\u3082\u3001outlook\u30e6\u30fc\u30b6\u30fc\u306fmsg\u3067\u3057\u304b\u63d0\u4f9b\u51fa\u6765\u307e\u305b\u3093\u3068\u3044\u3046\u306e\u3067\u3001eml\u3078\u5909\u63db\u3057\u305f\u3044\u306e\u3067\u3059\u304c\u3001\u30cd\u30c3\u30c8\u306b\u8ee2\u304c\u3063\u3066\u308b\u30c4\u30fc\u30eb\u3092\u4f7f\u3063\u3066\u3082\u30a4\u30de\u30a4\u30c1\u5909\u63db\u3067\u304d\u307e\u305b\u3093\u3002\u3002<br \/>\n\u3000\u306a\u3089\u4f5c\u308d\u3046\u3068\u8abf\u3079\u305f\u6240\u3001java\u306a\u3089jotlmsg\u3068\u3044\u3046\u306e\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u65e5\u672c\u8a9e\u304c\u6587\u5b57\u5316\u3051\u3057\u307e\u3059\u3002<br \/>\n\u3000\u7d50\u5c40\u3001apache poi\u3067outlook\u306emsg\u3092\u6271\u3048\u308b\u3088\u3046\u306a\u306e\u3067\u3001msg\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3093\u3067eml\u30d5\u30a1\u30a4\u30eb\u3078\u51fa\u529b\u3059\u308b\u3068\u3044\u3046\u5f62\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3000\u5b9f\u884c\u6642\u306e\u5f15\u6570\u3092\u3001msg\u30d5\u30a1\u30a4\u30eb\u3092\u5165\u308c\u305f\u30d1\u30b9\u3001eml\u30d5\u30a1\u30a4\u30eb\u3092\u5410\u304d\u51fa\u3059\u30d1\u30b9\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p>\u3000\u4f55\u4ef6\u304b\u306emsg\u30d5\u30a1\u30a4\u30eb\u3092\u8a66\u3057\u305f\u6240\u3001html\u3067\u53d6\u308c\u308b\u30b1\u30fc\u30b9\u304c\u7121\u304b\u3063\u305f\u3002\u3002\u30e1\u30bd\u30c3\u30c9\u7684\u306b\u306f\u3042\u308b\u306f\u305a\u306a\u306e\u306b\u3002\u3002<br \/>\n\u3000\u305d\u306e\u70ba\u3001\u5b8c\u5168\u306beml\u30d5\u30a1\u30a4\u30eb\u3067\u518d\u73fe\u51fa\u6765\u308b\u3068\u3044\u3046\u30ec\u30d9\u30eb\u306b\u306f\u306a\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u304c\u3001\u672c\u6587\u30c6\u30ad\u30b9\u30c8\u3068\u6dfb\u4ed8\u30d5\u30a1\u30a4\u30eb\u306feml\u30d5\u30a1\u30a4\u30eb\u306b\u51fa\u529b\u3067\u304d\u3066\u3044\u308b\u306e\u3067\u3001\u307e\u3042\u3053\u3053\u307e\u3067\u3067\u3088\u3044\u304b\u3068\u3002<\/p>\n<pre class=\"lang:java decode:true \" title=\"MsgtoEml.java\" >package jp.esoro.mail.main;\r\n\r\nimport java.io.File;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.util.Properties;\r\n\r\nimport javax.mail.MessagingException;\r\nimport javax.mail.Multipart;\r\nimport javax.mail.Session;\r\nimport javax.mail.internet.MimeBodyPart;\r\nimport javax.mail.internet.MimeMessage;\r\nimport javax.mail.internet.MimeMultipart;\r\n\r\nimport org.apache.commons.lang3.StringUtils;\r\nimport org.apache.poi.hsmf.MAPIMessage;\r\nimport org.apache.poi.hsmf.datatypes.AttachmentChunks;\r\nimport org.apache.poi.hsmf.exceptions.ChunkNotFoundException;\r\n\r\npublic class MsgtoEml {\r\n\tpublic static void main(String[] args)  {\r\n\t\tString msgdir = null;\r\n\t\tString emldir = null;\r\n\t\tfor(String arg : args){\u00e5\r\n\t\t\tif(msgdir == null){\r\n\t\t\t\tmsgdir = arg;\r\n\t\t\t}\r\n\t\t\telse{\r\n\t\t\t\temldir = arg;\r\n\t\t\t}\r\n\t\t}\r\n\t\tif(msgdir == null || emldir == null){\r\n\t\t\tSystem.out.println(\"first arg is msg-file path. second arg is export-eml path\");\r\n\t\t\tSystem.exit(100);\r\n\t\t}\r\n    \tFile dir = new File(msgdir);\r\n    \tFile[] files = dir.listFiles();  \r\n    \t\r\n        Properties prop = new Properties();\r\n\t    Session session = Session.getDefaultInstance(prop, null);\r\n\t    \r\n\t\tfor(File file: files){\r\n\t\t\tif( file.isDirectory() ) {\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\t\t\tif(StringUtils.endsWithIgnoreCase(file.getName(),\"msg\")){\r\n\t\t\t\ttry {\r\n\t\t\t\t\tFileOutputStream fos = new FileOutputStream(emldir + \"\/\" + StringUtils.replaceIgnoreCase(file.getName(), \"msg\", \"eml\"));\r\n\t\t\t\t\t\r\n\t\t\t\t\tMAPIMessage  msg = new MAPIMessage(file);\r\n\t\t\t\t\tMimeMessage eml = new MimeMessage(session);\r\n\t\t\t\t\tString headerValue = \"\";\r\n\t\t\t\t\tString headerKey = \"\";\r\n\t\t\t\t\tfor(String head: msg.getHeaders()){\r\n\t\t\t\t\t\tif(head.startsWith(\" \")){\r\n\t\t\t\t\t\t\theaderValue = headerValue + System.lineSeparator() +head;\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\telse{\r\n\t\t\t\t\t\t\tif(headerKey.length() &gt; 0){\r\n\t\t\t\t\t\t\t\teml.setHeader(headerKey, headerValue);\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\tString[] h = head.split(\":\");\r\n\t\t\t\t\t\t\tif(h.length &gt; 1){\r\n\t\t\t\t\t\t\t\theaderKey = head.split(\":\")[0];\r\n\t\t\t\t\t\t\t\theaderValue = head.split(\":\")[1];\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\teml.setSubject(msg.getSubject(), \"UTF-8\");\r\n\t\t\t\t\tif(msg.getAttachmentFiles() != null){\r\n\t\t\t\t\t\tMimeBodyPart mbody = new MimeBodyPart();\r\n\t\t\t\t\t\ttry {\r\n\t\t\t\t\t\t\tmbody.setContent(msg.getHtmlBody(),\"text\/html; charset=UTF-8\");\r\n\t\t\t\t\t\t} catch (ChunkNotFoundException e) {\r\n\t\t\t\t\t\t\tmbody.setContent(msg.getTextBody(),\"text\/plain; charset=UTF-8\");\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\tMultipart mp = new MimeMultipart();\r\n\t\t\t\t\t\tmp.addBodyPart(mbody);\r\n\t\t\t\t\t\tfor (AttachmentChunks chunks : msg.getAttachmentFiles()){\r\n\t\t\t\t\t\t\tMimeBodyPart mbAttachment = new MimeBodyPart();\r\n\t\t\t\t\t\t\tmbAttachment.setContent(chunks.getAttachData().getValue(), \"application\/octet-stream\");\r\n\t\t\t\t\t\t\tmbAttachment.setFileName(chunks.getAttachFileName().getValue());\r\n\t\t\t\t\t\t\tmp.addBodyPart(mbAttachment);\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\teml.setContent(mp);\r\n\t\t\t\t\t}\r\n\t\t\t\t\telse{\r\n\t\t\t\t\t\ttry {\r\n\t\t\t\t\t\teml.setContent(msg.getHtmlBody(),\"text\/html; charset=UTF-8\");\r\n\t\t\t\t\t\t} catch (ChunkNotFoundException e) {\r\n\t\t\t\t\t\t\teml.setContent(msg.getTextBody(),\"text\/plain; charset=UTF-8\");\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t}\r\n\t\t\t\t\teml.setSentDate(msg.getMessageDate().getTime());\r\n\t\t\t\t\teml.writeTo(fos);\r\n\t\t\t\t\tmsg.close();\r\n\t\t\t\t\tfos.close();\r\n\t\t\t\t\tSystem.out.println(\"[\"+file.getName() + \"] msg exchanged to eml\");\r\n\t\t\t\t\t\r\n\t\t\t\t} catch (IOException | MessagingException | ChunkNotFoundException e) {\r\n\t\t\t\t\tSystem.out.println(\"[\" +file.getName() + \"] msg export to eml is fail \");\r\n\t\t\t\t\te.printStackTrace();\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u6570\u5e74\u524d\u306bjavamail\u3067\u4f5c\u3063\u305f\u30e1\u30fc\u30eb\u3092\u53d7\u4fe1\u3057\u305f\u5f8c\u306e\u696d\u52d9\u3092\u81ea\u52d5\u5316\u3059\u308b\u30c4\u30fc\u30eb\u3092\u3001 &hellip; <a href=\"https:\/\/esoro.jp\/?p=1643\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1643"}],"collection":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1643"}],"version-history":[{"count":0,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1643\/revisions"}],"wp:attachment":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}