AnsweredAssumed Answered

Sending Email Notifications for Calendar Events in Alfresco 4.2

Question asked by tuti on Jan 22, 2014
<em><strong>I have a custom implementation of email notifications for calendar events. I hope to send emails 5 days in advance to site members about a calendar event. So far, I keep getting this error (which is from the tomcat log files)</em></strong>


2014-01-22 17:54:01,241  ERROR [quartz.core.ErrorLogger] [DefaultScheduler_Worker-10] Job (jobGroup.jobD threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: org.alfresco.scripts.ScriptException: 00220017 Failed to execute script 'workspace://SpacesStore/8827eb4d-68e5-4b12-8874-367b2c52fda3': 00220016 Failed to send email to: TTuti@kemri-wellcome.org cause org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset]
        at org.quartz.core.JobRunShell.run(JobRunShell.java:227)
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:563)
Caused by: org.alfresco.scripts.ScriptException: 00220017 Failed to execute script 'workspace://SpacesStore/8827eb4d-68e5-4b12-8874-367b2c52fda3': 00220016 Failed to send email to: TTuti@kemri-wellcome.org cause org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset
        at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:254)
        at org.alfresco.repo.processor.ScriptServiceImpl.execute(ScriptServiceImpl.java:237)
        at org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:143)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
        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:46)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:161)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at com.sun.proxy.$Proxy298.executeScript(Unknown Source)
        at org.alfresco.repo.action.executer.ScriptActionExecuter.executeImpl(ScriptActionExecuter.java:164)
        at org.alfresco.repo.action.executer.ActionExecuterAbstractBase.execute(ActionExecuterAbstractBase.java:258)
        at org.alfresco.repo.action.ActionServiceImpl.directActionExecution(ActionServiceImpl.java:838)
        at org.alfresco.repo.action.ActionServiceImpl.executeActionImpl(ActionServiceImpl.java:738)
        at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:572)
        at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:558)
        at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:847)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
        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:46)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:161)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at com.sun.proxy.$Proxy42.executeAction(Unknown Source)
        at org.alfresco.repo.action.scheduled.AbstractScheduledAction$JobDefinition$1$2.execute(AbstractScheduledAction.java:567)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:447)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:318)
        at org.alfresco.repo.action.scheduled.AbstractScheduledAction$JobDefinition$1.runTransactionalAction(AbstractScheduledAction.java:557)
        at org.alfresco.repo.action.scheduled.AbstractScheduledAction$JobDefinition$1.doWork(AbstractScheduledAction.java:426)
        at org.alfresco.repo.security.authentication.AuthenticationUtil.runAs(AuthenticationUtil.java:546)
        at org.alfresco.repo.action.scheduled.AbstractScheduledAction$JobDefinition.execute(AbstractScheduledAction.java:392)
        at org.quartz.core.JobRunShell.run(JobRunShell.java:216)
        … 1 more
Caused by: org.alfresco.error.AlfrescoRuntimeException: 00220016 Failed to send email to: TTuti@kemri-wellcome.org cause org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset
        at org.alfresco.repo.action.executer.MailActionExecuter.sendEmail(MailActionExecuter.java:958)
        at org.alfresco.repo.action.executer.MailActionExecuter.executeImpl(MailActionExecuter.java:490)
        at org.alfresco.repo.action.executer.ActionExecuterAbstractBase.execute(ActionExecuterAbstractBase.java:258)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.alfresco.repo.management.subsystems.SubsystemProxyFactory$1.invoke(SubsystemProxyFactory.java:72)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at com.sun.proxy.$Proxy300.execute(Unknown Source)
        at org.alfresco.repo.action.ActionServiceImpl.directActionExecution(ActionServiceImpl.java:838)
        at org.alfresco.repo.action.ActionServiceImpl.executeActionImpl(ActionServiceImpl.java:738)
        at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:572)
        at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:558)
        at org.alfresco.repo.action.ActionServiceImpl.executeAction(ActionServiceImpl.java:847)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
        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:46)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.alfresco.repo.audit.AuditMethodInterceptor.invoke(AuditMethodInterceptor.java:161)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at com.sun.proxy.$Proxy42.executeAction(Unknown Source)
        at org.alfresco.repo.jscript.ScriptAction.executeImpl(ScriptAction.java:166)
        at org.alfresco.repo.jscript.ScriptAction.execute(ScriptAction.java:136)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:155)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:243)
        at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:66)
        at org.mozilla.javascript.gen.c14._c0(workspace://SpacesStore/8827eb4d-68e5-4b12-8874-367b2c52fda3:46)
        at org.mozilla.javascript.gen.c14.call(workspace://SpacesStore/8827eb4d-68e5-4b12-8874-367b2c52fda3)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
        at org.mozilla.javascript.gen.c14.call(workspace://SpacesStore/8827eb4d-68e5-4b12-8874-367b2c52fda3)
        at org.mozilla.javascript.gen.c14.exec(workspace://SpacesStore/8827eb4d-68e5-4b12-8874-367b2c52fda3)
        at org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:502)
        at org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:250)
        … 52 more
Caused by: org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset; message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:400)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)
        at org.alfresco.repo.action.executer.MailActionExecuter.sendEmail(MailActionExecuter.java:929)
        … 101 more
