2010-12-06

A simple way of sending emails in Java: mailto links

Whenever I send automated emails, I prefer to have one last look at them, before sending them off. The following method allows one to do this, as all of the generated emails are opened in your default email program, complete with recipients, subject, and content. Thankfully, this is easy to do, by just sending a properly encoded URL to the operating system. Doing this depends on java.awt.Desktop and thus Java 6.

mailto: URL syntax

"mailto:" recipients ( "?" key "=" value ("&" key "=" value)* )?

Java source code

public static void mailto(List<String> recipients, String subject,
        String body) throws IOException, URISyntaxException {
    String uriStr = String.format("mailto:%s?subject=%s&body=%s",
            join(",", recipients), // use semicolon ";" for Outlook!
            urlEncode(subject),
            urlEncode(body));
    Desktop.getDesktop().browse(new URI(uriStr));
}

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

public static final String join(String sep, Iterable<?> objs) {
    StringBuilder sb = new StringBuilder();
    for(Object obj : objs) {
        if (sb.length() > 0) sb.append(sep);
        sb.append(obj);
    }
    return sb.toString();
}

public static void main(String[] args) throws IOException, URISyntaxException {
    mailto(Arrays.asList("john@example.com", "jane@example.com"), "Hello!",
            "This is\nan automatically sent email!\n");
}
Related post:
  1. Generate emails with mailto URLs and Python

8 comments:

Mail2mesatya said...

hi,

does this work on  a remote machine which does not have this code but is connecting to the application through a webserver address? 

Axel Rauschmayer said...

I don’t fully understand your setting, but “remote machine” sounds tricky. All the above does is create a new message in you mail application and fill in the content (as in “type it for you”). You still have to push the send button to send the email. It’s a GUI thing which is why there are many remote settings where this will not work.

satya said...

 I tried this code and it works totally fine on my system.  when i connect to the webserver on my system from any other system, launch the app and hit submit, it opens outlook on my system rather than on the remote system where app was opened through web server.  Is this b'coz its using Desktop of the host? Is there any way i can lauch the mail application of any system which connects through webserver?

Axel Rauschmayer said...

Yes, if you execute the script on the server. But then you need to remote-control the server GUI (of the email app) for actually sending the email.

satya said...

Well, I do not want the email program of server to open up. It shld do that on the client machine.  looks like this code is not for that. Thanks for all  your help Axel.

amlsoft said...

Hi,
Is there a way the body of the e-mail actually shows the HTML tags and does formatted?

Ex.
URI uri = new URI("mailto","myemail@email.com&SUBJECT=Test Subject&BODY=Test body line 1",null);
desktop.mail(uri);

Axel Rauschmayer said...

Good question. Don’t know.

amlsoft said...

I tried body as Test body line #1

But couldnt formatted body as I intended. :(

Web Analytics