开发者

Open mails in outlook from java using the protocol "mapi://"

开发者 https://www.devze.com 2022-12-22 01:19 出处:网络
I developp a Java application using Windows Desktop Search from which I can retrieve some information about files on my computer such as urls (System.ItemUrl). An example of such url is

I developp a Java application using Windows Desktop Search from which I can retrieve some information about files on my computer such as urls (System.ItemUrl). An example of such url is

file://c:/users/ausername/documents/aninterestingfile.txt

for "normal" files. This field give also urls of mail items indexed from Outlook or Thunderbird. Thunderbird's items (only available using vista and seven) are also files (.wdseml). But outlook's items urls start with "mapi://" like :

mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/t开发者_Go百科oto@mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가

The problem I have is opening the real item from Java in Outlook using this url. If I copy/paste it in the run dialog of Windows, it works ; it also works if I use "start" followed by the copied/pasted url in command line.

The url seems to be encoded in UTF-16. I want to be able to write such code :

String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto@mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가";

Runtime.getRuntime().exec("cmd.exe /C start " + url);

I doesn't work and I've tried other solutions like :

String start = "start";
String url = "mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto@mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가";

FileOutputStream fos = new FileOutputStream(new File("test.bat");
fos.write(start.getBytes("UTF16");
fos.write(url.getBytes("UTF16"));
fos.close();

Runtime.getRuntime().exec("cmd.exe /C test.bat");

without any success. Using the solution above, the file "test.bat" contains the correct url and the "start" command, but the run of "test.bat" results in the well known error message :

'■' is not recognized as an internal or external command, operable program or batch file.

Has anybody an idea to be able to open "mapi://" items from Java ?


Well, my question was a little bit tricky. But I finally found an answer and will share it here.

What I suspected was true : Windows uses UTF-16 (little endian) urls. It makes no differences working in UTF-8 when we only use paths to files such as images, text, etc. But to be able to access Outlook items, we must use UTF-16LE. If I were coding in C#, there wouldn't have been any problem. But in Java, you have to be more inventive.

From Windows Desktop Search, I retrieve this:

mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto@mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가

And what I did is creating a temporary VB script and run it like this:

/**
 * Opens a set of items using the given set of paths.
 */
public static void openItems(List<String> urls) {
  try {

    // Create VB script
    String script =
      "Sub Run(ByVal sFile)\n" +
      "Dim shell\n" +
      "Set shell = CreateObject(\"WScript.Shell\")\n" +
      "shell.Run Chr(34) & sFile & Chr(34), 1, False\n" +
      "Set shell = Nothing\n" +
      "End Sub\n";

    File file = new File("openitems.vbs");

    // Format all urls before writing and add a line for each given url
    String urlsString = "";
    for (String url : urls) {
      if (url.startsWith("file:")) {
        url = url.substring(5);
      }
      urlsString += "Run \"" + url + "\"\n";
    }

    // Write UTF-16LE bytes in openitems.vbs
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(script.getBytes("UTF-16LE"));
    fos.write(urlsString.getBytes("UTF-16LE"));
    fos.close();

    // Run vbs file
    Runtime.getRuntime().exec("cmd.exe /C openitems.vbs");

  } catch(Exception e){}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号