mapi java,使用协议“mapi://”从java打开outlook中的邮件

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值