Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2006, 12:08 AM
Authorized User
 
Join Date: Aug 2005
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Default some serious problem

i got this code from a site ... it is compiled successfully but gives an exception "java.net.UnknownHostException: mailhost"
does it mean that i need a Mail Server to execute this code? ... if YES, then where i can find a mail server, plz give me a web link ... if NO, then what iz the problem with this code ... plz guide me bcoz i m new in this field

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.oreilly.servlet.ParameterParser;
import com.oreilly.servlet.ServletUtils;

import sun.net.smtp.SmtpClient;

public class MailServlet extends HttpServlet {

  static final String FROM = "[email protected]";
  static final String TO = "[email protected]";

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    ParameterParser parser = new ParameterParser(req);
    String from = parser.getStringParameter("from", FROM);
    String to = parser.getStringParameter("to", TO);

    try {
      SmtpClient smtp = new SmtpClient(); // assume localhost
      smtp.from(from);
      smtp.to(to);
      PrintStream msg = smtp.startMessage();

      msg.println("To: " + to); // so mailers will display the To: address
      msg.println("Subject: just checking...");
      msg.println();

      Enumeration enm = req.getParameterNames();
      while (enm.hasMoreElements()) {
        String name = (String)enm.nextElement();
        if (name.equals("to") || name.equals("from")) continue; // Skip to/from
        String value = parser.getStringParameter(name, null);
        msg.println(name + " = " + value);
      }

      msg.println();
      msg.println("---");
      msg.println("Sent by " + HttpUtils.getRequestURL(req));

      smtp.closeServer();

      out.println("Thanks for the submission...");
    }
    catch (Exception e) {
        e.printStackTrace();
      out.println("There was a problem handling the submission...");
      getServletContext().log(e, "There was a problem sending email");
    }
  }
}










Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.