4 Replies Latest reply on Aug 22, 2017 1:45 PM by Rodrigo Guerrero

    Attach an uploaded document to an email in email task node jBPM 6.1.0 CR1.

    Eric Allen Newbie

      Hi all,

       

      I just started working with jBPM 6.1 and have a small process working that takes in a document that can be downloaded in a later task. What I want to do now is attach that document to an automated email sent from my custom gmail work item handler. Any suggestions?

       

      Here is the code for my email class

       

      import javax.activation.DataHandler;

      import javax.activation.DataSource;

      import javax.activation.FileDataSource;

      import javax.mail.Message;

      import javax.mail.MessagingException;

      import javax.mail.Session;

      import javax.mail.Transport;

      import javax.mail.internet.AddressException;

      import javax.mail.internet.InternetAddress;

      import javax.mail.internet.MimeBodyPart;

      import javax.mail.internet.MimeMessage;

      import javax.mail.internet.MimeMultipart;

       

       

      import org.drools.core.common.DroolsObjectInputStream;

      import org.jbpm.document.Document;

      import org.jbpm.document.service.DocumentStorageService;

       

       

      public class email {

       

       

          private static String USER_NAME = "**********@gmail.com";

          private static String PASSWORD = "***********"; // GMail password

          private static String RECIPIENT = "recipient@email.com";

       

       

          public email(String[] to, String subject, String body, Document attachment) {

              String from = USER_NAME;

              String pass = PASSWORD;

              //String[] to = { RECIPIENT }; // list of recipient email addresses

             // String subject = "Java send mail example";

             // String body = "Welcome to JavaMail!";

              System.out.println(to[0] + " " + subject + " " + body);

            

             sendFromGMail(from, pass, to, subject, body, attachment);

          }

       

       

          private static void sendFromGMail(String from, String pass, String[] to, String subject, String body, Document attachment) {

              Properties props = System.getProperties();

              String host = "smtp.gmail.com";

              props.put("mail.smtp.starttls.enable", "true");

              props.put("mail.smtp.host", host);

              props.put("mail.smtp.user", from);

              props.put("mail.smtp.password", pass);

              props.put("mail.smtp.port", "587");

              props.put("mail.smtp.auth", "true");

       

       

              Session session = Session.getDefaultInstance(props);

              MimeMessage message = new MimeMessage(session);

       

       

              try {

                  message.setFrom(new InternetAddress(from));

                  InternetAddress[] toAddress = new InternetAddress[to.length];

       

       

                  // To get the array of addresses

                  for( int i = 0; i < to.length; i++ ) {

                      toAddress[i] = new InternetAddress(to[i]);

                  }

       

       

                  for( int i = 0; i < toAddress.length; i++) {

                      message.addRecipient(Message.RecipientType.TO, toAddress[i]);

                  }

       

       

                  message.setSubject(subject);

                  message.setText(body);

                

                  MimeMultipart multiPart = new MimeMultipart();

                  MimeBodyPart messageBodyPart = new MimeBodyPart();

                

                  DataSource source = new FileDataSource(attachment.getLink());

                  String fileName = "Resume";

                  messageBodyPart.setDataHandler(new DataHandler(source));

                  messageBodyPart.setFileName(fileName);

                  multiPart.addBodyPart(messageBodyPart);

                

                  Transport transport = session.getTransport("smtp");

                  transport.connect(host, from, pass);

                  transport.sendMessage(message, message.getAllRecipients());

                  transport.close();

              }

              catch (AddressException ae) {

                  ae.printStackTrace();

              }

              catch (MessagingException me) {

                  me.printStackTrace();

              }

          }

      }

       

      Thanks,

       

      Eric

        • 1. Re: Attach an uploaded document to an email in email task node jBPM 6.1.0 CR1.
          Maciej Swiderski Master

          take a look at the code that jBPM uses by default for sending emails and it has attachment support. It simply requires to have a valid URL (with any supported scheme e.g. file://) to find and attach files to an email.

           

          HTH

          1 of 1 people found this helpful
          • 2. Re: Attach an uploaded document to an email in email task node jBPM 6.1.0 CR1.
            Eric Allen Newbie

            I have my email service working correctly, and it will attach files from the URL I input but when trying to attach an uploaded document (org.jbpm.document.Document) using the document's getLink() method it attaches a CSS/HTML document instead. That document looks like this:

             

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

            <html xmlns="http://www.w3.org/1999/xhtml">

            <head>

                <title>JBoss BPM Suite 6</title>

             

             

                <style type="text/css">

                    * {

                        font-family: Helvetica, Arial, sans-serif;

                    }

             

             

                    body {

                        margin: 0;

                        pading: 0;

                        color: #fff;

                        background: url('/jbpm-console/images/bg-login.png') repeat #1b1b1b;

                        font-size: 14px;

                        text-shadow: #050505 0 -1px 0;

                        font-weight: bold;

                    }

             

             

                    li {

                        list-style: none;

                    }

             

             

                    #dummy {

                        position: absolute;

                        top: 0;

                        left: 0;

                        border-bottom: solid 3px #777973;

                        height: 250px;

                        width: 100%;

                        background: url('/jbpm-console/images/bg-login-top.png') repeat #fff;

                        z-index: 1;

                    }

             

             

                    #dummy2 {

                        position: absolute;

                        top: 0;

                        left: 0;

                        border-bottom: solid 2px #545551;

                        height: 252px;

                        width: 100%;

                        background: transparent;

                        z-index: 2;

                    }

             

             

                    #login-wrapper {

                        margin: 0 0 0 -160px;

                        width: 370px;

                        text-align: center;

                        z-index: 99;

                        position: absolute;

                        top: 0;

                        left: 50%;

                    }

             

             

                    #login-top {

                        height: 120px;

                        width: 401px;

                        padding-top: 20px;

                        text-align: center;

                    }

             

             

                    #login-content {

                        margin-top: 120px;

                    }

             

             

                    label {

                        width: 70px;

                        float: left;

                        padding: 8px;

                        line-height: 14px;

                        margin-top: -4px;

                    }

             

             

                    input.text-input {

                        width: 200px;

                        float: right;

                        -moz-border-radius: 4px;

                        -webkit-border-radius: 4px;

                        border-radius: 4px;

                        background: #fff;

                        border: solid 1px transparent;

                        color: #555;

                        padding: 8px;

                        font-size: 13px;

                    }

             

             

                    input.button {

                        float: right;

                        padding: 6px 10px;

                        color: #fff;

                        font-size: 14px;

                        background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#a4d04a), to(#459300));

                        text-shadow: #050505 0 -1px 0;

                        background-color: #459300;

                        -moz-border-radius: 4px;

                        -webkit-border-radius: 4px;

                        border-radius: 4px;

                        border: solid 1px transparent;

                        font-weight: bold;

                        cursor: pointer;

                        letter-spacing: 1px;

                    }

             

             

                    input.button:hover {

                        background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#a4d04a), to(#a4d04a), color-stop(80%, #76b226));

                        text-shadow: #050505 0 -1px 2px;

                        background-color: #a4d04a;

                        color: #fff;

                    }

             

             

                    div.error {

                        padding: 8px;

                        background: rgba(52, 4, 0, 0.4);

                        -moz-border-radius: 8px;

                        -webkit-border-radius: 8px;

                        border-radius: 8px;

                        border: solid 1px transparent;

                        margin: 6px 0;

                    }

                </style>

            </head>

             

             

            <body id="login">

             

             

            <div id="login-wrapper" class="png_bg">

                <div id="login-top">

                    <img src="/jbpm-console/images/kie-ide.png" alt="KIE IDE Logo" title="Powered By Drools/jBPM"/>

                </div>

             

             

                <div id="login-content">

                   

                    <form action="j_security_check" method="POST">

                        <p>

                            <label style="white-space: nowrap;">Username</label>

                            <input value="" name="j_username" class="text-input" type="text"/>

                        </p>

                        <br style="clear: both;"/>

             

             

                        <p>

                            <label style="white-space: nowrap;">Password</label>

                            <input name="j_password" class="text-input" type="password"/>

                        </p>

                        <br style="clear: both;"/>

             

             

                        <p>

                            <input class="button" type="submit" value='Sign In'/>

                        </p>

             

             

                    </form>

                </div>

            </div>

            <div id="dummy"></div>

            <div id="dummy2"></div>

            </body>

            </html>

             

             

             

             

             

            What I want is to be able to upload a document in one task and then have that document attached to the automated email that is sent in the next task. Is it possible to make the document marshaller save the document to file so I can access it directly from there?

             

            Eric

            • 3. Re: Attach an uploaded document to an email in email task node jBPM 6.1.0 CR1.
              Maciej Swiderski Master

              this is caused by the demo implementation of the storage service that uses form modeler to fetch the document - it's focusing only on UI access. But since you have the Document instance already available you can get the content directly out of it so no need to use URL for it. Attach the content directly to the email and you should be good to go with that setup.

               

              HTH

              • 4. Re: Attach an uploaded document to an email in email task node jBPM 6.1.0 CR1.
                Rodrigo Guerrero Newbie

                Hi Maciej,

                 

                I am trying to attach an document (org.jbpm.document.Document), but my document is stored locally, in the same server. So, I am getting a different document whit this code:

                 

                 

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

                <html xmlns="http://www.w3.org/1999/xhtml">

                <head> 

                  <link rel="stylesheet" href="styles/base.css">

                </head>

                 

                <body id="login">

                 

                <div id="pfly-login-screen">

                 

                <img id="logo-ucuenca" src="images/logo-ucuenca.png" alt="Logo" style="left: 64px;position: absolute;top: 50px;">

                  <img id="logo" src="images/kie-ide.png" alt="KIE IDE Logo" title="Powered By Drools/jBPM"/>

                   

                  <div id="login-wrapper" class="png_bg"> 

                    <div id="login-top">     

                    </div>

                 

                    <div id="login-content" class="png_bg">     

                      <form action="j_security_check?locale=null" method="POST">

                        <fieldset>

                         

                          <label style="font-weight: bold !important;font-size: larger;">Username</label><input value="" name="j_username" class="text-input" type="text" autofocus/>

                          <br style="clear: both;"/>

                          <label style="font-weight: bold !important;font-size: larger;">Password</label><input name="j_password" class="text-input" type="password"/>

                          <br style="clear: both;"/>         

                         

                          <input class="button login" type="submit" value='Sign In'/>         

                        </fieldset>

                      </form>

                    </div>

                  </div>

                </div>

                </body>

                </html>

                 

                 

                 

                I think that this is a authentication problem, maybe you know how to solve it?