Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

java - How do I open the default mail program with a Subject and Body in a cross-platform way?

How do I open the default mail program with a Subject and Body in a cross-platform way?

Unfortunately, this is for a a client app written in Java, not a website.

I would like this to work in a cross-platform way (which means Windows and Mac, sorry Linux). I am happy to execute a VBScript in Windows, or AppleScript in OS X. But I have no idea what those scripts should contain. I would love to execute the user's default program vs. just searching for Outlook or whatever.

In OS X, I have tried executing the command:

open mailto:?subject=MySubject&body=TheBody

URL escaping is needed to replace spaces with %20.

Updated On Windows, you have to play all sorts of games to get start to run correctly. Here is the proper Java incantation:

class Win32 extends OS {
    public void email(String subject, String body) throws Exception {
        String cmd = "cmd.exe /c start "" "" + formatMailto(subject, body) + """;
        Runtime.getRuntime().exec(cmd);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In Java 1.6 you have a stardard way to open the default mailer of the platform: the Desktop.mail(URI) method.The URI can be used to set all the fields of the mail (sender, recipients, body, subject). You can check a full example of desktop integration in Java 1.6 on Using the Desktop API in Java SE 6


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...