有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用协议“mapi://”从java对outlook中打开的邮件进行编码

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

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

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

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

我遇到的问题是使用此url从Outlook中的Java打开真正的项目。如果我在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和“start”命令,但运行“test.bat”会导致众所周知的错误消息:

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

有人想到可以从Java打开“mapi://”项吗


共 (1) 个答案

  1. # 1 楼答案

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

    我的猜测是真的:Windows使用UTF-16(little endian)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){}
    }