KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > mail > templates > MgnlMultipartEmail


1 package info.magnolia.cms.mail.templates;
2
3 import info.magnolia.cms.mail.MailException;
4
5 import java.net.URL JavaDoc;
6
7 import javax.activation.DataHandler JavaDoc;
8 import javax.activation.DataSource JavaDoc;
9 import javax.activation.FileDataSource JavaDoc;
10 import javax.activation.URLDataSource JavaDoc;
11 import javax.mail.MessagingException JavaDoc;
12 import javax.mail.Session JavaDoc;
13 import javax.mail.internet.MimeBodyPart JavaDoc;
14 import javax.mail.internet.MimeMultipart JavaDoc;
15
16
17 /**
18  * Date: Apr 1, 2006 Time: 9:00:35 PM
19  * @author <a HREF="mailto:niko@macnica.com">Nicolas Modrzyk</a>
20  */

21 public abstract class MgnlMultipartEmail extends MgnlEmail {
22
23     private static final String JavaDoc CONTENT_ID = "Content-ID";
24
25     private static final String JavaDoc RELATED = "related";
26
27     protected MimeMultipart JavaDoc multipart;
28
29     protected MgnlMultipartEmail(Session JavaDoc _session) {
30         super(_session);
31         // Create a related multi-part to combine the parts
32
this.multipart = new MimeMultipart JavaDoc(RELATED);
33     }
34
35     public boolean isMultipart() {
36         try {
37             int count = this.multipart.getCount();
38             return (count > 0);
39         }
40         catch (MessagingException JavaDoc e) {
41             return false;
42         }
43     }
44
45     public MimeMultipart JavaDoc getMailMultipart() {
46         return this.multipart;
47     }
48
49     public MimeBodyPart JavaDoc addAttachment(MailAttachment attachment) throws MailException {
50
51         try {
52             MimeBodyPart JavaDoc messageBodyPart = new MimeBodyPart JavaDoc();
53             String JavaDoc key = attachment.getName();
54             log.info("Found new attachment with name :" + key);
55
56             // get info on the attachment
57
URL JavaDoc url = attachment.getURL();
58             String JavaDoc name = attachment.getFileName();
59             String JavaDoc contentType = attachment.getContentType();
60
61             // set the header as well as the content type
62
messageBodyPart.setHeader(CONTENT_TYPE, contentType + "; name=\"" + name + "\"");
63             // set the disposition of the file.
64
messageBodyPart.setDisposition("inline; filename=\"" + name + "\"");
65
66             // Fetch the image and associate to part
67
DataSource JavaDoc fds = url.getProtocol().startsWith("file:")
68                 ? (DataSource JavaDoc) new FileDataSource JavaDoc(url.getFile())
69                 : (DataSource JavaDoc) new URLDataSource JavaDoc(url);
70             // DataSource fd = new FileDataSource(file);
71
messageBodyPart.setDataHandler(new DataHandler JavaDoc(fds));
72             // Add a header to connect to the HTML
73
messageBodyPart.setHeader(CONTENT_ID, key);
74             // Add part to multi-part
75
this.multipart.addBodyPart(messageBodyPart);
76
77             // Set the content again
78
this.setContent(this.multipart);
79
80             return messageBodyPart;
81         }
82         catch (Exception JavaDoc e) {
83             throw new MailException(e.getMessage(), e);
84         }
85     }
86
87     // public abstract String getContentDescription();
88
}
89
Popular Tags