java 用附件启动邮件客户端?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6029579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:02:28  来源:igfitidea点击:

Start Mail-Client with Attachment?

javaemailemail-clientjdic

提问by schlingel

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.

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

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.

由于 RFC 的限制,java.awt.Desktop.mail-Method 不能处理附件。JDIC 项目已死,而 JMAPI 项目在构建过程中相当模糊。(需要 1.4 Mozilla-Sources)而且我必须自己为 64 位系统构建它。

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.

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

回答by Rajiv Makhijani

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.

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

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.

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

https://github.com/briandealwis/jmapi

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.*; ...

使用此代码,您似乎只需要构造一个消息对象并调用一个方法即可在邮件应用程序中启动它: 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.

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

回答by Gumboots

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.

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

回答by polerto

(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.

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