0

I'm trying to implement my application so that when a user hits the send button an email is generated automatically and sent to another recipient. I research this on the Spring docs and found a way to try it. But so far an exception keeps coming up:

  ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
  java.lang.NoClassDefFoundError: org/springframework/mail/MailException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2404)
at java.lang.Class.getDeclaredConstructors(Class.java:1853)
at 
  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor. 

 determine CandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:230)
at 
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:976)
at  org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:949)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)at 
                                                                                        org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at 
  org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
at 
   org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5291)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:657)
at 

   org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1637)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)


  Caused by: java.lang.ClassNotFoundException: org.springframework.mail.MailException
at 

  org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at 

  org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
... 32 more

For the implementation I modified an example on the spring website and added it to my constructor method for submitting the form:

   public class ModuleController {

private MailSender mailSender;
   private SimpleMailMessage templateMessage;

   public void setMailSender(MailSender mailSender) {
    this.mailSender = mailSender;
  }

  public void setTemplateMessage(SimpleMailMessage templateMessage) {
    this.templateMessage = templateMessage;
  }

  @RequestMapping(value = "/module", method = RequestMethod.POST, params = "send")
public String sendModule(@ModelAttribute("module") Module module,BindingResult   
  result, ModelMap map) {
 SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);

    msg.setTo(module.getSta().getEmail());

    msg.setText(
     "Dear " + module.getSta().getName()
         + "a coursework has been submitted for checking"
         + module.getModuleCode()
         +module.getModuleName());
    try{
     this.mailSender.send(msg);
   }
   catch(MailException ex) {
     // simply log it and go on...
     System.err.println(ex.getMessage());   

}
return "/staff/checker/view_submission";
}   

In the project servlet I added this:

     <beans:bean id="mailSender" 
   class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <beans:property name="host" value="mail.mycompany.com"/>
</beans:bean>

<!-- this is a template message that we can pre-load with default state -->
<beans:bean id="templateMessage" 
   class="org.springframework.mail.SimpleMailMessage">
    <beans:property name="from" value="[email protected]"/>
    <beans:property name="subject" value="Your order"/>
</beans:bean>

<beans:bean id="moduleController" 
     class="com.**.**.controller.ModuleController">
    <beans:property name="mailSender" ref="mailSender"/>
    <beans:property name="templateMessage" ref="templateMessage"/>
</beans:bean>

I also added two jars, JavaMail mail.jar and JAF activation.jar to the classpath. If this implementation is wrong and the cause of the exception please let me know.

2
  • 1
    It looks like you are missing a JAR dependency. Figure out which JAR org.springframework.mail.MailException is in, and make sure it's either in your WAR's lib folder or provided by your app server somehow. Commented Apr 29, 2013 at 20:14
  • Thank you. I added the two jars to my project lib folder and refreshed the project followed by restarting the server and the exception was gone. Commented Apr 29, 2013 at 20:53

1 Answer 1

3

You need to ensure you have the spring-context-support jar in your classpath.

Sign up to request clarification or add additional context in comments.

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.