简体   繁体   English

如何在Java中向mailto添加附件

[英]How to add an attachment to mailto in Java

I am creating a text editor and I would like to add a share feature that would allow you to email your document. 我正在创建一个文本编辑器,我想添加一个共享功能,使您可以通过电子邮件发送文档。 I need some help in finding a way to use the mailto with a variable. 我需要一些帮助,以找到一种将mailto与变量结合使用的方法。 I use the string "saveName" as the path of the file to be sent. 我使用字符串“ saveName”作为要发送的文件的路径。 Here is what I have right now: 这是我现在所拥有的:

share.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

        Desktop desktop = Desktop.getDesktop();
        String mailData = "mailto:?subject=Document&attachment=" + saveName;
        System.out.println(mailData);
        try {
            desktop.mail(new URI(mailData));
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }

        }
    });

Can some one shed some light on how to do this? 有人可以阐明如何做到这一点吗? Help is appreciated, thanks. 感谢您的帮助,谢谢。

You should use JavaMail API. 您应该使用JavaMail API。 It will allow you to connect to a server and the send and receive mails. 它将允许您连接到服务器以及发送和接收邮件。

Here is an example of JavaMail API with Attachments . 这是带有附件的JavaMail API的示例。

This page has more comprehensive working examples . 此页面具有更全面的工作示例

You are trying to use the Desktop class to launch he user's preferred mail client so they can send an email, but you want to have already attached a file. 您正在尝试使用Desktop类启动用户的首选邮件客户端,以便他们可以发送电子邮件,但您希望已经附加了文件。 A mailto: link may include (the start of) a body, and email headers including to , cc , subject and others... mailto:链接可能包括正文(的开头),以及包括toccsubject和其他内容的电子邮件标题

Unfortunately, the mailto: URL type does not support attachments. 遗憾的是, mailto: URL类型支持附件。 Attachments are not headers and are not part of the body; 附件不是标题,也不是主体的一部分; they are a separate part of a multi-part mime message 它们是多部分mime消息的单独部分

The Desktop class javadoc mentions some of the fields available to mail(URI) , but has no mention of support for attachments. Desktop类 javadoc提及了mail(URI)可用的某些字段,但没有提及对附件的支持。

I understand the desire to use the user's already-configured mail client, but to send an attachment you will have to handle the mail yourself and build the message out of Part s 我了解使用用户已经配置的邮件客户端的愿望,但是要发送附件,您将必须自己处理邮件并在Part s之外构建邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM