Recipe 19.1 Sending Email: Browser Version


You want an applet to permit the user to compose and send email.


Use a mailto: URL, but hide it in some Java code.


Since most web browsers are now configured with either built-in or linked-in email clients, you can use the mailto: URL as a poor-person's email composer to have users contact you. Many people prefer this to a fill-in-the-blank "mail" form connected to a CGI script or servlet since they can use a specialized tool and save their own copy of the mail either in their log file or by CC'ing their own account. While you could use a mailto: URL directly in HTML, experience suggests that a species of parasite called a spam perpetrator will attach itself permanently to your mailbox if you do.

<H1>Test</H1> <P>Here is how to <A HREF="mailto:spam-magnet@darwinsys. com?subject=Testing Mailto URL&cc=dilbert@office.comics">contact us</A>

My approach is to hide the mailto: URL inside a Java applet, where spam perps are less likely to notice it. The applet uses showDocument( ) to activate the mailto: URL:

String theURL = "mailto:" + username; URL targetURL = new URL(theURL); getAppletContext.showDocument(targetURL);

Further, I break the email address into two parts and provide the @ directly, so it won't be seen even if the spam-spider is clever enough to look into the param parts of the applet tag. Since I know you won't actually deploy this code without changing Target1 and Target2 the param tags for the mail receiver's email name and host domain you're fairly safe from spam with this. Example 19-1 is the Java applet class.

Example 19-1.
import java.applet.*; import java.awt.*; import java.awt.event.*; import*; import java.util.*; /**   * MailtoButton -- look like a mailto, but not visible to spiders.  */ public class MailtoButton extends Applet {     /** The label that is to appear in the button */     protected String label = null;     /** The width and height */     protected int width, height;     /** The string form of the URL to jump to */     protected String targetName, targetHost;     /** The URL to jump to when the button is pushed. */     protected URL targetURL;     /** The name of the font */     protected String fontName;     protected String DEFAULTFONTNAME = "helvetica";     /** The font */     protected Font theFont;     /** The size of the font */     protected int fontSize = 18;     /** The HTML PARAM for the user account -- keep it short */     private String TARGET1 = "U";    // for User      /** The HTML PARAM for the hostname -- keep it short */     private String TARGET2 = "H";    // for Host      // Dummy     private String BOGON1 = "username";    // happy strings-ing, spam perps     private String BOGON2 = "hostname";    // ditto.     /** The string for the Subject line, if any */     private String subject;     /** Called from the browser to set up. We want to throw various      * kinds of exceptions but the API predefines that we don't, so we      * limit ourselves to the ubiquitous IllegalArgumentException.      */     public void init( ) {         // System.out.println("In LinkButton::init");         try {             if ((targetName = getParameter(TARGET1)) == null)                 throw new IllegalArgumentException(                     "TARGET parameter REQUIRED");             if ((targetHost = getParameter(TARGET2)) == null)                 throw new IllegalArgumentException(                     "TARGET parameter REQUIRED");             String theURL = "mailto:" + targetName + "@" + targetHost;             subject = getParameter("subject");             if (subject != null)                 theURL += "?subject=" + subject;             targetURL = new URL(theURL);         } catch (MalformedURLException rsi) {             throw new IllegalArgumentException("MalformedURLException " +                 rsi.getMessage( ));         }         label = getParameter("label");    // i.e., "Send feedback"         if (label == null)                 throw new IllegalArgumentException("LABEL is REQUIRED");         // Now handle font stuff.         fontName = getParameter("font");         if (fontName == null)             fontName = DEFAULTFONTNAME;         String s;         if ((s = getParameter("fontsize")) != null)             fontSize = Integer.parseInt(s);         if (fontName != null || fontSize != 0) {             System.out.println("Name " + fontName + ", size " + fontSize);             theFont = new Font(fontName, Font.BOLD, fontSize);         }                  Button b = new Button(label);         b.addActionListener(new ActionListener( ) {             public void actionPerformed(ActionEvent e) {                 if (targetURL != null) {                     // showStatus("Going to " + target);                     getAppletContext( ).showDocument(targetURL);                 }             }         });         if (theFont != null)             b.setFont(theFont);         add(b);     }          /** Give Parameter info to the AppletViewer, just for those      * writing HTML without hardcopy documentation :-)      */     public String[][] getParameterInfo( ) {         String info[][] = {             { "label",        "string",    "Text to display" },             { "fontname",    "name",        "Font to display it in" },             { "fontsize",    "10-30?",    "Size to display it at" },             // WARNING - these intentionally lie, to mislead spammers who             // are incautious enough to download and run (or strings) the             // .class file for this Applet.             { "username",    "email-account",                 "Where do you want your mail to go today? Part 1" },             { "hostname",    "host.domain",                 "Where do you want your mail to go today? Part 2" },             { "subject",    "subject line",                 "What your Subject: field will be." },         };         return info;     } }

Example 19-2 shows the program in a simple HTML page to show you the syntax of using it.

Example 19-2. MailtoButton.htm
<HTML><HEAD> <TITLE>Darwin Open Systems: Feedback Page</TITLE></HEAD> <BODY BGCOLOR="White"> <H1>Darwin Open Systems: Feedback Page</H1> <P>So, please, send us your feedback!</P> <APPLET CODE=MailtoButton WIDTH=200 HEIGHT=40>     <PARAM NAME="H" VALUE="">     <PARAM NAME="U" VALUE="wile_e_coyote">     <PARAM NAME="subject" VALUE="Acme Widgets Feedback">     <PARAM NAME="label" VALUE="Send Feedback by Mail">     <PARAM NAME="font" VALUE="Helvetica">     <PARAM NAME="fontsize" VALUE="16">     <P>Your browser doesn't recognize Java Applets.       Please use the non-Java CGI-based feedback form.</P> </APPLET> <P>You should get an acknowledgement by email shortly. Thank you for your comments!</P> <HR> <P>Here is a traditional "CGI"-style form to let you to send feedback if you aren't running Java or if your browser doesn't support email composition.</P> <FORM METHOD=POST ACTION="">     <TEXTAREA NAME=message ROWS=5 COLS=60></TEXTAREA>     <BR>     <INPUT TYPE=SUBMIT VALUE="Send Feedback"></INPUT> </FORM> <P>Thank you for your comments.</P>

Example 19-2 requires JavaMail API Version 1.2 or later due to a limitation in earlier versions.

Of course, not everybody uses a full-featured browser, and the light version doesn't include the email composer. Therefore, the page features a traditional CGI-based form for the benefit of those poor souls in need of a Java-based browser. Figure 19-1 is a screenshot in Netscape 4, showing the Compose window resulting from pressing the Feedback button.

Figure 19-1. MailtoButton

The CGI form is a workaround, so it's better to provide a full-blown mail composer.

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: