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");
}
}
}