KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > mail > MultiPartEmail


1 package org.apache.turbine.util.mail;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.net.URL JavaDoc;
20
21 import java.util.Vector JavaDoc;
22
23 import javax.activation.DataHandler JavaDoc;
24 import javax.activation.DataSource JavaDoc;
25 import javax.activation.URLDataSource JavaDoc;
26
27 import javax.mail.MessagingException JavaDoc;
28 import javax.mail.internet.MimeBodyPart JavaDoc;
29 import javax.mail.internet.MimeMultipart JavaDoc;
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,v 1.7.2.2 2004/05/20 03:20:17 seade Exp $
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 JavaDoc emailBody;
54
55     /** The message container. */
56     protected MimeBodyPart JavaDoc main;
57
58     /** The file server if it exists. */
59     private String JavaDoc fileServer = null;
60
61     /**
62      * Initialize the multipart email.
63      *
64      * @exception MessagingException.
65      */

66     public MultiPartEmail()
67             throws MessagingException JavaDoc
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 JavaDoc
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 JavaDoc
107     {
108         super.init();
109
110         fileServer = null;
111
112         /* The body of the mail. */
113         emailBody = new MimeMultipart JavaDoc();
114         message.setContent(emailBody);
115
116         /* The main message content. */
117         main = new MimeBodyPart JavaDoc();
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 JavaDoc
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 JavaDoc attachments;
157
158         if (criteria.containsKey(ATTACHMENTS))
159         {
160             attachments = (Vector JavaDoc) criteria.get(ATTACHMENTS);
161         }
162         else
163         {
164             attachments = new Vector JavaDoc();
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 JavaDoc msg)
188             throws MessagingException JavaDoc
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 JavaDoc
210     {
211         URL JavaDoc url = attachment.getURL();
212         if (url == null)
213         {
214             try
215             {
216                 String JavaDoc file = attachment.getPath();
217                 url = new URL JavaDoc("file", fileServer, file);
218             }
219             catch (Exception JavaDoc e)
220             {
221                 throw new MessagingException JavaDoc("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 JavaDoc url, String JavaDoc name, String JavaDoc description)
241             throws MessagingException JavaDoc
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 JavaDoc url,
257                                  String JavaDoc name,
258                                  String JavaDoc description,
259                                  String JavaDoc disposition)
260             throws MessagingException JavaDoc
261     {
262         return attach(new URLDataSource JavaDoc(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 JavaDoc ds,
275                                  String JavaDoc name,
276                                  String JavaDoc description)
277             throws MessagingException JavaDoc
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 JavaDoc ds,
293                                  String JavaDoc name,
294                                  String JavaDoc description,
295                                  String JavaDoc disposition)
296             throws MessagingException JavaDoc
297     {
298         MimeBodyPart JavaDoc mbp = new MimeBodyPart JavaDoc();
299         emailBody.addBodyPart(mbp);
300
301         mbp.setDisposition(disposition);
302         mbp.setFileName(name);
303         mbp.setDescription(description);
304         mbp.setDataHandler(new DataHandler JavaDoc(ds));
305
306         return this;
307     }
308 }
309
Popular Tags