jesric1029 jesric1029 - 2 years ago 166
Java Question

Using Java Desktop mailto for two recipients

I am trying to add a feedback button to a Java program for work. I want this button to actually send an email to myself and one other person. All employees have the same default email application so using the Desktop mail method works fine.

I managed to get this working with 1 email addressee. It properly opens the email client, starts a new email and puts the addressee in the address line. The problem is when I try to add two email addresses.

int result = JOptionPane.showOptionDialog(null, panel, "Feedback", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, options1, null);

if(result == JOptionPane.NO_OPTION){
try {
Desktop.getDesktop().mail(new URI(""));
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (URISyntaxException e) {
// TODO Auto-generated catch block

So doing it like that works perfectly.

I've tried simply separating the addresses with a comma like this:

Desktop.getDesktop().mail(new URI("",""));

but this gives me an error and the only option is to actually remove the second argument.

Finally I've tried using a String[] like this:

String[] mailAddressTo = {"",""};

and then inserting that into the mailto method like this:

Desktop.getDesktop().mail(new URI("mailto:"+mailAddressTo));

but the email address comes out being

[Ljava.lang.String; @5e9394f7

once the email client is opened.

I've tried searching online and while I did find some solutions in regards to sending mail using Java through other methods than Desktop.mail - I found nothing related to how to accomplish this with Desktop.

If anyone can let me know how to make this work I would greatly appreciate it!

Answer Source

It helps to look at the documentation instead of guessing.

The list of URI constructors shows that there is no URI constructor which takes two Strings. That is why your first approach failed.

In Java, all arrays extend Object and inherit the default toString method of Object. Concatenating objects with + automatically invokes each object’s toString method, which is why your second approach yielded the results it did.

The official definition of the format of mailto: URLs is RFC 2368, which states that multiple recipients can be specified by separating with commas. So, you were on the right track.

As of Java 8, you can simply join your addresses with String.join:

String[] mailAddressTo = {"",""};
Desktop.getDesktop().mail(new URI("mailto:" + String.join(",", mailAddressTo)));

However, the documentation of the URI class states that the single-argument constructor assumes its String argument is already properly escaped. While it’s true that the example e-mail addresses you’ve provided don’t need to be escaped, it’s not safe to make that assumption with all possible addresses. To deal with this, you can use a multiple-argument URI constructor that will do the correct URI-escaping for you:

String[] mailAddressTo = { "", "" };
Desktop.getDesktop().mail(new URI("mailto", String.join(",", mailAddressTo), null));

If you’re using a version of Java older than 8, you can build the string yourself:

String[] mailAddressTo = { "", "" };

StringBuilder addressList = new StringBuilder();
String separator = "";
for (String address : mailAddressTo) {
    separator = ",";

Desktop.getDesktop().mail(new URI("mailto", addressList.toString(), null));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download