Attach an uploaded document to an email in email task node jBPM 6.1.0 CR1.
ericallen13 Jul 3, 2014 3:45 PMHi 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