001//Sign Me Up Servlet
002package ca.bc.webarts.servlet;
003
004
005import java.io.*;
006import java.util.*;
007import java.text.*;
008
009import javax.servlet.*;
010import javax.servlet.http.*;
011import javax.mail.*;
012import javax.mail.internet.*;
013import javax.activation.*;
014
015/**
016 * This is a <B>servlet</b> that sends a simple mail message using the JavaMail APIs
017 *
018 * @author Tom Gutwin P.Eng.
019 */
020public class InfoByEmailSignUp extends HttpServlet implements SingleThreadModel {
021    static String msgText = "This is a message body.<BR>\nHere's the second line.";
022  String emailAddr;
023    /**
024     * This method handles the GET requests for the client.
025     */
026    public void doGet (HttpServletRequest req, HttpServletResponse res)
027  throws ServletException, IOException {
028
029  // Set the content type
030      res.setContentType("text/html");
031
032  // Return early if this is a HEAD
033  if (req.getMethod().equals("HEAD")) return;
034
035  // Proceed otherwise
036  PrintWriter out = res.getWriter();
037  String name = req.getParameter("name");
038  String docTitle = req.getParameter("title");
039  emailAddr = req.getParameter("FormsEditField2");
040  if(emailAddr.equals(null)) emailAddr =  "webupdaterequest@ieee-spm2001.org";
041  boolean debug = true;
042  String to = "webupdaterequest@ieee-spm2001.org";
043  String from = emailAddr;
044  String host = "aurora1.webarts.bc.ca";
045  msgText = "\nSUBSCRIBE " + emailAddr;
046
047  // create some properties and get the default Session
048  Properties props = new Properties();
049  props.put("mail.smtp.host", host);
050  if (debug) props.put("mail.debug", "true");
051
052  Session session = Session.getDefaultInstance(props, null);
053  session.setDebug(debug);
054
055  try {
056      // create a message
057      Message msg = new MimeMessage(session);
058      msg.setFrom(new InternetAddress(from));
059      InternetAddress[] address = {new InternetAddress(to)};
060      msg.setRecipients(Message.RecipientType.TO, address);
061      msg.setSubject("SUBSCRIBE SPM2001 WEBUPDATE");
062      msg.setSentDate(new Date());
063      // If the desired charset is known, you can use
064      // setText(text, charset)
065      msg.setText(msgText);
066
067      Transport.send(msg);
068  } catch (MessagingException mex) {
069      System.out.println("\n--Exception handling in emailme.java");
070
071      mex.printStackTrace();
072      System.out.println();
073      Exception ex = mex;
074      do {
075    if (ex instanceof SendFailedException) {
076        SendFailedException sfex = (SendFailedException)ex;
077        Address[] invalid = sfex.getInvalidAddresses();
078        if (invalid != null) {
079      System.out.println("    ** Invalid Addresses");
080      if (invalid != null) {
081          for (int i = 0; i < invalid.length; i++)
082        System.out.println("         " + invalid[i]);
083      }
084        }
085        Address[] validUnsent = sfex.getValidUnsentAddresses();
086        if (validUnsent != null) {
087      System.out.println("    ** ValidUnsent Addresses");
088      if (validUnsent != null) {
089          for (int i = 0; i < validUnsent.length; i++)
090        System.out.println("         "+validUnsent[i]);
091      }
092        }
093        Address[] validSent = sfex.getValidSentAddresses();
094        if (validSent != null) {
095      System.out.println("    ** ValidSent Addresses");
096      if (validSent != null) {
097          for (int i = 0; i < validSent.length; i++)
098        System.out.println("         "+validSent[i]);
099      }
100        }
101    }
102    System.out.println();
103      } while ((ex = ((MessagingException)ex).getNextException())
104         != null);
105  }
106
107  // Make up the response
108  out.println(htmlDocHeader("IEEE Power Engineering Society - Summer Power Meeting 2001"));
109  out.println(htmlDocBody());
110  }
111
112  /**
113     * This method handles the "POST" submission.
114     * The <code>send</code> parameter denotes
115     * that the method is processing the compose form submission.
116     */
117    public  void doPost(HttpServletRequest req, HttpServletResponse res)
118  throws ServletException, IOException {
119
120  doGet(req, res);
121  }
122
123  /**
124  * This method returns a string containing the HTML to make up the body of a document.
125  * It will include everything  from the original <code> body </code> tag to the closing
126  * <code> body</code> and <code> html</code> .
127  */
128  private String htmlDocBody(){
129    String retVal = "";
130    retVal += "<BODY BGCOLOR=\"#99ccff\" >\n";
131    retVal += "<H2><font color=\"#203080\">Summer Power Meeting 2001<BR>Site Change Notification Confirmation</font></h2>\n";
132    retVal +=  "<IMG SRC=\"http://ieee.ca/vancouver/graphics/rainbowline.gif\" border=0 align=\"bottom\" alt=\".........................................................\"><BR>\n";
133    retVal += "<BR><I>" + emailAddr + "</I><BR><BR>\n<A HREF=\"http://www.ieee-spm2001.org\" alt=\"Home Page\">Back To IEEE SPM 2001 Home</A>";
134    retVal += "</body>\n</html>";
135    return retVal;
136  }
137
138  /**
139  * This method returns a string containing the HTML to make up the heading of a document.
140  * It will include everything  from the original <code> html </code> tag to the
141  * <code> head </code> BUT not the <code> body</code> .
142  */
143  private String htmlDocHeader(String title){
144    String retVal = "";
145    retVal += "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 3.0//EN\">\n";
146    retVal += "<HTML>\n";
147    retVal += "<HEAD>\n";
148    retVal += "<TITLE>" + title + "</TITLE>\n";
149    retVal += "</HEAD>\n";
150
151
152    return retVal;
153  }
154
155  /**
156  * This method returns a string containing the HTML to make up the heading of a document.
157  * It will include everything  from the original <code> html </code> tag to the
158  * <code> head </code> BUT not the <code> body</code> .
159  */
160  private String htmlDocHeader(){
161    String retVal = "<HTML>\n<HEAD><TITLE>"+"Sample Servlet Output"+"</TITLE></HEAD>";
162
163    return retVal;
164  }
165
166}