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
56 public abstract class MgnlMultipartEmail extends MgnlEmail {
57
58 private static final String CONTENT_ID = "Content-ID";
59
60 private static final String RELATED = "related";
61
62 protected MimeMultipart multipart;
63
64 public boolean isMultipart() {
65 return (this.multipart != null);
66 }
67
68 public MimeMultipart getMailMultipart() {
69 return this.multipart;
70 }
71
72 public MgnlMultipartEmail(MailTemplate template) {
73 super(template);
74 }
75
76 @Override
77 public MimeBodyPart addAttachment(MailAttachment attachment) throws MailException {
78
79 try {
80 if (!isMultipart()) {
81 turnOnMultipart();
82 }
83 MimeBodyPart messageBodyPart = new MimeBodyPart();
84 String key = attachment.getName();
85 log.info("Found new attachment with name :" + key);
86
87
88 URL url = attachment.getUrl();
89 String name = attachment.getFileName();
90 String contentType = attachment.getContentType();
91
92
93 messageBodyPart.setDisposition(attachment.getDisposition() + "; filename=\"" + name + "\"");
94
95
96 DataSource fds = url.getProtocol().startsWith("file:")
97 ? new FileDataSource(url.getFile())
98 : new URLDataSource(url);
99
100 messageBodyPart.setDataHandler(new DataHandler(fds));
101
102
103
104 if (!key.startsWith("<")) {
105 key = "<" + key + ">";
106 }
107 messageBodyPart.setHeader(CONTENT_ID, key);
108
109
110 messageBodyPart.setHeader(CONTENT_TYPE, contentType + "; name=\"" + name + "\"");
111
112
113 this.multipart.addBodyPart(messageBodyPart);
114
115
116 this.setContent(this.multipart);
117
118 return messageBodyPart;
119 }
120 catch (Exception e) {
121 throw new MailException(e.getMessage(), e);
122 }
123 }
124
125 private void turnOnMultipart() {
126
127 try {
128 if(this.multipart == null) {
129 this.multipart = new MimeMultipart(RELATED);
130 }
131 if(this.getContent() != null) {
132
133 Object o = this.getContent();
134 if (o instanceof String) {
135 BodyPart messageBodyPart = new MimeBodyPart();
136 messageBodyPart.setContent(o, getContentType());
137 this.multipart.addBodyPart(messageBodyPart, 0);
138 this.setContent(this.multipart);
139 }
140 }
141 }
142 catch (Exception e) {
143 log.info(e.getMessage());
144 }
145 }
146
147 @Override
148 public void setBody(String body) throws Exception {
149
150 this.setContent(body, getContentType());
151
152
153 final List<MailAttachment> attachments = super.getTemplate().getAttachments();
154 if (attachments != null) {
155 setAttachments(attachments);
156 }
157 }
158
159 }