启动带有附件的邮件客户端?

发布于 2024-11-07 17:26:10 字数 265 浏览 3 评论 0原文

我目前正在寻找一种方法(用Java)来启动带有定义的接收者、主题和正文以及预定义附件的默认邮件客户端。

由于 RFC 的限制,java.awt.Desktop.mail-Method 不适用于附件。 JDIC 项目已经消亡,而 JMAPI 项目在构建过程中也相当晦涩难懂。 (需要 1.4 Mozilla-Sources)而且我必须自己为 64 位系统构建它。

还有其他选择吗?我已经阅读了这里的文章,但使用 rundl32.dll 和此类“解决方案”并不是我想放入生产代码中的内容。

I'm currently searching for a way (in Java) to start the default mail client with defined receiver, subject and body and with a predefined attachment.

Due to the limitations of the RFC the java.awt.Desktop.mail-Method is not working with attachments. The JDIC-project is dead and the JMAPI-project is rather obscure in the building process. (Needs 1.4 Mozilla-Sources) And I have to build it for 64 bit systems myself.

Is there an alternative? I already read the articles here but using the rundl32.dll and such "solutions" aren't something I want to put in production code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森林很绿却致人迷途 2024-11-14 17:26:10

在 Java 中似乎没有任何与操作系统无关的方法来执行此操作,因为并非所有操作系统都提供标准方法来启动默认电子邮件应用程序,其中包含新电子邮件的基本字段。

在 Windows 上,可以使用 MAPI 的 JNI 接口,这将提供对在邮件应用程序中打开电子邮件的更多控制。正如您所提到的,JMAPI 就是这样的一个库 - 然而,似乎有许多具有类似用途的同名库。我发现了一个最近维护的并且看起来相当简单的。它包括一个预构建的二进制 dll 和一个附带的基于 Java JNI 的库。

https://github.com/briandealwis/jmapi

使用此代码,似乎您只需要构造消息对象并调用方法以在邮件应用程序中启动它:

导入 jmapi.*;
...

    if (JMAPI.isMapiSupported()) {
        Message msg = new Message();
        msg.setSubject("test!");
        msg.setBody("Hello world");

        List<String> toAddresses = new LinkedList<String>();
        toAddresses.add("[email protected]");
        msg.setToAddrs(toAddresses);

        List<String> attachPaths = new LinkedList<String>();
        //Must be absolute paths to file
        attachPaths.add("C:\Users\Documents\file.jpg");
        msg.setAttachments(attachPaths);

        JMAPI.open(msg);
    }

可能适用于 Windows 和 Mac(以及可能的其他操作系统)的另一种可能性是生成“.eml”或“.msg”文件,其中包含您希望包含的已编码为电子邮件一部分的内容和附件。然后可以使用相应电子邮件文件格式的默认处理程序启动该文件。但是,这并不能保证打开默认的电子邮件处理程序,文件格式也不能与每个电子邮件客户端兼容。

There does not appear to be any OS agnostic method of doing this in Java as not all OSes provide a standard way to launch the default e-mail application with more than the basic fields for a new email.

On Windows, it is possible to use a JNI interface to MAPI, which will provide more control over opening an email in a mail application. As you mentioned, one such library is JMAPI - however, it appears there are many libraries by such a name with similar purposes. I discovered one that is recently maintained and seems fairly straight-forward. It includes a pre-built binary dll and an accompanying Java JNI-based library.

https://github.com/briandealwis/jmapi

With this code, it seems you would only need to construct a message object and call a method to launch it in a mail application:

import jmapi.*;
...

    if (JMAPI.isMapiSupported()) {
        Message msg = new Message();
        msg.setSubject("test!");
        msg.setBody("Hello world");

        List<String> toAddresses = new LinkedList<String>();
        toAddresses.add("[email protected]");
        msg.setToAddrs(toAddresses);

        List<String> attachPaths = new LinkedList<String>();
        //Must be absolute paths to file
        attachPaths.add("C:\Users\Documents\file.jpg");
        msg.setAttachments(attachPaths);

        JMAPI.open(msg);
    }

Another possibility that might work for Windows and Mac (and potentially other OSes) is to generate a ".eml" or ".msg" file with the content and attachments you would like to include already encoded as part of the email. This file could then be launched with the default handler for the respective email file format. However, this is not guaranteed to open the default email handler, nor is the file format going to be compatible with everyone email client.

哽咽笑 2024-11-14 17:26:10

(据我所知)目前无法添加预定义附件,但您可以使用 java.awt.Desktop.mail 执行您提到的其他操作(使用定义的接收者、主题和正文启动默认邮件客户端)。我相信您已经检查过此处。不过这会很有用。

(as far as I know) That is currently not possible to add a predefined attachment but you can do other things that you mentioned (to start the default mail client with defined receiver, subject and body) using java.awt.Desktop.mail.. I believe that you have already checked here. It would be very useful though.

暖伴 2024-11-14 17:26:10

也许现在已经太晚了,但以防万一有人仍然发现这个问题:

Desktop.getDesktop().mail(new URI("mailto:[email protected]?subject=attachment_example&body=see_attached_file&attachment=/path/to/attachment"));

应该以独立于平台的方式来解决这个问题。

Probably it's too late by now, but just in case anybody still finds this problem:

Desktop.getDesktop().mail(new URI("mailto:[email protected]?subject=attachment_example&body=see_attached_file&attachment=/path/to/attachment"));

should do the trick in a platform-independent way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文