Sending Email: Browser Version

Problem

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

Solution

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

Discussion

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 (see Chapter 18), 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. Permanently, but not symbiotically, since this alleged life-form offers nothing in return to its host.

<H1>Test</H1> <P>Here is how to <A HREF="mailto:spam
[email protected]?subject=Testing Mailto URL&[email protected]">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. MailtoButton.java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
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="www.darwinsys.com">
    <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="http://www.darwinsys.com/bin/feedback.cgi">
    <TEXTAREA NAME=message ROWS=5 COLS=60></TEXTAREA>
    <BR>
    <INPUT TYPE=SUBMIT VALUE="Send Feedback"></INPUT>
</FORM>
<P>Thank you for your comments.</P>

Of course, not everybody uses a full-featured browser, and the light version doesn’t include the email composer. The page therefore 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.

MailToButton

Figure 19-1. MailToButton

The CGI form is a workaround, though. Better yet is to provide a full-blown mail composer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.172.130