1

I was trying to send a file using mail but I wanted to first it open in a draft mail like mailto in jsp. I had implemented mail to functionality in Java but I am not able to attach file to opened mail. other are working file except the attachment. here is my code:

public static void mailto(List<String> recipients, String subject,
            String body) throws IOException, URISyntaxException {
            String uriStr = String.format("mailto:%s?subject=%s&body=%s",
            recipients,subject,body);
            Desktop.getDesktop().browse(new URI(uriStr));
            }

can any one suggest me how to attach file using mailto or any other api that can useful for me.enter code here

thanks in advance.

  • show what you already try (ex: code sample) – NeronLeVelu Feb 12 '15 at 07:12
  • I think we need more information about the context in which you're trying to do this. What kind of application are you writing? Where is it running? Desktop? Server backend? Server with web user interface? Attaching a file to a message created with JavaMail is pretty obvious so there must be something about what you're doing that makes you think it's difficult. – Bill Shannon Feb 12 '15 at 07:38

1 Answers1

1

Create Multipart MimeMessage using JavaMail but instead sending it call MimeMessage.saveChanges then use MimeMessage.writeTo to save it to the filesystem as '.eml'. Then open that file with java.awt.Desktop.open to launch the email client. You'll have to handle clean up after the email client is closed.

You also have to think about the security implications of email messages being left on the file system.

Community
  • 1
  • 1
jmehrens
  • 10,580
  • 1
  • 38
  • 47