/*
************************************************************************
* you can copy, use, modify and distribute this code for educational,
* commercial or recreational use. all we ask is that you include
* this copyright notice in the source code you distribute. for
* compiled code, you will need to make accessible this copyright notice
* somewhere in the distribution, and/or via a link on the web.
* there are several reasons for this caveat - the most important being
* that open source is based on one main principle: what you find and use,
* others should also have access to. don't keep it to yourself!
* credit where credit is due! http://www.orgdot.com/javaopensource
*
*
* this software is provided by the author and contributors ``as is'' and
* any express or implied warranties, including, but not limited to, the
* implied warranties of merchantability and fitness for a particular purpose
* are disclaimed. in no event shall the author or contributors be liable
* for any direct, indirect, incidental, special, exemplary, or consequential
* damages (including, but not limited to, procurement of substitute goods
* or services; loss of use, data, or profits; or business interruption)
* however caused and on any theory of liability, whether in contract, strict
* liability, or tort (including negligence or otherwise) arising in any way
* out of the use of this software, even if advised of the possibility of
* such damage.
*
************************************************************************ */ /* STICK THIS CODE IN THE BODY OF THE HTML REDIRECT-PAGE. <script language="javascript"><!-- function replaceSpaces (str) { while (str.indexOf ('+') > -1) str = str.substring (0, str.indexOf ('+')) + ' ' + str.substring (str.indexOf ('+') + 1) return str; } function getSearchString() { str = document.location.search; if (str != "") return replaceSpaces (unescape(str.substring (1))); else return ""; } document.writeln (getSearchString()); //--></script>; STICK THIS CODE IN THE FORM <form action="http://www.your.domain.com/servlet/xendmail" method="post" name="mail_form"> <input type=hidden name="recipient" value="post@your.domain.com"> <input type=hidden name="subject" value="some subject"> <input type=hidden name="redirect" value="http://www.your.domain.com/msg.htm"> <input type=hidden name="nok_message" value="Your message has not been sent"> <input type=hidden name="aok_message" value="Your message has been sent"> </form> */ import java.io.IOException; import java.io.PrintStream; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.net.Socket; import java.net.InetAddress; import java.net.URLEncoder; import java.util.Enumeration; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class xendmail extends HttpServlet { private final String mailserver, default_recipient, default_sender, default_subject, default_redirect, default_nok_message, default_aok_message, domains[]; private final int mailport; public xendmail () { super (); mailport = 25; mailserver = "your.mailserver.com"; default_recipient = "some.email@your.domain.com"; default_sender = "some.email@your.domain.com";//we want a valid sender in case that is not entered default_subject = "orgdot automail"; default_redirect = "http://www.your.domain.com/msg.htm"; default_nok_message = "the message was sent"; default_aok_message = "there was an error - did you fill in all the fields?"; String[] domains_ = { "http://www.some.domain.com", "http://www.some.other.domain.com", "http://www.some.more.domains.com" }; domains = domains_; } public void init ( ServletConfig sc ) throws ServletException { super.init (sc); } private boolean okDomain ( HttpServletRequest request ) { String referer = request.getHeader ("referer"); for (int i = 0; i < domains.length; i++) if (referer.indexOf (domains[i]) > -1) { return true; } return false; } private String[] getSortedParams ( Enumeration param_names ) { String [] workaround = new String [100]; int num_params = 0; while(param_names.hasMoreElements()) { workaround [num_params] = (String) param_names.nextElement(); num_params ++; if (num_params == 100) break; } num_params --; String [] sorted_params = new String [num_params]; for (int i = 0; i < num_params; i++) sorted_params [i] = workaround [i]; alphabetSort (sorted_params); return sorted_params; } /* a monstrosity - but I do not have time to sort it better just right now */ private void alphabetSort ( String [] arr ) { int len = arr.length; int [] ints = new int [len]; for (int i = 0; i < len; i++) for (int j = 0; j < len; j++) { if (arr[i].compareTo (arr[j]) < 0) { String rem = arr[j]; arr [j] = arr [i]; arr [i] = rem; } } } private void sendRedirect ( HttpServletResponse response, String page, String msg ) throws IOException { response.sendRedirect (page + "?" + URLEncoder.encode (msg) ); } /* in the flash movie you will of course have a textfield named 'msg' */ private void replyFlash ( HttpServletResponse response, String msg ) throws IOException { response.setContentType("text/html"); PrintStream ps = new PrintStream ( response.getOutputStream() ); ps.print ("msg=" + URLEncoder.encode (msg)); ps.close(); } private boolean invalidMailEntry (String str) { return ( str.length() < 7 || str.indexOf ("@") < 0 || str.indexOf (".") < 0 || str.indexOf (" ") > - 1 ); } private boolean sendMail ( String mailserver_, int mailport_, String recipient_, String sender_, String subject_, StringBuffer body_ ) { if (invalidMailEntry (sender_)) sender_ = default_sender; try { Socket socket = new Socket ( mailserver_, mailport_ ); BufferedInputStream bis = new BufferedInputStream ( socket.getInputStream() ); ByteArrayOutputStream baos = new ByteArrayOutputStream (); PrintStream ps = new PrintStream ( socket.getOutputStream() ); if ( readStringLine (bis, baos) && writeStringLine (ps, "HELO " + socket.getLocalAddress().getHostName() + "\r\n") && readStringLine (bis, baos) && writeStringLine (ps, "MAIL FROM: " + sender_ + "\r\n") && readStringLine (bis, baos) && writeStringLine (ps, "RCPT To: " + recipient_ + "\r\n") && readStringLine (bis, baos) && writeStringLine (ps, "DATA\r\n") && readStringLine (bis, baos) && writeStringLine (ps, "Subject: " + subject_ + "\r\n") && writeStringLine (ps, body_.toString() + "\r\n") && writeStringLine (ps, ".\r\n") && writeStringLine (ps, "quit\r\n") ) { ps.close (); bis.close (); socket.close (); baos.close (); } return true; } catch (Throwable t) { return false; } } private boolean writeStringLine ( PrintStream ps, String str ) throws IOException { ps.print (str); ps.flush(); return true; } private boolean readStringLine ( BufferedInputStream in, ByteArrayOutputStream baos ) throws IOException { baos.reset (); if (in == null) return false; in.mark ( in.available() ); int i; while ( ( i = in.read() ) > - 1) { if (i == '\n') return true; else if (i == '\r') { if ( (i = in.read()) == '\n' ) return true; else if (i == -1 ) return false; else { in.reset (); in.skip ( baos.size () + 1 ); return true; } } else baos.write (i); } in.close(); return true; } public void service ( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { if ( ! okDomain (request) ) { sendRedirect (response, default_redirect, default_nok_message); } else { String recipient = default_recipient; String sender = default_sender; String subject = default_subject; String redirect = default_redirect; String nok_message = default_nok_message; String aok_message = default_aok_message; StringBuffer body = new StringBuffer(); boolean reply_flash = false; body.append ("------------------------------\n"); body.append ("Orgdot automail:\ndate: "); body.append (new Date ()); body.append ("\nServer: "); body.append (getServletConfig().getServletContext().getServerInfo()); body.append ("\nIP trace on remote machine: "); body.append (InetAddress.getByName(request.getRemoteAddr())); body.append ("\n------------------------------\n\n\n"); body.append ("\nPlease note! If the sender's address above is "); body.append (default_sender); body.append ("\nthen the user filling out the form has not entered anything"); body.append ("\nin the e-mail field. "); body.append (default_sender); body.append (" is just to keep\nsendmail happy..."); body.append ("\n------------------------------\n\n\n"); String[] sorted_param_names = getSortedParams (request.getParameterNames ()); for (int j = 0; j < sorted_param_names.length; j++) { String str = sorted_param_names[j]; String check; String [] param_values = request.getParameterValues (str); if ( str.equals ("recipient") && ( check = param_values [0]) != null ) { recipient = check; } else if ( str.equals ("sender") && ( check = param_values [0]) != null) { sender = check; } else if ( str.equals ("reply_flash") && ( check = param_values [0]) != null ) { reply_flash = check.equals("true"); } else if ( str.equals ("subject") && ( check = param_values [0]) != null ) { subject = check; } else if ( str.equals ("redirect") && ( check = param_values [0]) != null ) { redirect = check; } else if ( str.equals ("nok_message") && ( check = param_values [0]) != null ) { nok_message = check; } else if ( str.equals ("aok_message") && ( check = param_values [0]) != null ) { aok_message = check; } else for (int i = 0; i < param_values.length; i++) { body.append (str); body.append (":\n"); body.append (param_values [i]); body.append ("\n\n"); } } if ( sendMail ( mailserver, mailport, recipient, sender, subject, body ) ) { if (reply_flash) replyFlash (response, aok_message); else sendRedirect (response, redirect, aok_message); } else { if (reply_flash) replyFlash (response, nok_message); else sendRedirect (response, redirect, nok_message); } } } }