Caused by: javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketException: Connection reset
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
        at javax.mail.Service.connect(Service.java:275)
        at org.alfresco.repo.mail.AlfrescoJavaMailSender$TransportFactory.makeObject(AlfrescoJavaMailSender.java:71)
        at org.apache.commons.pool.impl.GenericKeyedObjectPool.borrowObject(GenericKeyedObjectPool.java:1190)
        at org.alfresco.repo.mail.AlfrescoJavaMailSender$PooledTransportWrapper.protocolConnect(AlfrescoJavaMailSender.java:205)
        at javax.mail.Service.connect(Service.java:275)
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
        … 104 more
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:189)
        at java.net.SocketInputStream.read(SocketInputStream.java:121)
        at sun.security.ssl.InputRecord.readFully(InputRecord.java:442)
        at sun.security.ssl.InputRecord.read(InputRecord.java:480)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
        at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:882)
        at sun.security.ssl.AppInputStream.read(AppInputStream.java:102)
        at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
        at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:75)
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1440)
        … 112 more


<em><strong>The scheduled cron task is set in var/lib/tomcat7/shared/classes/alfresco/extension/scheduled-action-services-context.xml and it looks like the following</em></strong>



<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>     <!–
    Define the model factory used to generate object models suitable for use with freemarker templates.
    –>
    <bean id="templateActionModelFactory" class="org.alfresco.repo.action.scheduled.FreeMarkerWithLuceneExtensionsModelFactory">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry"/>
        </property>
    </bean>

            <!–
    Execute the script /Company Home/Record Management/testscript.js
    –>
    <bean id="runScriptAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
        <property name="actionName">
            <value>script</value>
        </property>
        <property name="parameterTemplates">
            <map>
                <entry>
                    <key>
                        <value>script-ref</value>
                    </key>
                    <!– Note that as of Alfresco 4.0, due to a  Spring upgrade, the FreeMarker ${foo} entries must be escaped –>
                    <value>\$\{selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:calendar_notify.js"' )\}</value>
                </entry>
            </map>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="dictionaryService">
            <ref bean="DictionaryService"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
    </bean>

    <!–
   Run the script every minute - select the single node company home that is not used …
    –>
    <bean id="runScript" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">
        <property name="transactionMode">
            <value>UNTIL_FIRST_FAILURE</value>
        </property>
        <property name="compensatingActionMode">
            <value>IGNORE</value>
        </property>
        <property name="searchService">
            <ref bean="SearchService"/>
        </property>
        <property name="templateService">
            <ref bean="TemplateService"/>
        </property>
        <property name="queryLanguage">
            <value>lucene</value>
        </property>
        <property name="stores">
            <list>
                <value>workspace://SpacesStore</value>
            </list>
        </property>
        <property name="queryTemplate">
            <value>+PATH:"/app:company_home/st:sites//*/cm:calendar//*" AND +@ia\:fromDate:[NOW TO MAX]</value>
        </property>
        <property name="cronExpression">
            <value>0 0/3 * * * ?</value>
        </property>
        <property name="jobName">
            <value>jobD</value>
        </property>
        <property name="jobGroup">
            <value>jobGroup</value>
        </property>
        <property name="triggerName">
            <value>triggerD</value>
        </property>
        <property name="triggerGroup">
            <value>triggerGroup</value>
        </property>
        <property name="scheduler">
            <ref bean="schedulerFactory"/>
        </property>
        <property name="actionService">
            <ref bean="ActionService"/>
        </property>
        <property name="templateActionModelFactory">
            <ref bean="templateActionModelFactory"/>
        </property>
        <property name="templateActionDefinition">
            <ref bean="runScriptAction"/> <!– This is name of the action (bean) that gets run –>
        </property>
        <property name="transactionService">
            <ref bean="TransactionService"/>
        </property>
        <property name="runAsUser">
            <value>System</value>
        </property>
    </bean>
</beans>


<em><strong>The javascript file being called has the following syntax</em></strong>


var titoloEvento = document.properties["{http://www.alfresco.org/model/calendar}whatEvent"];
var fromDate = document.properties["{http://www.alfresco.org/model/calendar}fromDate"];

var today = new Date();

fromDate.setHours(0,0,0);
today.setHours(0,0,0);

var milli_fromDate = fromDate.getTime();
var milli_today = today.getTime();

var diff = fromDate - today;

var num_days =Math.round(diff/(86400000));

var person;
var email;

if (num_days == 1) {

      var displayPaths=document.displayPath.split("/");
      var siteName = displayPaths[3];
      var site=siteService.getSite(siteName);

     var siteMembers = site.listMembers(null,null,0);

     if(siteMembers !=null){
            for (userName in siteMembers ){

                 person = people.getPerson(userName);
                 email = person.properties["cm:email"];
                 var mail = actions.create("mail");

                 mail.parameters.html = "html";

                 mail.parameters.to = email;
       mail.parameters.template = companyhome.childByNamePath("Data Dictionary/Email Templates/Notify Email Templates/calendar_event_notify.html.ftl")

                 mail.parameters.subject = "CIN Share reminder: "+titoloEvento;
                 mail.parameters.from = "clinicalinformationnetwork@gmail.com";

                 // execute action against a document  
                 mail.execute(document);
         }
    }
}



<em><strong>The cron job does send an email immediately after deployment. It is the successive executions that produce the error message at the top. What might be causing this? FYI I have set the cron job to execute every 3 minutes for purposes of testing and the users are being pulled from an ldap-ad server</em></strong>

Outcomes