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