DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. A Simple Way of Sending Emails in Java: mailto Links

A Simple Way of Sending Emails in Java: mailto Links

Axel Rauschmayer user avatar by
Axel Rauschmayer
·
Dec. 07, 10 · Interview
Like (0)
Save
Tweet
Share
26.81K Views

Join the DZone community and get the full member experience.

Join For Free

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. If you want to do this in Python then this blog post shows you how.

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

  • recipients: comma-separated email addresses
  • value: should be URL-encoded (e.g. space becomes %20)
  • key: subject, cc, bcc, body
  • Example: mailto:joe@example.com?subject=hello&body=How%20are%20you%3F

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");
}

From http://2ality.blogspot.com/2010/12/simple-way-of-sending-emails-in-java.html

Java (programming language) Links

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How To Handle Secrets in Docker
  • Fargate vs. Lambda: The Battle of the Future
  • HTTP vs Messaging for Microservices Communications
  • Cloud Performance Engineering

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: