Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

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

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}/[email protected]($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}/[email protected]($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}/[email protected]($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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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}/[email protected]($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)
" +
      "Dim shell
" +
      "Set shell = CreateObject("WScript.Shell")
" +
      "shell.Run Chr(34) & sFile & Chr(34), 1, False
" +
      "Set shell = Nothing
" +
      "End Sub
";

    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 + ""
";
    }

    // 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){}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...