在从Java前景打开邮件使用协议“MAPI://”
题
我developp使用Windows桌面搜索的Java应用程序从中我能找回我的计算机上的文件,如网址的一些信息(的 System.ItemUrl )。这样的URL的一个例子是
file://c:/users/ausername/documents/aninterestingfile.txt
有 “正常” 的文件。该字段也给从Outlook或Thunderbird索引邮件的URL。 Thunderbird的项目(仅可使用Vista和7件)也是文件(.wdseml)。但前景的项目网址开头为“MAPI://”,如:
mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto@mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가
我的问题是从Java中使用这个网址打开真正的项目在Outlook中。如果我复制/粘贴在Windows中的,它的工作原理在运行对话框;如果使用“启动”,然后在命令行复制/粘贴URL它也适用。
在URL似乎UTF-16进行编码。我希望能够写这样的代码:
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);
我不工作,我试着像其他的解决方案:
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");
没有任何成功。使用上述方案中,文件“test.bat的”包含正确的URL和“启动”命令,但是在公知的错误消息“test.bat的”结果的运行:
'■' is not recognized as an internal or external command, operable program or batch file.
大家有一个想法,可以打开 “MAPI://”?从Java项目
解决方案
好了,我的问题是有点棘手。但我终于找到了答案,并会在这里分享。
我怀疑是正确的:Windows使用UTF-16(小端)的URL。这是没有差异的UTF-8工作时,我们只用如图像,文字等的文件的路径,但要能够访问Outlook项目,我们必须使用UTF-16LE。如果我在C#编码,也不会有任何问题。但在Java中,你必须要更具创造性。
从Windows桌面搜索,我找回这样的:
mapi://{S-1-5-21-1626573300-1364474481-487586288-1001}/toto@mycompany.com($b423dcd5)/0/Inbox/가가가가곕갘객겒갨겑곓걌게겻겨곹곒갓곅갩갤가갠가
和我所做的是创建一个临时VB脚本,并像这样运行:
/**
* 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){}
}
不隶属于 StackOverflow