我正在尝试实现我的应用程序,以便当用户点击发送按钮时,会自动生成一封电子邮件并将其发送给另一个收件人。我在 Spring docs 上对此进行了研究,并找到了一种尝试方法。但到目前为止,一个例外不断出现:
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
对于实现,我在 spring 网站上修改了一个示例,并将其添加到我的构造函数方法中以提交表单:
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";
}
在项目 servlet 中,我添加了以下内容:
<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="customerservice@mycompany.com"/>
<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>
我还在类路径中添加了两个 jar,JavaMail mail.jar 和 JAF activation.jar。如果这个实现是错误的以及异常的原因,请告诉我。