Start Mail-Client with Attachment?

浪子不回头ぞ 提交于 2019-11-26 06:46:01

问题


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.


回答1:


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("example@example.com");
        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.




回答2:


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




回答3:


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

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

should do the trick in a platform-independent way.



来源:https://stackoverflow.com/questions/6029579/start-mail-client-with-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!