Open mail in Outlook from java using "mapi: //" protocol

I am developing a Java application using Windows Desktop Search, from which I can get some information about the files on my computer such as URL ( System.ItemUrl ). An example of such a URL is

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

      

for "normal" files. This field also lists the URLs of mail items indexed from Outlook or Thunderbird. Thunderbird items (only available using vista and seven) are also files (.wdseml). But the prediction point URLs start with "mapi: //" like:

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

      

I have a problem opening a real object from Java in Outlook using this url. If I copy / paste it into the windows start dialog it works; it also works if I use "start" and then the copied / pasted url on the command line.

The url appears to be UTF-16 encoded. I want to write code like this:

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 am not working and I have 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 test.bat file contains the correct url and the "start" command, but running "test.bat" results in the known error message:

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

      

Does anyone have an idea to open mapi: // objects from Java?

+1


source to share


1 answer


Well my question was a little tricky. But I finally found the answer and shared it here.

What I suspected was true: Windows uses UTF-16 (low-end) URLs. There is no difference in UTF-8 when we only use file paths like images, text, etc. But in order to be able to access Outlook items, we must use UTF-16LE. If I was coding in C # there would be no problem. But in Java, you have to be more creative.

On Windows Desktop Search, I get this:



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

      

And I created 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){}
}

      

+1


source







All Articles