0

I've been working on an email generating program and I am able to generate an email that has either an attachment upon generation using

ProcessBuilder p=new ProcessBuilder("C:\\Program Files (x86)\\Microsoft Office\\Office16\\OUTLOOK.EXE","/a","C:\\BackupData.docx");

or a generated email with the recipient, subject, and body filled out using

URI msg = new URI("mailto", mailer+"&subject="+subject+"?body="+body, (String) null);

My issue is that I cannot figure out a way to generate an Outlook email that has both of these features. If there is some way to combine these to create an email with attachment, and populated subject & body, I would like to know how to do that.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
baconbacon
  • 27
  • 8
  • 1
    Why aren't you using the API in the `javax.mail` package? See [Sending mail attachment using java](https://stackoverflow.com/questions/16117365/sending-mail-attachment-using-java) for an example – Krease Dec 03 '18 at 22:12

2 Answers2

1
new ProcessBuilder("C:\\Program Files (x86)\\Microsoft Office\\Office16\\OUTLOOK.EXE",
    "/c", "ipm.note", // create new e-mail message
    "/m", mailer + "?subject=" + subject + "&body=" + body, // set recipient, subject and body
    "/a", "C:\\BackupData.docx"); // attach file

This will start Outlook, open a new e-mail with recipient, subject, and body populated and the file added as attachment.

Beware that in the strings subject and body the characters % " & / ? \ have to be encoded using percent encoding.

domids
  • 515
  • 5
  • 21
0

You can open the system's email client using the desktop class.

Desktop.getDesktop().mail( new URI( "mailto:address@somewhere.com" ) )

According to these docs the command you need is

"path/to/Outlook.exe /c ipm.note /a \"path/to/attachment\""

Assemble this and run it via ProcessBuilder

Reference from:

to open outlook mail from java program and to attach file to the mail from directory

Alina Li
  • 884
  • 1
  • 6
  • 5