home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / MailServlet.java < prev    next >
Encoding:
Java Source  |  1999-02-10  |  4.6 KB  |  148 lines

  1. /*
  2.  * @(#)MailServlet.java    1.31 97/11/13
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21. import java.io.*;
  22. import java.util.*;
  23. import java.net.InetAddress;
  24.  
  25. import javax.servlet.*;
  26. import javax.servlet.http.*;
  27.  
  28. import sun.net.smtp.SmtpClient;
  29.  
  30. /** Simple mail servlet which processes the form and send an email.  Note: 
  31.  * requires sun.net.smtp.SmtpClient to function.  Also note that it also
  32.  * requires a direct internet connection (no firewalls), or a mailserver
  33.  * running on the webserver's host machine.  To work in other situations,
  34.  * modify the line with "java.sun.com" as the host to include a host within
  35.  * your local network that has the ability to process incoming SMTP mail.
  36.  * Most Unix machines are capable of this.
  37.  * @author  Pavani Diwanji
  38.  * @author  Jim Driscoll
  39.  */
  40. public class MailServlet extends HttpServlet {
  41.  
  42. /**
  43.  *  Sends email to the user specified by the form's "email" field.
  44.  *  @param res The servlet's response.
  45.  *  @param tab A Dictionary of the fields entered in the form.  
  46.  */
  47.     public void service(HttpServletRequest req, HttpServletResponse res)
  48.     throws IOException
  49.     {
  50.         Enumeration keys;
  51.         String key;
  52.         String value;
  53.     ServletOutputStream out = res.getOutputStream();
  54.         String line = null;
  55.  
  56.         res.setContentType("text/html");
  57.  
  58.     preamble(out, "Mail Servlet Output");
  59.  
  60.     out.println("<h1> Form Reply </h1>");
  61.     out.println("<p>");
  62.  
  63.     String name = req.getParameter("name");
  64.     String email = req.getParameter("e-mail");
  65.     boolean succeed = false;
  66.     PrintStream ps = null;
  67.         SmtpClient sendmail = null;
  68.     if ((name.length() > 0) && (email.length() > 0)) {
  69.         try {
  70.             // open smtp connection
  71.             sendmail = new SmtpClient("java.sun.com");
  72.                 sendmail.from("bogusaddress@pleasefillin");
  73.             sendmail.to(email);
  74.             // get printstream
  75.             ps = sendmail.startMessage();
  76.             succeed = true;
  77.         } catch (Exception e) {
  78.             e.printStackTrace();
  79.             out.println("Couldn't reach you through javasoft, trying your local"+
  80.               "machine instead.  You probably are behind a firewall.");
  81.         }        
  82.         if (!succeed) {
  83.             try { //try again, this time, to localhost
  84.                 // open smtp connection
  85.                 sendmail = new SmtpClient(InetAddress.getLocalHost().getHostName());
  86.             sendmail.from(email);
  87.                 sendmail.to(email);
  88.                 // get printstream
  89.                 ps = sendmail.startMessage();
  90.             } catch (Exception e) {
  91.                 e.printStackTrace();
  92.             out.println("There was an error sending you mail.");
  93.             postamble(out);
  94.             //if we bomb this time, give up
  95.             return;
  96.             }        
  97.         }
  98.         
  99.         try {
  100.             // send headers.
  101.             ps.println("From: "+email);
  102.             ps.println("To: "+email);
  103.             ps.println("Subject: Thanks for using JavaWebServer");
  104.             ps.print("\r\n"); //header area delimiter
  105.     
  106.             // now send data to it
  107.             ps.println("Hello " + name + ",");
  108.             ps.println();
  109.             ps.println("Thanks for looking at JavaWebServer.");
  110.             ps.println();
  111.             ps.println("- JavaWebServer Team.");
  112.             ps.println("(email was generated by MailServlet).");
  113.             
  114.             ps.flush();
  115.             ps.close();
  116.             sendmail.closeServer();
  117.             out.println("You will get an email thanking you for using the Java Web Server.");
  118.         } catch (Exception e) {
  119.             e.printStackTrace();
  120.             out.println("There was an error sending you mail.");
  121.             postamble(out);
  122.             return;
  123.         }
  124.     } else 
  125.         out.println("You need to enter both name and email address");
  126.  
  127.     postamble(out);
  128.     }
  129.  
  130.     public String getServletInfo() {
  131.     return "This mail servlet sends email thanking for downloading.";
  132.     }
  133.  
  134.     private void preamble(ServletOutputStream out, String s)
  135.     throws IOException
  136.     {
  137.         out.println("<HEAD><TITLE> " + s + "</TITLE></HEAD><BODY>");
  138.     }
  139.  
  140.     private void postamble(ServletOutputStream out)
  141.     throws IOException
  142.     {
  143.         out.println("</BODY>");
  144.     out.flush();
  145.     }
  146. }
  147.  
  148.