Axeda: Email File From Data Accumulator

    This code snippet finds an uploaded file associated with an asset and emails it to a destination email address.  It uses a data accumulator to create a temporary file.


    import org.apache.commons.codec.binary.Base64;
    import java.util.Date;
    import java.util.Properties;
    import com.axeda.drm.sdk.Context
    import com.axeda.drm.sdk.device.*
    import groovy.json.JsonSlurper
    import javax.activation.DataHandler;
    import javax.activation.FileDataSource;
    import org.apache.axiom.attachments.ByteArrayDataSource;
    import com.thoughtworks.xstream.XStream;
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.PasswordAuthentication;
    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;
    try {
        Context ctx = Context.create(parameters.username)
        DeviceFinder dfinder = new DeviceFinder(ctx)
        def bytes
        Device d = dfinder.find()
        UploadedFileFinder uff = new UploadedFileFinder(ctx)
        uff.device = d
        def ufiles = uff.findAll()
        UploadedFile ufile
        if (ufiles.size() > 0) {
            ufile = ufiles[0]
            File f = ufile.extractFile()
            def slurper = new JsonSlurper()
            def objects = slurper.parseText(f.getText())
            def bugreport = objects.objects[0].mobj_update[0].bugreport
            String from = "";
            String to = "";
            String subject = "My file";
            String mailContent = "Attaching test";
            String filename = "payload.tar.gz";
            def dataStoreIdentifier = "FILE-IO-SUB-testing"
            def daSvc = new ServiceFactory().dataAccumulatorService
            if (daSvc.doesAccumulationExist(dataStoreIdentifier, {
            daSvc.writeChunk(dataStoreIdentifier,, bugreport);
            InputStream is = daSvc.streamAccumulation(dataStoreIdentifier,
            Base64 base64 = new Base64()
            ByteArrayDataSource rawData = new ByteArrayDataSource(base64.decodeBase64(is.getBytes()));
            // You need to create a properties object to store mail server
            // smtp information such as the host name and the port number.
            // With this properties we create a Session object from
            // which we'll create the Message object.
            Properties properties = new Properties();
            properties.put("mail.smtp.port", "25");
            properties.put("mail.smtp.auth", "true");
            Authenticator authenticator = new CustomAuthenticator();
            Session session = Session.getInstance(properties, authenticator);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSentDate(new Date());
            // Set the email message text.
            MimeBodyPart messagePart = new MimeBodyPart();
            // Set the email attachment file
            MimeBodyPart attachmentPart = new MimeBodyPart();
            //      FileDataSource fileDataSource = new FileDataSource(file)
            attachmentPart.setDataHandler(new DataHandler(rawData))  //fileDataSource));
            Multipart multipart = new MimeMultipart();
            // Set the content
            // Send the message with attachment
    catch (Exception e) {
        StringWriter logStringWriter = new StringWriter();
        PrintWriter logPrintWriter = new PrintWriter(logStringWriter)
    // This class is the implementation of the Authenticator
    // Where you need to implement the getPasswordAuthentication
    // to provide the username and password
    public class CustomAuthenticator extends Authenticator {
        protected PasswordAuthentication getPasswordAuthentication() {
            String username = "";
            String password = "";
            return new PasswordAuthentication(username, password);
    static byte[] getBytes(File file) throws IOException {
        return getBytes(new FileInputStream(file));
    static byte[] getBytes(InputStream is) throws IOException {
        ByteArrayOutputStream answer = new ByteArrayOutputStream();
    // reading the content of the file within a byte buffer
        byte[] byteBuffer = new byte[8192];
        int nbByteRead /* = 0*/;
        try {
            while ((nbByteRead = != -1) {
    // appends buffer
                answer.write(byteBuffer, 0, nbByteRead);
        } finally {
        return answer.toByteArray();