我正在使用Spring,Quartz来安排发送电子邮件功能。但我不能这样做 这是我的错误
java.lang.NullPointerException
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTr ansport.java:557)
at org.springframework.mail.javamail.JavaMailSenderIm pl.doSend(JavaMailSenderImpl.java:382)
at org.springframework.mail.javamail.JavaMailSenderIm pl.send(JavaMailSenderImpl.java:291)
at org.springframework.mail.javamail.JavaMailSenderIm pl.send(JavaMailSenderImpl.java:277)
at org.vnitt.service.SendMailService.sendMail(SendMai lService.java:59)
at org.vnitt.shedule.SendMailJob.executeInternal(Send MailJob.java:47)
at org.springframework.scheduling.quartz.QuartzJobBea n.execute(QuartzJobBean.java:90)
at org.quartz.core.JobRunShell.run(JobRunShell.java:2 02)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run (SimpleThreadPool.java:525)
---这是我的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailS enderImpl">
<property name="defaultEncoding" value="UTF-8"/>
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="protocol" value="smtps" />
<property name="username" value="myemail@gmail.com"/>
<property name="password" value="mypass"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtps.auth">true</prop>
<prop key="mail.smtps.starttls.enable">true</prop>
<prop key="mail.smtps.debug">true</prop>
</props>
</property>
</bean>
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="myemail@gmail.com" />
<property name="subject" value="Testing send email using Spring and Gmail " />
</bean>
<bean class="org.springframework.scheduling.quartz.Sched ulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronSendMailTrigger"/>
</list>
</property>
</bean>
<!-- end config Quartz to schdule a task -->
<!-- begin config to schedule SendMailService -->
<bean id="sendMailService"
class="org.service.SendMailService">
<property name="mailSender">
<ref bean="mailSender"/>
</property>
<property name="mailMessage">
<ref bean="mailMessage"/>
</property>
</bean>
<!-- config JobDetail -->
<bean id="sendMailJob"
class="org.springframework.scheduling.quartz.JobDe tailBean">
<property name="jobClass">
<value>org.shedule.SendMailJob</value>
</property>
<property name="jobDataAsMap">
<map>
<entry key="sendMailService">
<ref bean="sendMailService"/>
</entry>
</map>
</property>
</bean>
<!-- config cron trigger -->
<bean id="cronSendMailTrigger"
class="org.springframework.scheduling.quartz.CronT riggerBean">
<property name="jobDetail">
<ref bean="sendMailJob"/>
</property>
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>
<!-- end config to schedule SendMailService -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/spring_quartz_scheduler.vnitt">mainController</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlVi ew</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Plz帮我解决了这个问题! 感谢
修改
感谢您的回答。我修复了我的错误,我没有在我的代码和配置中设置收件人。这是原因。
答案 0 :(得分:10)
您是否指定了电子邮件的收件人?您在Spring上下文中没有这样做(to
未设置)。这很可能是NPE的原因。
答案 1 :(得分:0)
我也遇到了类似的问题,而且由于没有收件人。