Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X
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 java.io.StringWriter
import java.io.PrintWriter
import com.axeda.drm.sdk.Context
import com.axeda.drm.sdk.data.*
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.axeda.platform.sdk.v1.services.ServiceFactory;
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
dfinder.setSerialNumber(parameters.serial_number)
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 = "demo@axeda.com";
String to = "destination@axeda.com";
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, d.id.value)) {
daSvc.deleteAccumulation(dataStoreIdentifier, d.id.value)
}
daSvc.writeChunk(dataStoreIdentifier, d.id.value, bugreport);
InputStream is = daSvc.streamAccumulation(dataStoreIdentifier, d.id.value)
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.host","mail01.bo2.axeda.com");
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.setSubject(subject);
message.setSentDate(new Date());
// Set the email message text.
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(mailContent);
// Set the email attachment file
MimeBodyPart attachmentPart = new MimeBodyPart();
// FileDataSource fileDataSource = new FileDataSource(file)
attachmentPart.setDataHandler(new DataHandler(rawData)) //fileDataSource));
attachmentPart.setFileName(filename);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
// Set the content
message.setContent(multipart);
// Send the message with attachment
Transport.send(message);
}
}
catch (Exception e) {
logger.info(e.message)
StringWriter logStringWriter = new StringWriter();
PrintWriter logPrintWriter = new PrintWriter(logStringWriter)
e.printStackTrace(logPrintWriter)
logger.info(logStringWriter.toString())
}
// 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 = is.read(byteBuffer)) != -1) {
// appends buffer
answer.write(byteBuffer, 0, nbByteRead);
}
} finally {
is.close()
}
return answer.toByteArray();
}