Wednesday, May 4, 2011

email servlet


Listing 1:

 email servlet
email.code=au.com.esoft.servlet.MailServlet
email.initparams=\
  sendserver=smtp.esoft.com.au,\
  recvserver=pop3.esoft.com.au,\
  protocol=pop3,\
  domain=esoft.com.au,\
  debug=true

Listing 2:

public void init(ServletConfig config)
                   throws ServletException
{
  super.init(config);

  sendServer = getInitParameter("sendserver");
  if (sendServer == null)
    throw new ServletException("Send mail server parameter
    missing!");

  recvServer = getInitParameter("recvserver");
  if (recvServer == null)
    throw new ServletException("Incoming mail server parame-
    ter missing!");

  protocol = getInitParameter("protocol");
  if (protocol == null)
    protocol = "pop3";

  domain = getInitParameter("domain");
  if (domain == null)
    throw new ServletException("Domain parameter missing!");

  Properties props = System.getProperties();
  props.put("mail.smtp.host", sendServer);
  mailSession = Session.getDefaultInstance(props, null);

  String debugParam = getInitParameter("debug");
  if ("true".equals(debugParam))
  {
    debug = true;
    mailSession.setDebug(true);
  }
  else
  {
    debug = false;
    mailSession.setDebug(false);
  }
}

Listing 3:

public void doMessageList(HttpServletRequest req,
                       HttpServletResponse res)
                       throws ServletException, IOException
{
  PrintWriter toClient = res.getWriter();

  // Get the session for this user.
  HttpSession httpSession = req.getSession(false);
  sendPageStart(toClient);
  Folder folder = null;
  Message[] msgs = null;
  try
  {
    folder = (Folder)httpSession.getValue("folder");
    if (!folder.isOpen())
      folder.open(Folder.READ_WRITE);

    msgs = folder.getMessages();
    FetchProfile fp = new FetchProfile();
    fp.add(FetchProfile.Item.ENVELOPE);
    folder.fetch(msgs, fp);
  }
  catch (MessagingException me)
    toClient.println("<tr><td>" + me.toString() +
                              "</td></tr>");

  if (msgs.length > 0 )
  {
    sendReplyHeader(toClient, "Message List");
    sendTableHeader(toClient);
  }
  else
    sendReplyHeader(toClient,
          "There are no messages in the mailbox!");

  // For each message, show its header
  for (int i = msgs.length; i > 0; i--)
  {
    try
    {
      Message m = msgs[i-1];
      if (m.isSet(Flags.Flag.DELETED))
        continue;

      // Generate HTML code for the message header details.
      toClient.println("<tr bgcolor=#eeeecc><td><a href=" +
                             HttpUtils.getRequestURL(req) +
                             ?command=retrieve&username=" +
                               username + "&msg=" + i +
                              ">" +  
                              m.getFrom()[0].toString() +
                              "</a></td><td>" +
                              m.getSentDate() +
                              "</td><td>" +
                             m.getSubject() +
                             "&nbsp</td></tr>");

  }
    catch (MessagingException me)
      toClient.println("<tr><td>" +
                                me.toString() +
                                "</td></tr>");
}

  if (msgs.length > 0 )
    sendTableFooter(toClient);

  // Send the mail options.
  toClient.println( "<center>" );
  toClient.println( "<nobr>&#160;" +
                          "<font face=Arial, Helvetica " +
                          "size=2 color=#FFFFFF " +
                          "style=color:#FFFFFF; " +
                          "text-decoration:none><a href=" +
                           HttpUtils.getRequestURL(req) +
                          "?command=logout&username=" +
                           username +
                             ">Logout</a></font>&#160;</nobr> ");
  toClient.println( "<nobr>&#160;" +
                          "<font face=Arial, Helvetica " +
                          "size=2 color=#FFFFFF " +
                          "style=color:#FFFFFF; " +
                          "text-decoration:none><a href=" +
                             HttpUtils.getRequestURL(req) +
                             "?command=msglist&username=" +
                             username +
                             ">Refresh</a></font>&#160;</nobr> ");
  toClient.println( "<nobr>&#160;" +
                          "<font face=Arial, Helvetica " +
                          "size=2 color=#FFFFFF " +
                          "style=color:#FFFFFF; " +
                          "text-decoration:none><a href=" +
                             HttpUtils.getRequestURL(req) +
                             "?command=compose&username=" +
                             username +
                             ">Compose</a></font>&#160;</nobr> ");
  toClient.println( "</center>" );

  try
    folder.close(true);
  catch (MessagingException me)
    me.printStackTrace();

sendPageEnd(toClient);
}

Listing 4:

public void doSend(HttpServletRequest req,
                                HttpServletResponse res)
                                throws ServletException,
                                IOException
{
  PrintWriter toClient = res.getWriter();

  // Get the session for this user.
  HttpSession httpSession = req.getSession(false);

  sendPageStart(toClient);
  try
  {
    MimeMessage msg = new MimeMessage(mailSession);
    InternetAddress sender =
                new InternetAddress(username + '@' + domain);
    msg.setFrom(sender);
    String recipients = req.getParameter("to");
    // JavaMail follows RFC822 standard which allows
    // only comma separated addresses, so we have to
    // replace semicolons with commas, if any
    recipients = recipients.replace(';', ',');
    Address[] address =
              (Address[])InternetAddress.parse(recipients, false);
    msg.addRecipients(Message.RecipientType.TO, address);
    // Parse the 'cc' recipients and add them to the message.
    recipients = req.getParameter("cc");
    recipients = recipients.replace(';', ',');
    address = (Address[])InternetAddress.parse(recipients, false);
    msg.addRecipients(Message.RecipientType.CC, address);
    // Parse the 'bcc' recipients and add them to the message.
    recipients = req.getParameter("bcc");
    recipients = recipients.replace(';', ',');
    address = (Address[])InternetAddress.parse(recipients, false);
    msg.addRecipients(Message.RecipientType.BCC, address);
    // Check if we have to send the message to the sender too.
    String bccMyself = req.getParameter("bccmyself");
    debugMsg("Bcc myself = " + bccMyself);
    if (bccMyself != null)
    msg.addRecipient(Message.RecipientType.BCC, sender);
    // Set the message subject.
    String subject = req.getParameter("subject");
    msg.setSubject(subject);
    // Put the message body in the message.
    String body = req.getParameter("body");
    msg.setText(body);
    // Set other message parameters.
    msg.setHeader("X-Mailer", productName);
    msg.setSentDate(new Date());
    // Finally send the message.
    Transport.send(msg);
    sendReplyHeader(toClient, "Message sent.");
  }
  catch (AddressException ae)
    sendReplyHeader(toClient, ae.getMessage());
  catch (MessagingException me)
    sendReplyHeader(toClient, me.getMessage());

  toClient.println( "<br><br>" );
  toClient.println( "<center>" );
  toClient.println( "<nobr>&#160;" +
                       "<font face=Arial, Helvetica " +
                       "size=2 color=#FFFFFF " +
                       "style=color:#FFFFFF; " +
                       "text-decoration:none>" +
                       "<a href=" +
                        HttpUtils.getRequestURL(req) +
                       "?command=logout&username=" +
                        username +
                               
                       ">Logout</a></font>&#160;</nobr> ");
                        toClient.println( "<nobr>&#160;" +
                       "<font face=Arial, Helvetica " +
                       "size=2 color=#FFFFFF " +
                       "style=color:#FFFFFF; " +
                       "text-decoration:none>" +
                       "<a href=" +
                        HttpUtils.getRequestURL(req) +
                       "?command=msglist&username=" +
                        username +
                       ">Message  
                        List</a></font>&#160;</nobr> ");
                        toClient.println( "<nobr>&#160;" +
                       "<font face=Arial, Helvetica " +
                      "size=2 color=#FFFFFF " +
                      "style=color:#FFFFFF; " +
                      "text-decoration:none>" +
                      "<a href=" +
                       HttpUtils.getRequestURL(req) +
                      "?command=compose&username=" +
                       username +
                               
                      ">Compose</a></font>&#160;</nobr> ");
                       toClient.println( "</center>" );

  sendPageEnd(toClient);
}

No comments:

Post a Comment