1 10 11 package org.mmbase.applications.email; 12 13 import java.util.*; 14 15 import javax.mail.MessagingException ; 16 import javax.mail.internet.*; 17 18 19 import org.mmbase.util.logging.Logging; 20 import org.mmbase.util.logging.Logger; 21 22 27 public class MimeMessageGenerator { 28 29 private static final Logger log = Logging.getLoggerInstance(MimeMessageGenerator.class); 30 31 32 33 36 public static MimeMultipart getMimeMultipart(String text) { 37 38 Hashtable nodes = new Hashtable(); 39 Vector rootnodes = new Vector(); 40 41 42 Enumeration tags=MimeBodyTagger.getMimeBodyParts(text); 43 while (tags.hasMoreElements()) { 44 try { 45 MimeBodyTag tag=(MimeBodyTag)tags.nextElement(); 46 47 String type=tag.getType(); 49 String id=tag.getId(); 50 String related=tag.getRelated(); 51 String alt=tag.getAlt(); 52 53 nodes.put(id,tag); 55 56 if (alt==null && related==null) { 58 rootnodes.addElement(tag); 59 } else if (alt!=null) { 60 MimeBodyTag oldpart=(MimeBodyTag)nodes.get(alt); 61 if (oldpart!=null) { 62 oldpart.addAlt(tag); 63 } 64 } else if (related!=null) { 65 MimeBodyTag oldpart=(MimeBodyTag)nodes.get(related); 66 if (oldpart!=null) { 67 oldpart.addRelated(tag); 68 } 69 } 70 71 } catch(Exception e) { 72 log.error("Mime mail error"); 73 } 74 } 75 76 if (rootnodes.size()==1) { 77 MimeBodyTag t=(MimeBodyTag)rootnodes.elementAt(0); 78 MimeMultipart mmp=t.getMimeMultipart(); 79 if (mmp!=null) { 80 return(mmp); 81 } 82 } else { 83 if (rootnodes.size()>1) { 84 try { 85 MimeMultipart root = new MimeMultipart(); 86 root.setSubType("mixed"); 87 Enumeration l = rootnodes.elements(); 88 while (l.hasMoreElements()) { 89 MimeBodyTag t = (MimeBodyTag) l.nextElement(); 90 MimeMultipart mmp = t.getMimeMultipart(); 91 if (mmp != null) { 92 log.info("setting parent info : " + t.getId()); 93 MimeBodyPart wrapper = new MimeBodyPart(); 94 wrapper.setContent(mmp); 95 root.addBodyPart(wrapper); 96 } 97 else { 98 log.info("adding info : " + t.getId()); 99 root.addBodyPart(t.getMimeBodyPart()); 100 } 101 } 102 return root; 103 } catch (MessagingException e) { 104 log.error("Root generation error" + e.getMessage()); 105 } 106 } else { 107 log.error("Don't have a root node"); 108 } 109 } 110 return null; 111 } 112 113 } 114 | Popular Tags |