1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.mail.templates;
35
36 import info.magnolia.module.mail.MailException;
37 import info.magnolia.module.mail.MailTemplate;
38
39 import java.net.URL;
40 import java.util.List;
41
42 import javax.activation.DataHandler;
43 import javax.activation.DataSource;
44 import javax.activation.FileDataSource;
45 import javax.activation.URLDataSource;
46 import javax.mail.BodyPart;
47 import javax.mail.internet.MimeBodyPart;
48 import javax.mail.internet.MimeMultipart;
49
50
51
52
53
54
55 public abstract class MgnlMultipartEmail extends MgnlEmail {
56
57 private static final String CONTENT_ID = "Content-ID";
58
59 private static final String RELATED = "related";
60
61 protected MimeMultipart multipart;
62
63 public boolean isMultipart() {
64 return (this.multipart != null);
65 }
66
67 public MimeMultipart getMailMultipart() {
68 return this.multipart;
69 }
70
71 public MgnlMultipartEmail(MailTemplate template) {
72 super(template);
73 }
74
75 public MimeBodyPart addAttachment(MailAttachment attachment) throws MailException {
76
77 try {
78 if (!isMultipart()) {
79 turnOnMultipart();
80 }
81 MimeBodyPart messageBodyPart = new MimeBodyPart();
82 String key = attachment.getName();
83 log.info("Found new attachment with name :" + key);
84
85
86 URL url = attachment.getUrl();
87 String name = attachment.getFileName();
88 String contentType = attachment.getContentType();
89
90
91 messageBodyPart.setDisposition(attachment.getDisposition() + "; filename=\"" + name + "\"");
92
93
94 DataSource fds = url.getProtocol().startsWith("file:")
95 ? new FileDataSource(url.getFile())
96 : new URLDataSource(url);
97
98 messageBodyPart.setDataHandler(new DataHandler(fds));
99
100
101
102 if (!key.startsWith("<")) {
103 key = "<" + key + ">";
104 }
105 messageBodyPart.setHeader(CONTENT_ID, key);
106
107
108 messageBodyPart.setHeader(CONTENT_TYPE, contentType + "; name=\"" + name + "\"");
109
110
111 this.multipart.addBodyPart(messageBodyPart);
112
113
114 this.setContent(this.multipart);
115
116 return messageBodyPart;
117 }
118 catch (Exception e) {
119 throw new MailException(e.getMessage(), e);
120 }
121 }
122
123 private void turnOnMultipart() {
124
125 try {
126 if(this.multipart == null) {
127 this.multipart = new MimeMultipart(RELATED);
128 }
129 if(this.getContent() != null) {
130
131 Object o = this.getContent();
132 if (o instanceof String) {
133 BodyPart messageBodyPart = new MimeBodyPart();
134 messageBodyPart.setContent(o, getContentType());
135 this.multipart.addBodyPart(messageBodyPart, 0);
136 this.setContent(this.multipart);
137 }
138 }
139 }
140 catch (Exception e) {
141 log.info(e.getMessage());
142 }
143 }
144
145 public void setBody(String body) throws Exception {
146
147 this.setContent(body, getContentType());
148
149
150 final List<MailAttachment> attachments = super.getTemplate().getAttachments();
151 if (attachments != null) {
152 setAttachments(attachments);
153 }
154 }
155
156 }