package sample; import jaxcent.*; public class Email extends jaxcent.JaxcentPage { // To send Email, we require the user to enter a "captcha" number generated randomly. // // We could also use (or build on the fly, for that matter) a random captcha image, // but everybody is doing that! Being different means the spammers don't have incentive to // focus on your method. With Jaxcent, there are many ways to do this. E.g. you could // alter the text of a para and ask the user to input "the bold phrase from the para above" // or "the italicized word in line 1" etc... int captchaNumber = 0; HtmlButton sendMail = new HtmlButton( this, "SendMail" ) { protected void onClick( java.util.Map data ) { sendMail( data ); } }; HtmlButton getCode = new HtmlButton( this, "GetCode" ) { protected void onClick() { showCode(); } }; HtmlDiv mainDiv = new HtmlDiv( this, "mainDiv" ); HtmlDiv resultsDiv = new HtmlDiv( this, "resultsDiv" ); void showCode() { // Make a new message code. captchaNumber = 1001 + new java.util.Random().nextInt( 8999 ); try { showInputDialog( "The message code is " + captchaNumber + "\nYou can copy it from the box below", String.valueOf( captchaNumber )); } catch (Jaxception jax) { jax.printStackTrace(); } } void sendMail( java.util.Map data ) { try { String from = (String) data.get( "username" ); if ( from.equals( "" )) { showMessageDialog( "The \"Your Name\" field is required" ); return; } String subject = (String) data.get( "subject" ); String email = (String) data.get( "email" ); if ( email.equals( "" )) { showMessageDialog( "The \"Email\" field is required" ); return; } String email2 = (String) data.get( "email2" ); if ( email2.equals( "" )) { showMessageDialog( "The \"Email Verification\" field is required" ); return; } if ( ! email.equalsIgnoreCase( email2 )) { showMessageDialog( "The Email Verification does not match" ); return; } if ( email.indexOf( '@' ) <= 0 || email.indexOf( '@' ) == email.length() - 1) { showMessageDialog( "Email is not in a proper form" ); return; } String about = ((String) data.get( "about" )).substring(2); String text = (String) data.get( "text" ); if ( text.equals( "" )) { showMessageDialog( "Message text is required" ); return; } String code = (String) data.get( "code" ); if ( code.equals( "" ) || captchaNumber == 0 ) { showMessageDialog( "Please click the \"Get Message Code\" to get a message code, and enter it in the \"Message Code\" field." ); return; } if ( Integer.parseInt( code ) != captchaNumber ) { showMessageDialog( "Code does not match.\nPlease click the \"Get Message Code\" to get a message code, and enter it in the \"Message Code\" field." ); return; } // Everything ok, send the email. sun.net.smtp.SmtpClient client= new sun.net.smtp.SmtpClient( your-smtp-host-here ); client.from( smtp-from-address-here ); client.to( smtp-to-address-here ); java.io.PrintStream message = client.startMessage(); message.print( "From: " ); message.print( from + " <" + email + ">\n"); message.print( "To: " ); message.print( email-to-address-here ); message.print( "\n" ); message.print( "Subject: " + subject + "\n\n" ); message.print( "Message sent at " + new java.util.Date() + "\n" ); message.print( "About: " + about + "\n" ); message.print( text ); message.print( "\n" ); message.flush(); client.closeServer(); mainDiv.hide(); resultsDiv.show(); resultsDiv.setInnerHTML( "Thank you. Your message has been sent.\n" ); } catch (NumberFormatException nfe) { showMessageDialog( "Message code is not valid.\nPlease click the \"Get Message Code\" to get a message code, and enter it in the \"Message Code\" field." ); } catch (Exception ex) { ex.printStackTrace(); } } }