2
votes

Can someone help me with opening Outlook Mail Client with To, CC, Subject and Body using Java code. Tried with the following code

import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;

 try {
      desktop.mail( new URI( "mailto:[email protected]?subject=Test%20message" ) );
     } 
catch ( IOException ex )
    {
    } 

But this code allows me to add only mailto: and Subject , But I need to enter mail address in CC and Want to compose the Body also. Please someone help me... Thanks in Advance.

2

2 Answers

2
votes
package org.bnymellon.home;
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class sendMail
{
    public static void main(String[] args) throws URISyntaxException
    {
        String subject="password";
        String body="See%20it";
        String cc="[email protected]";

        try {
            Desktop.getDesktop().mail( new URI( "mailto:[email protected]?subject="+subject+"&cc="+cc+"&body="+body) );
        } 
        catch ( IOException ex )
        {
        }
    }
}
0
votes

Another possible Solution using ProcessBuilder is:-

    try {
         new ProcessBuilder("Outlook.exe Path on Your System",
            "/m","Pass Parameters for mail like To, CC, BCC, Subject separated by &",
            "/a","mail attachment Path on Your System").start();
        } 
    catch ( Exception ex ) {
    }

For example :-

    try {
         new ProcessBuilder("C:\\Program Files (x86)\\Microsoft Office\\Office15\\OUTLOOK.EXE", 
           "/m","[email protected]&[email protected]&subject=testagain&body=testbody",
           "/a","D:\\sample.pdf").start();
        } 
    catch ( Exception ex ) {
    }