Open default mail application in your machine using Java

We can open default mail app in  our machine using Java Desktop.
Use the following code for that.
1:  import java.awt.Desktop;  
2:  import java.io.IOException;  
3:  import java.net.URI;  
4:  import java.net.URISyntaxException;  
5:  public class Email {  
6:       /**  
7:        * @param args  
8:        */  
9:         public static void main(String[] args) {  
10:            //// TODO Auto-generated method stub  
11:            Desktop desktop = Desktop.getDesktop();  
12:            String url = "";  
13:            URI mailTo;  
14:            try {  
15:                 url = "mailTo:test@gmail.com" + "?subject=" + "TEST%20SUBJECT" 
16:                           + "&body=" + "TEST%20BODY";  
17:                 mailTo = new URI(url);  
18:                 desktop.mail(mailTo);  
19:            } catch (URISyntaxException e) {  
20:                 e.printStackTrace();  
21:            } catch (IOException e) {  
22:                 e.printStackTrace();  
23:            }  
24:       }  
25:  }  




While running this code, it will open your default mail client in your machine with the email id : "test@gmail.com" in To ,"TEST SUBJECT" in Subject & "TEST BODY" in Mail Body.
you can also attach a attachment to the mail using
the following lines:

1:  url = "mailTo:test@gmail.com" + "?subject=" + "TEST%20SUBJECT"   
2:               + "&body=" + "TEST%20BODY"+"&attachment=" + absoluteFilePath;   

where absoluteFilePath is the path for the attachment.
Note:
a. space will be denoted by %20.
b. New line will be denoted by %0A.
c. absolute file path start with "file:///".
d. The above for the attaching the file will work for all the mail clients except ThunderBird. Actually ThunderBird blocks attaching the files as an attachment. So, instead of this code we need to launch thunderbird in a different way.
1:  import java.awt.Desktop;  
2:  import java.io.IOException;  
3:  import java.net.URI;  
4:  import java.net.URISyntaxException;  
5:  public class Email {  
6:       /**  
7:        * @param args  
8:        */  
9:       public static void main(String[] args) {  
10:            // TODO Auto-generated method stub  
11:            Desktop desktop = Desktop.getDesktop();  
12:            String url = "";  
13:            URI mailTo;  
14:            try {  
15:                 String body = "TEST%20BODY";  
16:                 String command = "cmd /c start thunderbird.exe -compose subject=TEST%20SUBJECT,body="+
                                     ",attachment='"  
17:                      + absoluteFilePath + "'" ;  
18:                 Runtime.getRuntime().exec(command);  
19:            } catch (URISyntaxException e) {  
20:                 e.printStackTrace();  
21:            } catch (IOException e) {  
22:                 e.printStackTrace();  
23:            }  
24:       }  
25:  }  

The above code runs the thunderbird.exe from command prompt & opens thunderbird mail application with Subject, Body & Attachment.

Feel free to share & drop any comments!!

"Knowledge is true opinion." - Plato

2 comments :

  1. the attachments doesn't seem to be working on Windows 10.
    my final URI looks like this:
    Mail URL: mailto:test@test.com?subject=test&body=test&attachment=C:\Users\vipulba\AppData\Local\Temp\temp3689937100105073450.xls

    i also tried to put file:/// before my path which looks like below:
    Mail URL: mailto:test@test.com?subject=test&body=test&attachment=file:///C:\Users\vipulba\AppData\Local\Temp\temp3689937100105073450.xls

    Any idea what might be wrong.

    i have Outlook on my Windows.

    if i remove the &attachment from URI, it works well.

    ReplyDelete
  2. Were you able to figure out this problem? I am stuck on the same thing.

    ReplyDelete