gpt4 book ai didi

java - 无法使用 Spring 和 Quartz 发送电子邮件

转载 作者:行者123 更新时间:2023-12-04 20:45:14 25 4
gpt4 key购买 nike

我正在使用 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>

请帮我解决这个问题!谢谢

编辑

感谢您的回答。我已经修复了我的错误,我没有在我的代码和配置中设置收件人。这是原因。

最佳答案

您是否指定电子邮件消息的收件人?您没有在 Spring 上下文中这样做(to 未设置)。这很可能是 NPE 的原因。

关于java - 无法使用 Spring 和 Quartz 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1263805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com