• 0

[JAVA] Email Link


Question

Basically, I'm trying to recreate the HTML "mailto:" behavior:

<a href= "mailto:test@test.com">Sendil</a> 

What I want is for the user to click a button or a link, and it opens their default mail program, starting a new e-mail, with the To field already filled out. Is this possible at all? Everything I've searched, the only results I've been given are how to make the program itself send the email, which isn't what I want.

Thanks for the help!

Link to comment
https://www.neowin.net/forum/topic/868488-java-email-link/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

It's possible to do that, if the platform's VM is supporting it. You will need a Desktop and an URI object.

I hope this piece of code will help:

package mail;

import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class MailTest
{
	public static void main(String[] args)
	{
		Desktop	desktop;

		if(Desktop.isDesktopSupported())
		{
			desktop	=	Desktop.getDesktop();
			if(desktop.isSupported(Desktop.Action.MAIL))
			{
				try
				{
					URI uri	= new URI("mailto","peter@pan.com?SUBJECT=Test Mail&BODY=Test Text&CC=some@one.com&BCC=pan@peter.com", null);
					desktop.mail(uri);
				}
				catch(IOException e)
				{
					e.printStackTrace();
				}
				catch(URISyntaxException e)
				{
					e.printStackTrace();
				}

			}
		}
	}
}

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.