1 package info.magnolia.cms.mail.templates.impl; 2 3 import info.magnolia.cms.mail.MailException; 4 import info.magnolia.cms.mail.templates.MailAttachment; 5 import info.magnolia.cms.mail.templates.MgnlMultipartEmail; 6 7 import java.util.ArrayList ; 8 import java.util.Map ; 9 10 import javax.mail.BodyPart ; 11 import javax.mail.Session ; 12 import javax.mail.internet.MimeBodyPart ; 13 14 15 19 public class HtmlEmail extends MgnlMultipartEmail { 20 21 public static final String MAIL_ATTACHMENT = "attachment"; 22 23 public HtmlEmail(Session _session) throws Exception { 24 super(_session); 25 this.setHeader(CONTENT_TYPE, TEXT_HTML_UTF); 26 } 27 28 public void setBody(String body, Map parameters) throws Exception { 29 if (!isMultipart()) { this.setContent(body, TEXT_HTML_UTF); 32 } 33 else { BodyPart messageBodyPart = new MimeBodyPart (); 36 messageBodyPart.setContent(body, TEXT_HTML_UTF); 38 this.multipart.addBodyPart(messageBodyPart, 0); 40 this.setContent(this.multipart); 41 } 42 43 if (parameters != null && parameters.containsKey(MAIL_ATTACHMENT)) { 45 ArrayList attachment = (ArrayList ) parameters.get(MAIL_ATTACHMENT); 46 setAttachments(attachment); 47 } 48 } 49 50 private void turnOnMultipart() { 51 try { 52 Object o = this.getContent(); 53 if (o instanceof String ) { 54 BodyPart messageBodyPart = new MimeBodyPart (); 55 messageBodyPart.setContent(o, TEXT_HTML_UTF); 56 this.multipart.addBodyPart(messageBodyPart, 0); 57 this.setContent(this.multipart); 58 } 59 } 60 catch (Exception e) { 61 log.info("Could not turn on multipart"); 62 } 63 } 64 65 public MimeBodyPart addAttachment(MailAttachment attachment) throws MailException { 66 if (!isMultipart()) { 67 turnOnMultipart(); 68 } 69 return super.addAttachment(attachment); 70 } 71 72 } 73 | Popular Tags |