Search code examples
javaemailoutlookmailto

Add attachment mailto Outlook


I want to send an email from my application with the following code:

public static void mailto(String subject,String path) throws IOException, URISyntaxException {
   String uriStr = String.format("cmd.exe /c start mailto:%s?subject=%s&attachment=%s",
            "test@test.fr",
            urlEncode(subject),
            urlEncode(path));
    Runtime.getRuntime().exec(uriStr);
}

private static final String urlEncode(String str) {
    try {
        return URLEncoder.encode(str, "UTF-8").replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

The window opens normally, everything works except the attachment.I tried many method to add an attachment to the email, however I did not find solution. It is imperative that I go through the Outlook interface for sent mail.

Result of the execution of the code.

Thanks for your help.


Solution

  • Try it out with a fixed path to your Outlook client executable.

    public static void mailto(String subject,String path) throws IOException {   
      Runtime.getRuntime().exec("C:\\Program Files (x86)\\Microsoft Office\\Office12\\outlook.exe /c ipm.note /m \"test@test.fr&subject="+subject+"\" /a \""+path+"\"");
    }