import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.Map.Entry; import com.adeptia.ReceiveMail; import com.adeptia.indigo.TransactionExecutor; import com.adeptia.indigo.event.EventUtils; import com.adeptia.indigo.logging.Logger; import org.apache.commons.io.IOUtils; // TODO Auto-generated method stub String processFlowId = "192168001143157924548459300011"; //Id of process flow to be triggered String eventId = "192168001143157926019565100005"; //Id of complex event String eventName = "Mail_Attachment"; //Name of complex event String hostName = "imap.gmail.com"; //Name of Mail server String senderEmailId = "dummy@gmail.com"; //Sender email id String emailSubject = ""; // email subject String fileSearchCriteria = "*.*"; //Name of Attachment String receiverEmailId = "dummy@adeptia.com"; //Receiver email id String password = "adeptia123"; // Receiver password String protocol = "IMAP"; //Mail protocol int port = 993; // Mail port String tempFolderLocation = "C:\\Users\\Rahul\\Desktop\\Source\\"; //Location where Attachments are Stored and then Pick by File Source String delim= ""; try { Map hashMap = new HashMap(); Map eventContextMap = new HashMap(); ReceiveMail receiveMail = new ReceiveMail(); TransactionExecutor transactionExecutor = new TransactionExecutor(); StringBuilder rString = new StringBuilder(); eventId = "ComplexEvent:" + eventId; hashMap.put("EventID", eventId); hashMap.put("EventName", eventName); List msgList = receiveMail.receiveEmail(protocol, hostName, null, null, true, receiverEmailId, password, port, null, true); Map list = receiveMail.receiveMail(msgList, senderEmailId, emailSubject, "Multipart", fileSearchCriteria, true, "", null, "text/plain", true); if(list != null){ Iterator itr2 = list.keySet().iterator(); Iterator itr3 = list.values().iterator(); eventContextMap.put(EventUtils.EVENTCONTEXTVAR, hashMap); List fileNameList = new ArrayList(); while(itr2.hasNext()){ String key = itr2.next(); rString.append(delim).append(key); delim = ","; fileNameList.add(key); } InputStream inputStream; File attachmentFile; int index = 0; byte[] bytes; OutputStream outStream; int read; while(itr3.hasNext()) { inputStream = (InputStream)itr3.next(); attachmentFile = new File(tempFolderLocation+fileNameList.get(index)); try { outStream = new FileOutputStream(attachmentFile); bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outStream.write(bytes, 0, read); } }catch(Exception e) { e.printStackTrace(); }finally { if(outStream!=null) { outStream.flush(); outStream.close(); } if(inputStream!=null) { //inputStream.close(); } } index+=1; } hashMap.put("fileList", rString); hashMap.put("listSize", list.size()); transactionExecutor.executeTransactionWithTransID(eventContextMap, processFlowId, subject, false); receiveMail.close(); } } catch (Exception e) { // TODO: handle exception Logger.getLogger("Event").error( "Error while reading mail :: " + e.getMessage(), e); throw e; }