【问题标题】:Open mails in outlook from java using the protocol "mapi://"使用协议“mapi://”从 java 在 Outlook 中打开邮件
【发布时间】:2010-03-08 09:09:28
【问题描述】:

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

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

我遇到的问题是使用此 url 在 Outlook 中从 Java 中打开真实项目。如果我在 Windows 的运行对话框中复制/粘贴它,它可以工作;如果我在命令行中使用“start”后跟复制/粘贴的 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://”项目?

【问题讨论】:

    标签: java encoding utf-16 wds


    【解决方案1】:

    嗯,我的问题有点棘手。不过我终于找到了答案,在这里分享一下。

    我怀疑是真的:Windows 使用 UTF-16(小端)网址。当我们只使用图像、文本等文件的路径时,在 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){}
    }
    

    【讨论】:

    • 多绕路啊!为什么是 exec(cmd)? Java 有没有办法调用 COM 对象?然后你应该可以导入WScript.Shell(全名Windows Script Host Object Model)的类型库,直接调用Run。
    猜你喜欢
    • 2011-04-26
    • 1970-01-01
    • 2011-08-27
    • 2018-01-11
    • 2011-07-01
    • 2023-01-16
    • 1970-01-01
    • 2013-01-30
    • 2013-10-08
    相关资源
    最近更新 更多