SendMailServlet example & server setup ?



I tried this example :


// import the JavaMail packages
import javax.mail.*;
import javax.mail.internet.*;

// import the servlet packages
import javax.servlet.*;
import javax.servlet.http.*;

// import misc classes that we need
import java.util.*;
import java.io.*;

public class SendMailServlet extends HttpServlet {
String smtpServer;

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

// get the SMTP server from the servlet properties
smtpServer = config.getInitParameter("smtpServer");
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// get the message parameters from the HTML page
String from = req.getParameter("from");
String to = req.getParameter("to");
String subject = req.getParameter("subject");
String text = req.getParameter("text");

PrintWriter out = res.getWriter();
res.setContentType("text/html");

try {
// set the SMTP host property value
Properties properties = System.getProperties();
properties.put("smtp.mail.yahoo.com", smtpServer);

// create a JavaMail session
Session session = Session.getInstance(properties, null);

// create a new MIME message
MimeMessage message = new MimeMessage(session);

// set the from address
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);

// set the to address
if (to != null) {
Address[] toAddress = InternetAddress.parse(to);
message.setRecipients(Message.RecipientType.TO, toAddress);
}
else
throw new MessagingException("No \"To\" address specified");

// set the subject
message.setSubject(subject);

// set the message body
message.setText(text);

// send the message
Transport.send(message);

out.println("Message sent successfully.");
}
catch (AddressException e) {
out.println("Invalid e-mail address.<br>" + e.getMessage());
}
catch (SendFailedException e) {
out.println("Send failed.<br>" + e.getMessage());
}
catch (MessagingException e) {
out.println("Unexpected error.<br>" + e.getMessage());
}
}
}


and got this:

type Exception report

message

description The server encountered an internal error () that prevented it
from fulfilling this request.

exception

java.lang.NullPointerException
java.util.Hashtable.put(Hashtable.java:396)
SendMailServlet.doPost(SendMailServlet.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
with : properties.put("smtp.mail.yahoo.com", smtpServer);in line
40.That is my first servlet so if you be so kind to give me a detailed
answer.


.



Relevant Pages

  • Re: pls help w/cookies.......
    ... into a session object in the server side). ... the output of the handler servlet contains a link to a viewer ...
    (comp.lang.java.help)
  • Re: how does the Model (Command), in MVC, return objects to a servlet?
    ... Command command = lookupCommand; Result result = ... command.execute(request); ... The Base Enterprise Servlet from which all Enterprise Servlet ... public void service ...
    (comp.lang.java.help)
  • Re: How to re-use existing classes in JSP/JavaBeans/Servlets
    ... I want to instantiate them and have the instance ... so the lifecycle is that of the client session. ... Process all data that comes in a HTTP request within a servlet -- they're ... are a good match with JSPs, and JSPs are good for creating HTML views. ...
    (comp.lang.java.programmer)
  • applet to servlet
    ... I'm trying to send an object to a servlet for processing. ... getting an EOFException or NullPointerException at the readObject() method ... public void doPost(HttpServletRequest request, ... class TableData implements Serializable ...
    (comp.lang.java.help)
  • Re: Servlet design question
    ... don't put huge objects in the session, ... Concerning fields in the servlet class itself, no, you shouldn't do it, ... > for that request only (e.g. request-related stuff scooped out of ... > wrapper also contains a reference to my SessionState object, ...
    (comp.lang.java.programmer)