001//Sign Me Up Servlet
002package ca.bc.webarts.servlet;
003
004import java.io.*;
005import java.util.*;
006import java.text.*;
007
008import javax.servlet.*;
009import javax.servlet.http.*;
010import javax.mail.*;
011import javax.mail.internet.*;
012import javax.activation.*;
013
014/**
015 * This is a <B>servlet</b> that sends a simple mail message using the JavaMail APIs
016 *
017 * @author Tom Gutwin P.Eng.
018 */
019public class SimpleSendMail extends HttpServlet implements SingleThreadModel {
020    static String msgText = "This is a message body.<BR>\nHere's the second line.";
021
022    /**
023     * This method handles the GET requests for the client.
024     */
025    public void doGet (HttpServletRequest req, HttpServletResponse res)
026  throws ServletException, IOException {
027
028  // Set the content type
029      res.setContentType("text/html");
030
031  // Return early if this is a HEAD
032  if (req.getMethod().equals("HEAD")) return;
033
034  // Proceed otherwise
035  PrintWriter out = res.getWriter();
036  String name = req.getParameter("name");
037  String docTitle = req.getParameter("title");
038  boolean debug = true;
039  String to = "tgutwin@webarts.bc.ca";
040  String from = "fred@webarts.bc.ca";
041  String host = "aurora1.webarts.bc.ca";
042
043  // create some properties and get the default Session
044  Properties props = new Properties();
045  props.put("mail.smtp.host", host);
046  if (debug) props.put("mail.debug", "true");
047
048  Session session = Session.getDefaultInstance(props, null);
049  session.setDebug(debug);
050
051  try {
052      // create a message
053      Message msg = new MimeMessage(session);
054      msg.setFrom(new InternetAddress(from));
055      InternetAddress[] address = {new InternetAddress(to)};
056      msg.setRecipients(Message.RecipientType.TO, address);
057      msg.setSubject("JavaMail APIs Test");
058      msg.setSentDate(new Date());
059      // If the desired charset is known, you can use
060      // setText(text, charset)
061      msg.setText(msgText);
062
063      Transport.send(msg);
064  } catch (MessagingException mex) {
065      System.out.println("\n--Exception handling in msgsendsample.java");
066
067      mex.printStackTrace();
068      System.out.println();
069      Exception ex = mex;
070      do {
071    if (ex instanceof SendFailedException) {
072        SendFailedException sfex = (SendFailedException)ex;
073        Address[] invalid = sfex.getInvalidAddresses();
074        if (invalid != null) {
075      System.out.println("    ** Invalid Addresses");
076      if (invalid != null) {
077          for (int i = 0; i < invalid.length; i++)
078        System.out.println("         " + invalid[i]);
079      }
080        }
081        Address[] validUnsent = sfex.getValidUnsentAddresses();
082        if (validUnsent != null) {
083      System.out.println("    ** ValidUnsent Addresses");
084      if (validUnsent != null) {
085          for (int i = 0; i < validUnsent.length; i++)
086        System.out.println("         "+validUnsent[i]);
087      }
088        }
089        Address[] validSent = sfex.getValidSentAddresses();
090        if (validSent != null) {
091      System.out.println("    ** ValidSent Addresses");
092      if (validSent != null) {
093          for (int i = 0; i < validSent.length; i++)
094        System.out.println("         "+validSent[i]);
095      }
096        }
097    }
098    System.out.println();
099      } while ((ex = ((MessagingException)ex).getNextException())
100         != null);
101  }
102
103  // Make up the response
104  out.println(htmlDocHeader(docTitle));
105  out.println("<BODY>");
106  out.println("Hello, " + name +"<BR>\n");
107  out.println(msgText);
108
109  out.println("</BODY></HTML>");
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 heading of a document.
125  * It will include everything  from the original <code> html </code> tag to the
126  * <code> head </code> BUT not the <code> body</code> .
127  */
128  private String htmlDocHeader(String title){
129    String retVal = "<HTML>\n<HEAD><TITLE>"+title+"</TITLE></HEAD>";
130
131    return retVal;
132  }
133  /**
134  * This method returns a string containing the HTML to make up the heading of a document.
135  * It will include everything  from the original <code> html </code> tag to the
136  * <code> head </code> BUT not the <code> body</code> .
137  */
138  private String htmlDocHeader(){
139    String retVal = "<HTML>\n<HEAD><TITLE>"+"Sample Servlet Output"+"</TITLE></HEAD>";
140
141    return retVal;
142  }
143
144}