0

When I run the following code, the client mail service opens up with the To:, Subject:, Body: except the attachment which is highly needed. I couldn't figure out why the attachment is not adding into the mail.

public static void main(String[] args) throws FileNotFoundException, IOException {
      
     final String HTML = "<h1>Hello</h1>"
        + "<p>This was created using iText</p>"
        + "<a href='hmkcode.com'>hmkcode.com</a>";

    String filePath = "C:/Users/HP/Desktop/pdfTest/a.txt";
    HtmlConverter.convertToPdf(HTML, new FileOutputStream(filePath, true));

    Desktop desktop = Desktop.getDesktop();

    try {
        // Open user-default mai
        // l client application.
     
        String message = "mailto:username@domain.com?subject=New_Profile&body=see%20body%20content&attachment=" + filePath;
        URI uri = URI.create(message);
        desktop.mail(uri);
    } catch (IOException e) {
        e.printStackTrace();
    }

    SpringApplication.run(DemoApplication.class, args);

}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Mohamed
  • 31
  • 1
  • 6
  • Why are you calling `desktop.mail();` twice? Shouldn't you just do `desktop.mail(uri);`? – Gregg Jan 29 '21 at 16:29
  • sorry i removed it.. but still the same.. its not getting attached – Mohamed Jan 29 '21 at 16:32
  • Can you get the right behavior from your browser by pasting the URI you are generating into its address bar? Do you know that the "mailto" URI spec and the target email client both support the specification of `attachment` in the way you are attempting? – CryptoFool Jan 29 '21 at 17:10
  • isnt there is way to sent the attchment then. – Mohamed Jan 29 '21 at 17:24
  • thank you, isnt there is any other way to do. i wanna open clients default mail service with an attachment.. – – Mohamed Jan 31 '21 at 16:29

1 Answers1

1

According to using mailto to send email with an attachment there is no support for attachments in mailto uri. And I, too, think that this is quite a logical behavior, for file access being extremly dangerous in a security point of view.

  • thank you, isnt there is any other way to do. i wanna open clients default mail service with an attachment.. – Mohamed Jan 31 '21 at 16:29