HowTo:Handle Incoming Emails Programmatically
From LongJump Support Wiki
Revision as of 02:34, 18 January 2012 by imported>Aeric
The sample class below logs an incoming email message in an EmailLog object (an object that needs to be created for the purpose). In addition to processing the messages, it handles any incoming attachments. It uses each attachment's content type (MIME type) to determine if the attached file contains an image.
Learn more:
package com.platform.demo.mail; import com.platform.api.mail.*; import com.platform.api.*; import com.platform.beans.*; import com.platform.api.utility.Base64Codec; // For decoding attachments /** * Handle an incoming email message by creating a record. * * Prerequisite: An EmailLog Object in the platform with the following fields: * <pre> * Field Type Description * -------- ------ --------------------- * to_list String List of addresses * from_addr String Sender's email address * subject String Message heading * body String Message content * file_1 File As many fields as needed for the maximum * ... number of attachments you expect to process. * file_N * image_1 Image Ditto. * image_N * </pre> * Note: * Fields of type Image could also be used for attachments. The difference * in the object is that an image (or its thumbnail) is displayed when the * record is displayed, whereas a file isn't. */ public class SampleEmailHandler implements com.platform.api.mail.EmailHandler { public void processEmail(Email email) { try { // Log the incoming message Functions.debug ("In: " + email.toString() ); // Store email data as parameters for the record we'll create. // (All components of an email are available.) Parameters params = Functions.getParametersInstance(); params.add("to_list", email.getToAddresses()); params.add("from_addr", email.getFromAddress()); params.add("subject", email.getSubject()); params.add("body", email.getPlainTextBody()); com.platform.api.mail.Attachment[] attachments = email.getAttachments(); int file_num = 1; int image_num = 1; Base64Codec codec = new Base64Codec(); for (int i = 0 ; i < attachments.length; i++) { // Process attachments // Note: Attachments without corresponding fields are ignored. // Throw an exception (or least log an message) if the number // of attachments exceeds the number of fields defined for them. String filename = attachments[i].getFileName(); // Deterine the field name to use for the attachment. // Here, we assume that for a MIME type like "image/jpg", // the value is stored in an Image field. String mimetype = attachments[i].getContentType(); String fieldname = ""; if (mimetype.startsWith("image/")) { fieldname = "image_" + (image_num++); } else { fieldname = "file_" + (file_num++); } // Decode each attachment & save it as a record parameter. byte[] body = codec.decode(attachments[i].getBody()); PlatformFileBean filebean = new PlatformFileBean("", filename, new String(body)); params.add(fieldname, filebean); } // Create a new record from the email. // (Do any processing you like, here.) Result result = Functions.addRecord("EmailLog", params); // Verify the add succeeded. Log any errors. if (result.getCode() == 0) { String record_id = result.getID(); Functions.debug("Record Id: " + record_id); } else { Functions.debug("Message: " + result.getMessage()); } } catch (Exception e) { Functions.debug("Exception:" + e.getMessage()); } } }