Search code examples
javaemailattachmentmailto

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. I use the string "saveName" as the path of the file to be sent. 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.


Solution

  • You should use 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.

    This page has more comprehensive working examples.