8 Replies Latest reply on Dec 4, 2005 10:45 PM by David Sanchez

    Anyone have tips on using JavaMail in an Action class?

    David Sanchez Novice

      I'm investigating this now but any help would be great.

      (I'd be happy to trade for tips on file uploading, file downloading and implementing the real Identity login with passwods, all of which I have running.)

        • 1. Re: Anyone have tips on using JavaMail in an Action class?
          David Sanchez Novice

          So far not too bad. You need a couple of jars, mail.jar and activation.jar (this is because JavaMail has a dependancy on the JavaBeans Activation Service.)

          Being fairly new to Java and J2EE I had some classpath problems compiling and running the test program I had to see if everything was working..so be careful with that.

          After the classpath problems were solved however using the test program went smoothly. I'm guessing the Linux server I use for external exposure was already configured for sendmail, 'localhost' worked fine as the smtp host.

          I'll let you know if I run into any problems creating an action class.

          I'm anticipated importing the above two jar files into my jbpm project and designing an action class to closely model the test program I was successful with.

          • 2. Re: Anyone have tips on using JavaMail in an Action class?
            Ronald van Kuijk Master

            I've don failry similar things. It is indeed not difficult.

            I've even created an assignmenthandler that sends an email to the address of the person the task got assigned to.

            • 3. Re: Anyone have tips on using JavaMail in an Action class?
              David Sanchez Novice

              ..Finally got back to working on this. I was able to get a stand alone JavaMail java application to send mail but haven't quite gotten the Timer to successfully send one. Here is where I am now.

              processdefinition.xml

              <?xml version="1.0" encoding="UTF-8"?>
              
              <process-definition name="EmailTimerProcess">
              
               <swimlane name="compliance manager">
               <assignment expression="user(Compliance Manager)"/>
               </swimlane>
              
               <start-state name="Start Email Reminder">
               <task swimlane="compliance manager">
               <controller>
               <variable name="anything_here" mapped-name="Hit Save and Close" />
               </controller>
               <transition name="transition_to_task" to="EmailsUntilComplete"></transition>
               </task>
               </start-state>
              
               <task-node name="EmailsUntilComplete">
               <task swimlane="compliance manager">
               <controller>
               <variable name="Quick! Complete the task before the timer goes off!!" />
               </controller>
               <timer name="Email reminder" duedate="30 seconds" repeat="60 seconds">
               <action class='com.sample.action.EmailTimerActionHandler' />
               </timer>
               <transition name="transition_to_end" to="end1"></transition>
               </task>
               </task-node>
               <end-state name="end1">
               </end-state>
              </process-definition>
              

              EmailTimerActionHandler.java
              package com.sample.action;
              
              import java.util.*;
              import javax.mail.*;
              import javax.mail.internet.*;
              import org.jbpm.graph.def.ActionHandler;
              import org.jbpm.graph.exe.ExecutionContext;
              
              public class EmailTimerActionHandler implements ActionHandler {
              
               public void execute(ExecutionContext executionContext) throws Exception {
               // TODO Auto-generated method stub
               try
               {
               String smtpServer="localhost";
               String to="davidsan1@earthlink.net";
               String from="dsanchez@svits.com";
               String subject="From jBPM Action Timer";
               String body="This is the first email warning to complete task";
              
               for (int i = 0; i < 1 ; i++)
              
               send(smtpServer, to, from, subject, body);
               }
               catch (Exception ex)
               {
               System.out.println("Usage: java com.lotontech.mail.SimpleSender"
               +" smtpServer toAddress fromAddress subjectText bodyText");
               }
              
               System.exit(0);
               }
              
               /**
               * "send" method to send the message.
               */
               public static void send(String smtpServer, String to, String from
               , String subject, String body)
               {
               try
               {
               Properties props = System.getProperties();
              
               // -- Attaching to default Session, or we could start a new one --
              
               props.put("mail.smtp.host", smtpServer);
               Session session = Session.getDefaultInstance(props, null);
              
               // -- Create a new message --
               Message msg = new MimeMessage(session);
              
               // -- Set the FROM and TO fields --
               msg.setFrom(new InternetAddress(from));
               msg.setRecipients(Message.RecipientType.TO,
               InternetAddress.parse(to, false));
              
               // -- We could include CC recipients too --
               // if (cc != null)
               // msg.setRecipients(Message.RecipientType.CC
               // ,InternetAddress.parse(cc, false));
              
               // -- Set the subject and body text --
               msg.setSubject(subject);
               msg.setText(body);
              
               // -- Set some other header information --
               msg.setHeader("X-Mailer", "LOTONtechEmail");
               msg.setSentDate(new Date());
              
               // -- Send the message --
               Transport.send(msg);
              
               System.out.println("Message sent OK.");
               }
               catch (Exception ex)
               {
               ex.printStackTrace();
               }
              
               }
              
              }
              

              I get a crash when I hit the start link. Any tips would be appreciated.

              • 4. Re: Anyone have tips on using JavaMail in an Action class?
                David Sanchez Novice

                For clarification..and for those that have not done this yet. In the directory

                jbpm.3/lib
                

                I added another directory..mail and put the mail.jar in there. Forget if I made any other changes so it would show up in the directory
                jboss-4.0.3/server/jbpm/lib
                

                To be safe I also added that directory to the build.deploy.xml file in the target "build.webapp"
                Tips to get this working still appreciated.

                • 5. Re: Anyone have tips on using JavaMail in an Action class?
                  David Sanchez Novice

                  Here is a few more steps...

                  You need to change the build.deploy.xml at the target "create.jbpm.configuration" so that it does not exclude the mail-service.xml file. Mine looks like this..

                   <target name="create.jbpm.configuration" depends="delete.jbpm.configuration, build.webapp, build.service.archive, create.db" description="creates a jbpm configuration">
                   <mkdir dir="${jboss.home}/server/jbpm" />
                   <copy todir="${jboss.home}/server/jbpm">
                   <fileset dir="${jboss.home}/server/${jboss.source.configuration}">
                   <exclude name="data/**" />
                   <exclude name="log/**" />
                   <exclude name="tmp/**" />
                   <exclude name="work/**" />
                   <exclude name="jms/jbossmq-destination-service.xml" />
                   <exclude name="deploy/jboss-aop.deployer/**" />
                   <exclude name="deploy/jboss-ws4ee.sar/**" />
                   <exclude name="deploy/management/**" />
                   <exclude name="deploy/bsh-deployer.xml" />
                   <exclude name="deploy/jboss-ha-local-jdbc.rar" />
                   <exclude name="deploy/jboss-ha-xa-jdbc.rar" />
                   <exclude name="deploy/jboss-ha-local-jdbc.rar" />
                  <!--
                   <exclude name="deploy/mail*" />
                  -->
                   <exclude name="deploy/monitoring-service.xml" />
                   <exclude name="deploy/properties-service.xml" />
                   <exclude name="deploy/schedule*" />
                   <exclude name="deploy/uuid-key-generator.sar" />
                   </fileset>
                  

                  then you need to edit that file to reflect your servers configuration. BTW that file is located at [jbosshome]/servers/default/deploy. The above target copies it to [jbosshome]/servers/jbpm/deploy. Code for this coming later because it doesn't actually send the mail yet.

                  Okay, so below is my edited version of the mail-service.xml file and the code in my ActionHandler Class. It still doesn't quite work. My code is below.
                  mail-service.xml
                  <?xml version="1.0" encoding="UTF-8"?>
                  <!DOCTYPE server>
                  <!-- $Id: mail-service.xml,v 1.5.6.1 2005/06/01 06:20:43 starksm Exp $ -->
                  
                  <server>
                  
                   <!-- ==================================================================== -->
                   <!-- Mail Connection Factory -->
                   <!-- ==================================================================== -->
                  
                   <classpath
                   codebase="lib"
                   archives="mail.jar, activation.jar, mail-plugin.jar" />
                  
                  
                   <mbean code="org.jboss.mail.MailService"
                   name="jboss:service=Mail">
                   <attribute name="JNDIName">java:/Mail</attribute>
                   <attribute name="User">[your email server user name</attribute>
                   <attribute name="Password">[your email server password</attribute>
                   <attribute name="Configuration">
                   <!-- Test -->
                   <configuration>
                   <!-- Change to your mail server prototocol -->
                   <property name="mail.store.protocol" value="pop3"/>
                   <property name="mail.transport.protocol" value="smtp"/>
                  
                   <!-- Change to the user who will receive mail -->
                   <property name="mail.user" value="nobody"/>
                  
                   <!-- Change to the mail server -->
                   <property name="mail.pop3.host" value="localhost"/>
                  
                   <!-- Change to the SMTP gateway server -->
                   <property name="mail.smtp.host" value="localhost"/>
                  
                   <!-- Change to the address mail will be from -->
                   <property name="mail.from" value="davidsan1@earthlink.net"/>
                  
                   <!-- Enable debugging output from the javamail classes -->
                   <property name="mail.debug" value="true"/>
                   </configuration>
                   <depends>jboss:service=Naming</depends>
                   </attribute>
                   </mbean>
                  
                  </server>
                  

                  Here is the code in my Action Class
                  package com.sample.action;
                  
                  import java.util.*;
                  import javax.mail.*;
                  import javax.mail.internet.*;
                  import org.jbpm.graph.def.ActionHandler;
                  import org.jbpm.graph.exe.ExecutionContext;
                  
                  public class EmailTimerActionHandler implements ActionHandler {
                  
                   public void execute(ExecutionContext executionContext) throws Exception {
                   // TODO Auto-generated method stub
                   try
                   {
                   String smtpServer="localhost";
                   String to="davidsan1@earthlink.net";
                   String from="dsanchez@svits.com";
                   String subject="From jBPM Action Timer";
                   String body="This is the first email warning to complete task";
                  
                   for (int i = 0; i < 1 ; i++)
                  
                   send(smtpServer, to, from, subject, body);
                   }
                   catch (Exception ex)
                   {
                   System.out.println("Usage: java com.lotontech.mail.SimpleSender"
                   +" smtpServer toAddress fromAddress subjectText bodyText");
                   }
                  
                   System.exit(0);
                   }
                  
                   /**
                   * "send" method to send the message.
                   */
                   public static void send(String smtpServer, String to, String from
                   , String subject, String body)
                   {
                   try
                   {
                   Properties props = System.getProperties();
                  
                   // -- Attaching to default Session, or we could start a new one --
                  
                   props.put("mail.smtp.host", smtpServer);
                   Session session = Session.getDefaultInstance(props, null);
                  
                   // -- Create a new message --
                   Message msg = new MimeMessage(session);
                  
                   // -- Set the FROM and TO fields --
                   msg.setFrom(new InternetAddress(from));
                   msg.setRecipients(Message.RecipientType.TO,
                   InternetAddress.parse(to, false));
                  
                   // -- We could include CC recipients too --
                   // if (cc != null)
                   // msg.setRecipients(Message.RecipientType.CC
                   // ,InternetAddress.parse(cc, false));
                  
                   // -- Set the subject and body text --
                   msg.setSubject(subject);
                   msg.setText(body);
                  
                   // -- Set some other header information --
                   msg.setHeader("X-Mailer", "LOTONtechEmail");
                   msg.setSentDate(new Date());
                  
                   // -- Send the message --
                   Transport.send(msg);
                  
                   System.out.println("Message sent OK.");
                   }
                   catch (Exception ex)
                   {
                   ex.printStackTrace();
                   }
                  
                   }
                  
                  }
                  

                  So this all compiles, launches and the timer seems to execute the ActionClass, however I still don't get the email. Help!

                  Tom, Ronald, Koen, Alex. I'm offering to write this all up if you guys can help me get to the finish line. I have some time before starting another consulting assignment.

                  • 6. Re: Anyone have tips on using JavaMail in an Action class?
                    Ronald van Kuijk Master

                    System.exit(0); ?????

                    Doesn't your server get stopped before it gets to sending the mail? or is jboss 'catching' this exit and still continuous?

                    In another post I put my small example which works in real life, although not from a timer.

                    The way you do it, you do not need the mail-service.xml. You, just like me in my example, fill the properties yourself.

                    • 8. Re: Anyone have tips on using JavaMail in an Action class?
                      David Sanchez Novice

                      ****BEWARE of JavaMail in JBoss and jBPM*****

                      Amending my instructions above, DO NOT add the mail.jar and activation.jar to the jbpm.sar or jbpm.war. I believe just adding to JBOSS_HOME/server/lib is enough.

                      Problem stems from having access to more than one mail.jar on the path. You get an extremely misleading message saying "javax.mail.NoSuchProviderException: smtp"

                      This can mean either:
                      1) There are NO mail.jar (and activation.jar) in the CLASSPATH
                      2) There are MORE THAN ONE mail.jar (and activation.jar) in the CLASSPATH
                      3) Faulty permissions on mail.jar (and activation.jar) .