使用协议“mapi://”从java打开outlook中的邮件 [英] Open mails in outlook from java using the protocol "mapi://"

查看:333
本文介绍了使用协议“mapi://”从java打开outlook中的邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Windows桌面搜索开发Java应用程序,从中可以检索有关计算机上文件的一些信息,例如urls( System.ItemUrl )。这样的URL的一个例子是

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

为正常文件。此字段还提供从Outlook或Thunderbird索引的邮件项目的URL。 Thunderbird的项目(仅适用于vista和7)也是文件(.wdseml)。但是outlook的项目URL以mapi://开头,如:

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

我遇到的问题是使用这个网址在Outlook中打开真实的项目。如果我将它复制/粘贴到Windows的运行对话框中,它可以工作;如果我在命令行中使用start,然后是复制/粘贴的url,那么它也可以工作。



这个URL似乎是以UTF-16编码的。我想要写这样的代码:

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

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 /收件箱/가가가가곕갘겒겒갨겑 곓걌게겻겨곹곒갓곅갩갤가갠가;

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和start 命令,但运行test.bat会导致众所周知的错误消息:

 '■'未被识别作为内部或外部命令,可操作的程序或批处理文件

有人有一个想法能够打开mapi://项目从Java?

解决方案

嗯,我的问题有点棘手,但是我终于找到了一个答案,并将在这里分享。



我怀疑是真的:Windows使用UTF-16(小端)url,在UTF-8中没有区别,我们只使用诸如imag的文件路径es,文本等。但是为了能够访问Outlook项目,我们必须使用UTF-16LE。如果我在C#编码,就不会有任何问题。但是在Java中,你必须更有创意。



从Windows桌面搜索,我检索到:

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

我所做的是创建一个临时VB脚本并运行它像这样:

  / ** 
*使用给定的路径打开一组项目。
* /
public static void openItems(List< String> urls){
try {

//创建VB脚本
String script =
Sub Run(ByVal sFile)\\\
+
Dim shell\\\
+
Set shell = CreateObject(\WScript.Shell\)\\\
+
shell.Run Chr(34)& sFile& Chr(34),1,False\\\
+
设置shell = Nothing\\\
+
结束子\\

文件文件=新文件(openitems.vbs);

//编写前格式化所有的URL,并为每个给定的URL添加一行
String urlsString =;
(String url:urls){
if(url.startsWith(file:)){
url = url.substring(5);
}
urlsString + =运行\+ url +\\\\
;
}

//在openitems.vbs中写入UTF-16LE字节
FileOutputStream fos = new FileOutputStream(file);
fos.write(script.getBytes(UTF-16LE));
fos.write(urlsString.getBytes(UTF-16LE));
fos.close();

//运行vbs文件
Runtime.getRuntime()。exec(cmd.exe / C openitems.vbs);

} catch(Exception e){}
}


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

这篇关于使用协议“mapi://”从java打开outlook中的邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