0

I'm developing an application for sending bulk emails from multiple senders in a continuous loop. Sender email-ID's are stored in a csv file and I am reading that in ReadFile class and calling it in servlet class where I am also calling an email utility class which have email sending functions.

ReadFile.java

CsvReader senders; 
public List<String> read(){
  ArrayList<String> al=new ArrayList<String>();
  try {
    senders = new CsvReader("C:/Users/dc/Documents/Senderlist.csv");
    senders.readHeaders();

    while (senders.readRecord()) {
      String SenderID = senders.get("SenderID");
      // perform program logic here
      System.out.println("Sender ID is: "+SenderID );
      al.add(SenderID);
    }
    senders.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return al;
}

Servlet.java:

public class MailController extends HttpServlet {
  private static final long serialVersionUID = 1L;
  private String ExchangeIP;
  private String port;
  ReadFile rf;
  /**
   * @throws IOException 
   * @see HttpServlet#HttpServlet()
   */
  public MailController() throws IOException {
    rf=new ReadFile();
  }

  public void init() {
    // reads SMTP server setting from web.xml file
    ServletContext context = getServletContext();
    ExchangeIP = context.getInitParameter("ExchangeIP");
    port = context.getInitParameter("port");
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // read from field
    List<File> uploadedFiles= saveUploadedFiles(request);
    String sender=request.getParameter("sender");// reading from the form page
    String recipient=request.getParameter("recipient");
    String subject=request.getParameter("subject");
    String content=request.getParameter("content");
    String resultMessage = ""; //null
    try {
      List sendersInput = rf.read();
      // print all the elements in the list 
      Iterator itr = sendersInput.iterator();
      while(itr.hasNext()) {
        EmailUtility.sendEmail(ExchangeIP, port, itr.next(), recipient, subject, content, uploadedFiles);           
        resultMessage = "The e-mail has been sent successfully";
      }
    } catch (Exception ex) {
            ex.printStackTrace();
            resultMessage = "There were an error: " + ex.getMessage();
    } finally {
      request.setAttribute("Message", resultMessage);
      getServletContext()
        .getRequestDispatcher("/Result.jsp")
        .forward(request, response);
    }   
  }
}

While running this I am getting an error:

java.lang.ClassNotFoundException: com.csvreader.CsvReader
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at com.project.util.ReadFile.read(ReadFile.java:20)
    at com.project.controller.MailController.doPost(MailController.java:99)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)

How to resolve this error.

2
  • How do you run your program? Commented May 15, 2017 at 11:30
  • using apache tomcat server Commented May 16, 2017 at 5:37

2 Answers 2

3

You're missing the opencsv.jar library. Specify it in your java command with

java -cp opencsv.jar:...
Sign up to request clarification or add additional context in comments.

6 Comments

It looks like the OP is running this as a web application. In that case, he should put opencsv.jar in the WEB-INF/lib directory of his war file.
I have added opencsv.jar in WEB-INF/lib ..still im gettting same error.
Which web/app-server do you use?
apache tomcat server
There is also the option: The Common class loader loads all classes and JAR files contained in $CATALINA_HOME/lib. These resources are visible to all applications and to Tomcat. but usually it should work with WEB-INF/lib how do you build the WAR file?
|
0

By adding servlet-api.jar from apache tomcat library in to projects->properties->java build path, Solved the error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.