Solved

How to emulate "mailto:" in Java?

Posted on 2003-12-03
17
5,657 Views
Last Modified: 2012-05-04
Can anyone send me a code snippet showing how to call up the mail program on the client machine?

What I need is code that will do the same thing as the HTML statement:
<a href="mailto:somemailaddress@somedomain.com">

This is for a stand-alone Java application. Hopefully, the same code can be used on a server in a client/server environment.
0
Comment
Question by:snazzyrags
  • 9
  • 6
  • 2
17 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9867581
You could put this HTML into a JLabel.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9867878
I think the best you could do is pop up a Java window for sending a mail. The functionality you see normally requires OS-level integration.
0
 

Author Comment

by:snazzyrags
ID: 9868049
jimmack,

This is what I did:

JButton emailButton;
...
emailButton = new JButton("<HTML><BODY><a href=\"mailto:dthomas@neo.rr.com\">Send Email</a></BODY></HTML>");

The button displayed as a hyperlink, as expected. However, when the button was pressed, nothing happened. Is there more?

Thanks for your quick response.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9868168
>>Is there more?

There certainly is ;-) See my comment above. Obviously, you can make the button open a program, but not necessarily with the address in the text field AFAIK.

Runtime.getRuntime().exec(yourMailProgram)
0
 

Author Comment

by:snazzyrags
ID: 9868389
CEHJ,

To use your solution, I need to know the email program on the client's machine. Now I'm really getting over my head here, but I'm sure there's a method for doing this - perhaps through the client's registry?

Secondly, I should have mentioned this earlier (sorry), but my program dynamically builds the recipient list, so I need to pass this list (String class) to the email program, as well. The subject, text and attachments are the responsibility of the user, once they are presented with their email application.

I haven't discarded your original solution, CEHJ, just exploring simpler solutions first.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9868417
You're going to need some low-level integration between Java and OS/mail program API. This, if you're interested in platform independence, would also have to be done for each OS.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9869200
My comment referred to a JLabel, not a JButton.  I don't think what you've done would have the same effect, the button callbacks would probably take priority over the HTML content.  Try changing the JButton to a JLabel:

JLable emailLabel;
...
emailLabel = new JButton("<HTML><BODY><a href=\"mailto:dthomas@neo.rr.com\">Send Email</a></BODY></HTML>");

However, as CEHJ said, this will then rely on the OS to manage the opening of the browser.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9869270
By default, even in a JEditorPane, absolutely nothing happens when you click on a hyperlink in Java. You have to make something happen. The question is what?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:snazzyrags
ID: 9869516
OK guys, have at it and let me know the answer when the dust settles!

BTW, a couple years ago I had a similar requirement except it was on the server side. So I just used the exec() method of Runtime and called a Perl program with the required parameters and let it send the email via sendmail. That was done quite easily. This new requirement seems much more difficult because I'm trying to use a Windows email system on the client platform - MS-Outlook Express, in particular.

Maybe I should investigate some Perl possibilities...
0
 

Author Comment

by:snazzyrags
ID: 9871036
Eureka! I received the solution on the MS-Office/Outlook expert's list. We were close, but not close enough.

FYI, here's the solution:

Runtime.getRuntime().exec("perl email.pl ...<command line parameters>...");

This is the same program I used a couple years ago with the following very important modification:

system("start mailto:<recipient list>");

For some reason, this doesn't work (yet!). I may investigate later - it would be nice to not have to call the perl program:

Runtime.getRuntime().exec("start mailto:<recipient list>");     // Doesn't work! (why?!?!)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9871063
Try:

Runtime.getRuntime().exec("start mailto:aa@bb.com;cc@dd.com");    

Works for me, but you'll need > Win 98
0
 

Author Comment

by:snazzyrags
ID: 9871632
I'm running W2K - Pro. I get the following IOException:

"CreateProcess: start mailto:dthomas@neo.rr.com error=2"

Is this the same old DOS IO error 2 from days of yore (NOENT)? If so, what's missing? I'm not familiar with 'mailto:' - whatever it is. I didn't know it had any relevence outside HTML.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9873803
Sorry

Runtime.getRuntime().exec("cmd.exe /C start mailto:dthomas@neo.rr.com");    
0
 

Author Comment

by:snazzyrags
ID: 9874469
CEHJ,

Abba - Dabba - Doooo! It works!!

Although I've already paid the points for the previous solution, I'll give them to you, as well, for this much more elegant solution.

Do you mind explaining what is going on here? I know cmd.exe opens a new environment (shell in Unix terms) and I've looked at the DOS help on 'start', although that explains how to use it with no background info on what it is doing or why it's required. Most importantly, what the heck is 'mailto:'? It's not an internal or external DOS command and it doesn't seem to be an executable file.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9874482
Shall come back on that later - got to go out now ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9874493
Or - to give you a quick one:

1. start 'knows' how to start a prog, probably based on registry settings
2. mailto: is a url (in RFCs no doubt)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9874499
>>2. mailto: is a url (in RFCs no doubt)

or, more precisely, the protocol part of a url
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
base64 decode encode 12 119
Java JRE greater than 1.6 5 50
stackato and cloud 4 69
Opening PDF on button click and fill new document 2 16
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now