cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Axeda: Emailing an Attachment

Level 5

Axeda: Emailing an Attachment

Email an attachment using bytes from a FileInfo

Parameters:

  1. fileId - the identifier to a FileInfo that has been previously uploaded to the FileStore
  2. filename - the name of the attachment
  3. toaddress - the email address to send to
  4. fromaddress - the email address to send from

import com.axeda.drm.util.Emailer;

import com.axeda.drm.sdk.contact.Email

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import static com.axeda.sdk.v2.dsl.Bridges.*

import com.axeda.services.v2.FileInfoCriteria

import org.apache.commons.io.IOUtils

import java.security.MessageDigest

try {

  String fromaddress = parameters.fromaddress

  String toaddress = parameters.toaddress

  def fileId = parameters.fileId

  def filename = parameters.filename

  String subject = "Axeda Test Attachment"

  String body = "<html><head/><body><p style='background:blue;'>This email has an attachment and a blue background.</p></body></html>"

  def thefile = new File(filename)

  def inputStream = fileInfoBridge.getFileData(fileId)

  byte[] bytes = IOUtils.toByteArray(inputStream);

  thefile.setBytes(bytes)

  def random_hash = md5('r');

  def contentType = "multipart/mixed; boundary=--\"$random_hash\"\r\n"

  def htmlType = "text/html"

sendEmail(fromaddress, toaddress, subject,  body, contentType, thefile, false, htmlType)

}

catch (Exception e) {

logger.error(e.localizedMessage)

}

return true

def md5(String s) {

    MessageDigest digest = MessageDigest.getInstance("MD5")

    digest.update(s.bytes);

    new BigInteger(1, digest.digest()).toString(16).padLeft(32, '0')

}

public void sendEmail(String fromAddress, String toAddress,String subject, String body, String encoding, File file, boolean compress, String mimeType) {

    try {

        Emailer.getInstance().send([new InternetAddress(toAddress)],new InternetAddress(fromAddress), subject,body, encoding, [file] as File[], compress, mimeType);

    } catch (Exception ae) {

        logger.error(ae.localizedMessage);

    }

}

Tags (2)