Skip to content

Instantly share code, notes, and snippets.

@axeda
Last active January 9, 2019 12:40
Show Gist options
  • Save axeda/6358237 to your computer and use it in GitHub Desktop.
Save axeda/6358237 to your computer and use it in GitHub Desktop.
Emails an attachment using bytes from a FileInfo
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment