Custom Object Example: A Transport for the Shoulder Tap Delivery Method

The following custom object provides a sample transport that supports email-gateway SMS for the Shoulder Tap delivery method. This custom object sends e-mail to the asset property value “Email”.

****************************************************************************************************

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import java.util.Properties;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import com.axeda.drm.sdk.device.DevicePropertyFinder;

import com.axeda.drm.sdk.device.PropertyType;

import com.axeda.drm.sdk.device.DeviceProperty;

import com.axeda.drm.sdk.device.Property;

 

logger.info("Reading Email property......");

def sdkcontext = context.context

def propValue

DevicePropertyFinder dpf = new DevicePropertyFinder(sdkcontext);

dpf.setType(PropertyType.DEVICE_TYPE);

dpf.setId(context.device.getId());

DeviceProperty prop = dpf.findOne();

List<Property> props = prop.getProperties();

for (Property prop1: props )

{

  if (prop1.name.equalsIgnoreCase("Email"))

  {

    propValue = prop1.getValue();

    logger.info("property : "+prop1.getValue());

  }

}

if (propValue == null)

{

logger.error("No property Email found for the device");

return false;

}

logger.info("Preparing to send SMS......");

boolean result;

try

{

  Properties properties = new Properties();

  properties.put("mail.smtp.host", "mail-server-name");

  Session session = Session.getDefaultInstance(properties, null);

  System.out.println("session : " + session);

  MimeMessage message = new MimeMessage(session);

  message.addRecipient(Message.RecipientType.TO, new InternetAddress(propValue));

  message.setFrom(new InternetAddress("admin@axeda.com"));

  message.setSubject("Testing SMS");

  message.setText("SMS Message");

  Transport.send(message);

  logger.info("SMS send");

  result = true;

}

catch(MessagingException ex)

{

  logger.error("Cannot send email. " + ex);

  result = false;

}

return result;

****************************************************************************************************