AnsweredAssumed Answered

Sending email from Cron Job

Question asked by younis15 on Jan 25, 2012
Latest reply on Jun 13, 2013 by nlounis
I created an email Job to send some notifications to end users. but when it tries to send an email, the following exception is thrown:

10:05:03,217  ERROR [quartz.core.ErrorLogger] Job (DEFAULT.expiredTaskJobDetail threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: org.alfresco.error.AlfrescoRuntimeException: 00250001 Failed to send email to:[younis15@msn.com, alomoush@gmail.com]]
   at org.quartz.core.JobRunShell.run(JobRunShell.java:227)
   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
Caused by: org.alfresco.error.AlfrescoRuntimeException: 00250001 Failed to send email to:[younis15@msn.com, alomoush@gmail.com]
   at org.alfresco.repo.action.executer.MailActionExecuter.executeImpl(MailActionExecuter.java:466)
   at org.alfresco.repo.action.executer.ActionExecuterAbstractBase.execute(ActionExecuterAbstractBase.java:133)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.alfresco.repo.management.subsystems.SubsystemProxyFactory$1.invoke(SubsystemProxyFactory.java:65)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy219.execute(Unknown Source)
   at org.alfresco.repo.action.ActionServiceImpl.directActionExecution(ActionServiceImpl.java:749)
   at org.alfresco.repo.action.ActionServiceImpl.executeActionImpl(ActionServiceImpl.java:675)
   at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:540)
   at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:526)
   at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:758)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.alfresco.repo.security.permissions.impl.AlwaysProceedMethodInterceptor.invoke(AlwaysProceedMethodInterceptor.java:34)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.security.permissions.impl.ExceptionTranslatorMethodInterceptor.invoke(ExceptionTranslatorMethodInterceptor.java:44)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.audit.AuditMethodInterceptor.proceedWithAudit(AuditMethodInterceptor.java:217)
   at org.alfresco.repo.audit.AuditMethodInterceptor.proceed(AuditMethodInterceptor.java:184)
   at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:137)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy34.executeAction(Unknown Source)
   at com.tts.mersal.util.EmailUtil$1.doWork(EmailUtil.java:87)
   at com.tts.mersal.util.EmailUtil$1.doWork(EmailUtil.java:1)
   at org.alfresco.repo.security.authentication.AuthenticationUtil.runAs(AuthenticationUtil.java:508)
   at com.tts.mersal.util.EmailUtil.sendMailByTemplate(EmailUtil.java:68)
   at com.tts.mersal.business.CleanExpiredTasks.escalate(CleanExpiredTasks.java:194)
   at com.tts.mersal.business.CleanExpiredTasks.doClean(CleanExpiredTasks.java:117)
   at com.tts.mersal.business.ExpiredTaskJob.execute(ExpiredTaskJob.java:34)
   at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
   … 1 more
Caused by: org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.NullPointerException
   at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:368)
   at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
   at org.alfresco.repo.action.executer.MailActionExecuter.executeImpl(MailActionExecuter.java:439)
   … 42 more
Caused by: java.lang.NullPointerException
   at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:557)
   at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:402)
   at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:341)
   at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
   … 44 more


The email sending code is :


AuthenticationUtil.runAs(new RunAsWork<Boolean>() {
          public Boolean doWork() throws Exception {
         
ActionService actionService = getServiceRegistry().getActionService();

      Action mail = actionService.createAction(MailActionExecuter.NAME);
      mail.setParameterValue(MailActionExecuter.PARAM_FROM, from);
      mail.setParameterValue(MailActionExecuter.PARAM_TO_MANY, (Serializable) to);
      mail.setParameterValue(MailActionExecuter.PARAM_SUBJECT, subject);
      mail.setParameterValue(MailActionExecuter.PARAM_TEXT, templateBody);

      
   
      actionService.executeAction(mail, null);

return true;
      
       }
         
    }, "System");


the Job configuration is :

<bean id="expiredTaskJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass">
            <value>com.tts.mersal.business.ExpiredTaskJob</value>
        </property>
        <property name="jobDataAsMap">
            <map>
                <entry key="cleanExpiredTasks">
                    <ref bean="cleanExpiredTasks" />
                </entry>
            </map>
        </property>
    </bean>
    <bean id="cleanupExpiredTaskTrigger" class="org.alfresco.util.CronTriggerBean">
        <property name="jobDetail">
            <ref bean="expiredTaskJobDetail" />
        </property>
        <property name="scheduler">
            <ref bean="schedulerFactory" />
        </property>
        <property name="cronExpression">
            <value>0 0/1 * * * ?</value>
        </property>
       
      
    </bean>
   

<bean id="emailUtil" class="com.tts.mersal.util.EmailUtil"
      init-method="init">
      <property name="serviceRegistry">
         <ref bean="ServiceRegistry" />
      </property>
      

   </bean>

Outcomes