1 package org.apache.turbine.util.mail;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.net.URL;
20
21 import java.util.Vector;
22
23 import javax.activation.DataHandler;
24 import javax.activation.DataSource;
25 import javax.activation.URLDataSource;
26
27 import javax.mail.MessagingException;
28 import javax.mail.internet.MimeBodyPart;
29 import javax.mail.internet.MimeMultipart;
30
31 import org.apache.torque.util.Criteria;
32
33 /***
34 * A multipart email.
35 *
36 * <p>This class is used to send multi-part internet email like
37 * messages with attachments.
38 *
39 * <p>To create a multi-part email, call the default constructor and
40 * then you can call setMsg() to set the message and call the
41 * different attach() methods.
42 *
43 * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
44 * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
45 * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
46 * @author <a href="mailto:unknown">Regis Koenig</a>
47 * @version $Id: MultiPartEmail.java 264148 2005-08-29 14:21:04Z henning $
48 * @deprecated Use org.apache.commons.mail.MultiPartEmail instead.
49 */
50 public class MultiPartEmail extends Email
51 {
52 /*** Body portion of the email. */
53 protected MimeMultipart emailBody;
54
55 /*** The message container. */
56 protected MimeBodyPart main;
57
58 /*** The file server if it exists. */
59 private String fileServer = null;
60
61 /***
62 * Initialize the multipart email.
63 *
64 * @exception MessagingException.
65 */
66 public MultiPartEmail()
67 throws MessagingException
68 {
69 this.init();
70 }
71
72 /***
73 * Constructor used to initialize attributes.
74 *
75 * <p>This method uses the criteria object to set the different
76 * fields of the e-mail. The expected fields of the Criteria are:
77 *
78 * <ul>
79 * <li>SENDER_EMAIL</li>
80 * <li>RECEIVER_EMAIL</li>
81 * <li>EMAIL_SUBJECT</li>
82 * <li>EMAIL_BODY</li>
83 * <li>ATTACHMENTS - A Vector of EmailAttachment.</li>
84 * <li>FILE_SERVER - Where the files are located. If not given,
85 * they are assumed to be local.</li>
86 * </ul>
87 *
88 * Deprecated, since Criteria is deprecated in mail API.
89 *
90 * @param criteria A Criteria.
91 * @exception MessagingException.
92 */
93 public MultiPartEmail(Criteria criteria)
94 throws MessagingException
95 {
96 this.init();
97 this.initCriteria(criteria);
98 }
99
100 /***
101 * Initialize the multipart email.
102 *
103 * @exception MessagingException.
104 */
105 protected void init()
106 throws MessagingException
107 {
108 super.init();
109
110 fileServer = null;
111
112
113 emailBody = new MimeMultipart();
114 message.setContent(emailBody);
115
116
117 main = new MimeBodyPart();
118 emailBody.addBodyPart(main);
119 }
120
121 /***
122 * Uses the criteria to set the fields.
123 *
124 * <p>This method uses the criteria object to set the different
125 * fields of the e-mail. The expected fields of the Criteria are:
126 *
127 * <ul>
128 * <li>SENDER_EMAIL</li>
129 * <li>RECEIVER_EMAIL</li>
130 * <li>EMAIL_SUBJECT</li>
131 * <li>EMAIL_BODY</li>
132 * <li>ATTACHMENTS - A Vector of EmailAttachment.</li>
133 * <li>FILE_SERVER - Where the files are located. If not given,
134 * they are assumed to be local.</li>
135 * </ul>
136 *
137 * Deprecated, since the Criteria is deprecated.
138 *
139 * @param criteria A Criteria.
140 * @exception MessagingException.
141 */
142 protected void initCriteria(Criteria criteria)
143 throws MessagingException
144 {
145 super.initCriteria(criteria);
146
147 if (criteria.containsKey(EMAIL_BODY))
148 {
149 setMsg(criteria.getString(EMAIL_BODY));
150 }
151 else
152 {
153 setMsg("NO MESSAGE");
154 }
155
156 Vector attachments;
157
158 if (criteria.containsKey(ATTACHMENTS))
159 {
160 attachments = (Vector) criteria.get(ATTACHMENTS);
161 }
162 else
163 {
164 attachments = new Vector();
165 }
166
167 if (criteria.containsKey(FILE_SERVER))
168 {
169 fileServer = criteria.getString(FILE_SERVER);
170 }
171
172 for (int i = 0; i < attachments.size(); i++)
173 {
174 EmailAttachment attachment =
175 (EmailAttachment) attachments.elementAt(i);
176 attach(attachment);
177 }
178 }
179
180 /***
181 * Set the message of the email.
182 *
183 * @param msg A String.
184 * @return An Email.
185 * @exception MessagingException.
186 */
187 public Email setMsg(String msg)
188 throws MessagingException
189 {
190 if (charset != null)
191 {
192 main.setText(msg, charset);
193 }
194 else
195 {
196 main.setText(msg);
197 }
198 return this;
199 }
200
201 /***
202 * Attach an EmailAttachement.
203 *
204 * @param attachment An EmailAttachment.
205 * @return A MultiPartEmail.
206 * @exception MessagingException.
207 */
208 public MultiPartEmail attach(EmailAttachment attachment)
209 throws MessagingException
210 {
211 URL url = attachment.getURL();
212 if (url == null)
213 {
214 try
215 {
216 String file = attachment.getPath();
217 url = new URL("file", fileServer, file);
218 }
219 catch (Exception e)
220 {
221 throw new MessagingException("Cannot find file", e);
222 }
223 }
224
225 return attach(url, attachment.getName(),
226 attachment.getDescription(),
227 attachment.getDisposition());
228 }
229
230 /***
231 * Attach a file located by its URL. The disposition of the file
232 * is set to mixed.
233 *
234 * @param url The URL of the file (may be any valid URL).
235 * @param name The name field for the attachment.
236 * @param description A description for the attachment.
237 * @return A MultiPartEmail.
238 * @exception MessagingException.
239 */
240 public MultiPartEmail attach(URL url, String name, String description)
241 throws MessagingException
242 {
243 return attach(url, name, description, EmailAttachment.ATTACHMENT);
244 }
245
246 /***
247 * Attach a file located by its URL.
248 *
249 * @param url The URL of the file (may be any valid URL).
250 * @param name The name field for the attachment.
251 * @param description A description for the attachment.
252 * @param disposition Either mixed or inline.
253 * @return A MultiPartEmail.
254 * @exception MessagingException.
255 */
256 public MultiPartEmail attach(URL url,
257 String name,
258 String description,
259 String disposition)
260 throws MessagingException
261 {
262 return attach(new URLDataSource(url), name, description, disposition);
263 }
264
265 /***
266 * Attach a file specified as a DataSource interface.
267 *
268 * @param ds A DataSource interface for the file.
269 * @param name The name field for the attachment.
270 * @param description A description for the attachment.
271 * @return A MultiPartEmail.
272 * @exception MessagingException.
273 */
274 public MultiPartEmail attach(DataSource ds,
275 String name,
276 String description)
277 throws MessagingException
278 {
279 return attach(ds, name, description, EmailAttachment.ATTACHMENT);
280 }
281
282 /***
283 * Attach a file specified as a DataSource interface.
284 *
285 * @param ds A DataSource interface for the file.
286 * @param name The name field for the attachment.
287 * @param description A description for the attachement.
288 * @param disposition Either mixed or inline.
289 * @return A MultiPartEmail.
290 * @exception MessagingException.
291 */
292 public MultiPartEmail attach(DataSource ds,
293 String name,
294 String description,
295 String disposition)
296 throws MessagingException
297 {
298 MimeBodyPart mbp = new MimeBodyPart();
299 emailBody.addBodyPart(mbp);
300
301 mbp.setDisposition(disposition);
302 mbp.setFileName(name);
303 mbp.setDescription(description);
304 mbp.setDataHandler(new DataHandler(ds));
305
306 return this;
307 }
308 }