1 8 9 package org.enhydra.oyster.mail; 10 11 import org.enhydra.oyster.exception.SMIMEException; 12 import org.enhydra.oyster.activation.StreamDataSource; 13 import javax.mail.internet.MimeBodyPart ; 14 import javax.mail.internet.MimeMultipart ; 15 import org.enhydra.oyster.util.HtmlAnalyzer; 16 import java.io.File ; 17 import java.io.InputStream ; 18 import java.util.Vector ; 19 import javax.activation.DataHandler ; 20 import javax.activation.FileDataSource ; 21 22 26 public class MultipartGenerator 27 { 28 29 40 public static MimeMultipart getHtmlMultipart(InputStream content0) 41 throws SMIMEException { 42 43 return getHtmlMultipart(content0, null); 44 } 45 46 47 63 public static MimeMultipart getHtmlMultipart(InputStream content0, String externalPlainText0) 64 throws SMIMEException { 65 66 MimeMultipart alternativePart = new MimeMultipart ("alternative"); 67 try { 68 HtmlAnalyzer ha = new HtmlAnalyzer(content0, externalPlainText0); 69 70 MimeBodyPart textBody = new MimeBodyPart (); 71 textBody.setText(ha.getPlainText(), "ISO-8859-1"); 72 MimeBodyPart htmlBody = new MimeBodyPart (); 73 htmlBody.setContent(ha.getHtmlText(),"text/html"); 74 75 alternativePart.addBodyPart(textBody); 76 alternativePart.addBodyPart(htmlBody); 77 } 78 catch(Exception e) { 79 throw SMIMEException.getInstance( "org.enhydra.oyster.mail.MultipartGenerator", 80 e, "getHtmlMultipart"); 81 } 82 return alternativePart; 83 } 84 85 86 105 public static MimeMultipart getHtmlMultipart( InputStream content0, 106 String path0, 107 InputStream [] resources0 ) 108 throws SMIMEException { 109 return getHtmlMultipart(content0, path0, resources0, null); 110 } 111 112 113 137 public static MimeMultipart getHtmlMultipart( InputStream content0, 138 String path0, 139 InputStream [] resources0, 140 String externalPlainText0 ) 141 throws SMIMEException { 142 try { 143 HtmlAnalyzer ha = new HtmlAnalyzer(content0, path0, externalPlainText0); 144 MimeBodyPart textBody = new MimeBodyPart (); 145 textBody.setText(ha.getPlainText(), "ISO-8859-1"); 146 MimeBodyPart htmlBody = new MimeBodyPart (); 147 htmlBody.setContent(ha.getHtmlText(),"text/html"); 148 149 MimeMultipart alternativePart = new MimeMultipart ("alternative"); 150 alternativePart.addBodyPart(textBody); 151 alternativePart.addBodyPart(htmlBody); 152 153 MimeMultipart relatedPart = null; 154 Vector vAddress = ha.getSwappedAdresses(); 155 if (vAddress != null) { 156 relatedPart = new MimeMultipart ("related"); 157 MimeBodyPart alternativeBody = new MimeBodyPart (); 158 alternativeBody.setContent(alternativePart); 159 relatedPart.addBodyPart( alternativeBody ); 160 161 for( int i = 0; i < vAddress.size(); i=i+2 ) { 162 try { 163 if(vAddress.elementAt(i) instanceof File ) { 164 MimeBodyPart mbp = new MimeBodyPart (); 165 File fn = (File )vAddress.elementAt(i); 166 FileDataSource fd = new FileDataSource (fn); 167 mbp.setDataHandler(new DataHandler (fd)); 168 mbp.setFileName(fn.getName()); 169 mbp.addHeader("Content-ID","<" + vAddress.elementAt(i+1) + ">"); 170 relatedPart.addBodyPart(mbp); 171 } 172 else { 173 MimeBodyPart mbp = new MimeBodyPart (); 174 int index = Integer.parseInt( 175 ((String )vAddress.elementAt(i)).substring(5,8)); 176 String fileName = ((String )vAddress.elementAt(i)).substring(8); 177 mbp.setDataHandler( new DataHandler (new StreamDataSource( 178 resources0[index], fileName )) ); 179 mbp.setFileName(fileName); 180 mbp.addHeader("Content-ID","<" + vAddress.elementAt(i+1) + ">"); 181 relatedPart.addBodyPart(mbp); 182 } 183 } 184 catch(Exception e) { 185 continue; } 187 } 188 if(relatedPart.getCount() == 0) 189 return alternativePart; 190 return relatedPart; 191 } 192 else 193 return alternativePart; 194 } 195 catch(Exception e) { 196 throw SMIMEException.getInstance( "org.enhydra.oyster.mail.MultipartGenerator", 197 e, "getHtmlMultipart"); 198 } 199 } 200 201 } 202 | Popular Tags |