1 package org.ofbiz.content.email; 2 3 import java.util.List ; 4 import java.util.Map ; 5 import java.util.HashMap ; 6 import java.io.ByteArrayOutputStream ; 7 import java.io.ByteArrayInputStream ; 8 import java.io.InputStream ; 9 import java.io.IOException ; 10 11 import javax.mail.internet.MimeMessage ; 12 import javax.mail.internet.InternetAddress ; 13 import javax.mail.MessagingException ; 14 import javax.mail.Multipart ; 15 import javax.mail.Part ; 16 17 import org.ofbiz.entity.GenericDelegator; 18 import org.ofbiz.entity.GenericValue; 19 import org.ofbiz.entity.GenericEntityException; 20 import org.ofbiz.base.util.UtilMisc; 21 import org.ofbiz.base.util.UtilValidate; 22 import org.ofbiz.base.util.Base64; 23 import org.ofbiz.base.util.Debug; 24 import org.ofbiz.service.LocalDispatcher; 25 import org.ofbiz.service.GenericServiceException; 26 import org.ofbiz.entity.util.ByteWrapper; 27 28 public class EmailWorker { 29 30 public final static String module = EmailWorker.class.getName(); 31 32 public String getForwardedField(MimeMessage message) { 33 34 String fieldValue = null; 35 36 return fieldValue; 37 } 38 39 53 public static int addAttachmentsToCommEvent(MimeMessage message, String communicationEventId, int bodyContentIndex, LocalDispatcher dispatcher, GenericValue userLogin) 54 throws MessagingException , IOException , GenericServiceException { 55 int attachmentCount =0; 56 Map commEventMap = new HashMap (); 57 commEventMap.put("communicationEventId", communicationEventId); 58 commEventMap.put("contentTypeId", "DOCUMENT"); 59 commEventMap.put("mimeTypeId", "text/html"); 60 commEventMap.put("userLogin", userLogin); 61 String subject = message.getSubject(); 62 Map result = null; 63 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 64 65 Multipart multipart = (Multipart )message.getContent(); 66 int multipartCount = multipart.getCount(); 67 for (int i=0; i < multipartCount; i++) { 68 Part part = multipart.getBodyPart(i); 69 String thisContentTypeRaw = part.getContentType(); 70 71 int idx2 = thisContentTypeRaw.indexOf(";"); 72 if (idx2 == -1) idx2 = thisContentTypeRaw.length(); 73 String thisContentType = thisContentTypeRaw.substring(0, idx2); 74 75 String disposition = part.getDisposition(); 76 77 if (((disposition == null) && (i == 0) && (i != bodyContentIndex) && thisContentType.startsWith("text")) 79 || ((disposition != null) 80 && (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE)) 81 && (i != bodyContentIndex)) ) 82 { 83 commEventMap.put("contentName", subject + "-" + i + " " + part.getFileName()); 84 commEventMap.put("drMimeTypeId", thisContentType); 85 if (thisContentType.startsWith("text")) { 86 87 String content = (String )part.getContent(); 88 89 commEventMap.put("drDataResourceTypeId", "ELECTRONIC_TEXT"); 90 commEventMap.put("textData", content); 91 } else { 92 93 InputStream is = part.getInputStream(); 94 int c; 95 while ((c = is.read()) > -1) { 96 baos.write(c); 97 } 98 99 ByteWrapper imageData = new ByteWrapper(baos.toByteArray()); 100 int len = imageData.getLength(); 101 if (Debug.infoOn()) Debug.logInfo("imageData length: " + len, module); 102 commEventMap.put("drDataResourceName", part.getFileName()); 103 commEventMap.put("imageData", imageData); 104 commEventMap.put("drDataResourceTypeId", "IMAGE_OBJECT"); 105 commEventMap.put("_imageData_contentType", thisContentType); 106 } 107 result = dispatcher.runSync("createCommContentDataResource", commEventMap); 108 attachmentCount++; 109 } 110 } 111 112 return attachmentCount; 113 } 114 } 115 | Popular Tags |