1 package info.magnolia.cms.mail.templates; 2 3 import info.magnolia.cms.mail.MailException; 4 5 import java.net.URL ; 6 7 import javax.activation.DataHandler ; 8 import javax.activation.DataSource ; 9 import javax.activation.FileDataSource ; 10 import javax.activation.URLDataSource ; 11 import javax.mail.MessagingException ; 12 import javax.mail.Session ; 13 import javax.mail.internet.MimeBodyPart ; 14 import javax.mail.internet.MimeMultipart ; 15 16 17 21 public abstract class MgnlMultipartEmail extends MgnlEmail { 22 23 private static final String CONTENT_ID = "Content-ID"; 24 25 private static final String RELATED = "related"; 26 27 protected MimeMultipart multipart; 28 29 protected MgnlMultipartEmail(Session _session) { 30 super(_session); 31 this.multipart = new MimeMultipart (RELATED); 33 } 34 35 public boolean isMultipart() { 36 try { 37 int count = this.multipart.getCount(); 38 return (count > 0); 39 } 40 catch (MessagingException e) { 41 return false; 42 } 43 } 44 45 public MimeMultipart getMailMultipart() { 46 return this.multipart; 47 } 48 49 public MimeBodyPart addAttachment(MailAttachment attachment) throws MailException { 50 51 try { 52 MimeBodyPart messageBodyPart = new MimeBodyPart (); 53 String key = attachment.getName(); 54 log.info("Found new attachment with name :" + key); 55 56 URL url = attachment.getURL(); 58 String name = attachment.getFileName(); 59 String contentType = attachment.getContentType(); 60 61 messageBodyPart.setHeader(CONTENT_TYPE, contentType + "; name=\"" + name + "\""); 63 messageBodyPart.setDisposition("inline; filename=\"" + name + "\""); 65 66 DataSource fds = url.getProtocol().startsWith("file:") 68 ? (DataSource ) new FileDataSource (url.getFile()) 69 : (DataSource ) new URLDataSource (url); 70 messageBodyPart.setDataHandler(new DataHandler (fds)); 72 messageBodyPart.setHeader(CONTENT_ID, key); 74 this.multipart.addBodyPart(messageBodyPart); 76 77 this.setContent(this.multipart); 79 80 return messageBodyPart; 81 } 82 catch (Exception e) { 83 throw new MailException(e.getMessage(), e); 84 } 85 } 86 87 } 89 | Popular Tags |