Sending Email Using the Desktop API in Java SE 6

Started by hari, Apr 27, 2008, 05:47 PM

Previous topic - Next topic

hari

Sending Email

Applications can launch the host's default email client, if that action is supported, by calling this Desktop instance method:

    public void mail(URI uri) throws IOException



DesktopDemo has an ActionListener for the Launch Mail button. In this case, the listener invokes the following method:

    private void onLaunchMail(java.awt.event.ActionEvent evt) {
        String mailTo = txtMailTo.getText();
        URI uriMailTo = null;
        try {
            if (mailTo.length() > 0) {
                uriMailTo = new URI("mailto", mailTo, null);
                desktop.mail(uriMailTo);
            } else {
                desktop.mail();
            }
        }
        catch(IOException ioe) {
            ioe.printStackTrace();
        }
        catch(URISyntaxException use) {
            use.printStackTrace();
        }
        ...
    }



The onLaunchMail() method retrieves the email recipient from the associated text field, creates the URI with a mailto scheme argument if a recipient exists, and then invokes the mail() method. The mail() method is overloaded, so you can call it with or without a URI that represents its mailto recipient



You can use more than just a single email recipient when creating this URI. The mailto scheme supports CC, BCC, SUBJECT, and BODY fields as well. For example, the following text could be used to create a mailto URI:

    mailto:duke@sun.com?SUBJECT=Happy New Year!&BODY=Happy New Year, Duke!



You can, of course, invoke mail() without an argument. In this case, your email client will launch a new email window without specifying a recipient, subject, or body message.
Opening, Editing, and Printing a File

Java applications can open, edit, and print files from their associated application using a Desktop object's open(), edit(), and print() methods. Again, DesktopDemo allows these actions only if the Desktop instance supports them, so in this application scenario, it is not necessary to check for support again.



Each of DesktopDemo's radio buttons has its own ActionListener as well. In this case, each sets an instance variable so that it represents the most recently selected button's associated Desktop.Action:

    Desktop.Action action;

    private void onPrintAction(java.awt.event.ActionEvent evt) {
        action = Desktop.Action.PRINT;
    }                             

    private void onEditAction(java.awt.event.ActionEvent evt) {
        action = Desktop.Action.EDIT;
    }                             

    private void onOpenAction(java.awt.event.ActionEvent evt) {
        action = Desktop.Action.OPEN;
    }



When you press the Launch Default Application button, it invokes its own listener, which calls the following method:

    private void onLaunchDefaultApplication(java.awt.event.ActionEvent evt) {
        String fileName = txtFile.getText();
        File file = new File(fileName);

        try {
            switch(action) {
                case OPEN:
                    desktop.open(file);
                    break;
                case EDIT:
                    desktop.edit(file);
                    break;
                case PRINT:
                    desktop.print(file);
                    break;
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
        ...
    }



This method determines which Desktop.Action is selected and invokes the appropriate Desktop instance method, either open(), edit(), or print(). Each method requires a File argument, which will be used to perform the requested action.

Interestingly, different applications may be registered for these different actions even on the same file type. For example, the Firefox browser may be launched for the OPEN action, Emacs for the EDIT action, and yet a different application for the PRINT action. Your host desktop's associations are used to determine what application should be invoked. The ability to manipulate desktop file associations is not possible with the existing Desktop API in JDK 6, and those associations can be created or changed only with platform-dependent tools at this time.

Thanks and Regards,
Hari
ITAcumens
(www.itacumens.com)

http://itacumens.com/images/homepage/ad_hari.swf