KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > mail > templates > impl > HtmlEmail


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 JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.mail.BodyPart JavaDoc;
11 import javax.mail.Session JavaDoc;
12 import javax.mail.internet.MimeBodyPart JavaDoc;
13
14
15 /**
16  * Date: Mar 30, 2006 Time: 2:12:53 PM
17  * @author <a HREF="mailto:niko@macnica.com">Nicolas Modrzyk</a>
18  */

19 public class HtmlEmail extends MgnlMultipartEmail {
20
21     public static final String JavaDoc MAIL_ATTACHMENT = "attachment";
22
23     public HtmlEmail(Session JavaDoc _session) throws Exception JavaDoc {
24         super(_session);
25         this.setHeader(CONTENT_TYPE, TEXT_HTML_UTF);
26     }
27
28     public void setBody(String JavaDoc body, Map JavaDoc parameters) throws Exception JavaDoc {
29         // check if multipart
30
if (!isMultipart()) { // it is not a multipart yet, just set the text for content
31
this.setContent(body, TEXT_HTML_UTF);
32         }
33         else { // some attachment are already in this mail. Init the body part to set the main text
34
// Create your new message part
35
BodyPart JavaDoc messageBodyPart = new MimeBodyPart JavaDoc();
36             // Set the _content of the body part
37
messageBodyPart.setContent(body, TEXT_HTML_UTF);
38             // Add body part to multipart
39
this.multipart.addBodyPart(messageBodyPart, 0);
40             this.setContent(this.multipart);
41         }
42
43         // process the attachments
44
if (parameters != null && parameters.containsKey(MAIL_ATTACHMENT)) {
45             ArrayList JavaDoc attachment = (ArrayList JavaDoc) parameters.get(MAIL_ATTACHMENT);
46             setAttachments(attachment);
47         }
48     }
49
50     private void turnOnMultipart() {
51         try {
52             Object JavaDoc o = this.getContent();
53             if (o instanceof String JavaDoc) {
54                 BodyPart JavaDoc messageBodyPart = new MimeBodyPart JavaDoc();
55                 messageBodyPart.setContent(o, TEXT_HTML_UTF);
56                 this.multipart.addBodyPart(messageBodyPart, 0);
57                 this.setContent(this.multipart);
58             }
59         }
60         catch (Exception JavaDoc e) {
61             log.info("Could not turn on multipart");
62         }
63     }
64
65     public MimeBodyPart JavaDoc addAttachment(MailAttachment attachment) throws MailException {
66         if (!isMultipart()) {
67             turnOnMultipart();
68         }
69         return super.addAttachment(attachment);
70     }
71
72 }
73
Popular Tags