1 package org.apache.turbine.util.mail; 2 3 18 19 import java.net.URL ; 20 21 import javax.activation.DataHandler ; 22 import javax.activation.URLDataSource ; 23 24 import javax.mail.MessagingException ; 25 import javax.mail.internet.MimeBodyPart ; 26 import javax.mail.internet.MimeMultipart ; 27 28 import org.apache.commons.lang.StringUtils; 29 30 import org.apache.ecs.Document; 31 import org.apache.ecs.ElementContainer; 32 import org.apache.ecs.html.Body; 33 import org.apache.ecs.html.Html; 34 import org.apache.ecs.html.PRE; 35 36 60 public class HtmlEmail extends MultiPartEmail 61 { 62 protected MimeMultipart htmlContent; 63 64 protected String text; 65 protected String html; 66 67 72 public HtmlEmail() 73 throws MessagingException 74 { 75 this.init(); 76 } 77 78 84 public MimeMultipart getHtmlContent() 85 { 86 if (htmlContent == null) 87 { 88 htmlContent = new MimeMultipart (); 89 } 90 return htmlContent; 91 } 92 93 100 public HtmlEmail setTextMsg(String text) 101 throws MessagingException 102 { 103 this.text = text; 104 return this; 105 } 106 107 114 public HtmlEmail setHtmlMsg(String html) 115 throws MessagingException 116 { 117 this.html = html; 118 return this; 119 } 120 121 128 public HtmlEmail setHtmlMsg(Document doc) 129 throws MessagingException 130 { 131 return setHtmlMsg(doc.toString()); 132 } 133 134 147 public Email setMsg(String msg) 148 throws MessagingException 149 { 150 setTextMsg(msg); 151 setHtmlMsg(new ElementContainer(new Html(new Body() 152 .addElement(new PRE(msg)))).toString()); 153 return this; 154 } 155 156 177 public String embed(URL url, String name) 178 throws MessagingException 179 { 180 MimeBodyPart mbp = new MimeBodyPart (); 181 182 mbp.setDataHandler(new DataHandler (new URLDataSource (url))); 183 mbp.setFileName(name); 184 mbp.setDisposition("inline"); 185 String cid = org.apache.turbine.util.GenerateUniqueId.getIdentifier(); 186 mbp.addHeader("Content-ID", cid); 187 188 getHtmlContent().addBodyPart(mbp); 189 return mbp.getContentID(); 190 } 191 192 197 public void send() 198 throws MessagingException 199 { 200 MimeBodyPart msgText = null; 201 MimeBodyPart msgHtml = null; 202 203 if (StringUtils.isNotEmpty(text) && StringUtils.isNotEmpty(html)) 204 { 205 MimeMultipart msg = getHtmlContent(); 207 msg.setSubType("alternative"); 208 main.setContent(msg); 209 210 msgText = new MimeBodyPart (); 211 msgHtml = new MimeBodyPart (); 212 msg.addBodyPart(msgText); 213 msg.addBodyPart(msgHtml); 214 215 } 216 else if (StringUtils.isNotEmpty(text)) 217 { 218 msgText = main; 220 } 221 else if (StringUtils.isNotEmpty(html)) 222 { 223 msgHtml = main; 225 } 226 else 227 { 228 msgText = main; 229 text = "NO BODY"; 230 } 231 232 if (msgText != null) 233 { 234 if (charset != null) 236 { 237 msgText.setText(text, charset); 238 } 239 else 240 { 241 msgText.setText(text); 242 } 243 } 244 245 if (msgHtml != null) 246 { 247 if (charset != null) 249 { 250 msgHtml.setContent(html, TEXT_HTML + ";charset=" + charset); 251 } 252 else 253 { 254 msgHtml.setContent(html, TEXT_HTML); 255 } 256 } 257 258 super.send(); 259 } 260 } 261 | Popular Tags |