• Post Reply Bookmark Topic Watch Topic
  • New Topic

JavaMail and Servlets

 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Iam trying to execute this code but its generating an exception..can someone let me know the cause of it.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmail extends HttpServlet
{

private String msg,str;
public void init(ServletConfig config) throws ServletException
{

}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
msg=req.getParameter("MSG");
res.setContentType("text/HTML");
try
{
MailClass m=new MailClass(msg);
str=m.sendmsg();
PrintWriter out = res.getWriter();
out.println("<Html><body>"+str+"</Body></html>");
}
catch(Exception e){System.out.println("Error1: " +e);}
}

}
class MailClass
{
String Msg;
MailClass(String msg)
{
Msg=msg;
}

public String sendmsg() throws Exception
{
String host = "w.com";
String from = "abc@w.com";
String to = "xyz@w.com";

// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getDefaultInstance(props,null);

// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));

// Set the to address
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// Set the subject
message.setSubject(Msg);

// Set the content
message.setText("Welcome to JavaMail");

// Send message
Transport.send(message);
session.setDebug(true);
return "Message sent";
}
}

the error it says is
Internal Servlet Error:
java.lang.NoClassDefFoundError: MailClass
at java.lang.Class.newInstance0(Native Method)
at java.lang.Class.newInstance(Unknown Source)
at com.sun.web.core.ServletWrapper.loadServlet(ServletWrapper.java:100)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:119)
at com.sun.web.core.InvokerServlet.service(InvokerServlet.java:168)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:840)
at com.sun.web.core.ServletWrapper.handleRequest(ServletWrapper.java:155)
at com.sun.web.core.Context.handleRequest(Context.java:414)
at com.sun.web.server.ConnectionHandler.run(ConnectionHandler.java:139)
Iam using jswdk-1.0.1 as the webserver and JDK1.3.1_06.
Thanks in advance
Swapna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chances are you have forgotten to copy the MailClass.class file to the web server.
 
swapna sivaraju
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah thats right i figured out that i didnt copy MailClass to the same directory.

Thanks
 
Michael Lowden
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could also have made the class an inner class without needing to make a whole new file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!