KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > SimpleTextMessage


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

17
18 package javax.mail;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.util.Arrays;
23 import java.util.Date;
24 import java.util.Enumeration;
25 import java.util.LinkedList;
26 import java.util.List;
27 import javax.activation.DataHandler;
28 import javax.mail.internet.InternetAddress;
29 /**
30  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
31  */

32 public class SimpleTextMessage extends Message {
33     public static final Address[] ADDRESS_ARRAY = new Address[0];
34     private List _bcc = new LinkedList();
35     private List _cc = new LinkedList();
36     private String _description;
37     private Flags _flags = new Flags();
38     private List _from = new LinkedList();
39     private Date _received;
40     private Date _sent;
41     private String _subject;
42     private String _text;
43     private List _to = new LinkedList();
44     /**
45      * @param folder
46      * @param number
47      */

48     public SimpleTextMessage(Folder folder, int number) {
49         super(folder, number);
50     }
51     /* (non-Javadoc)
52      * @see javax.mail.Message#addFrom(javax.mail.Address[])
53      */

54     public void addFrom(Address[] addresses) throws MessagingException {
55         _from.addAll(Arrays.asList(addresses));
56     }
57     /* (non-Javadoc)
58      * @see javax.mail.Part#addHeader(java.lang.String, java.lang.String)
59      */

60     public void addHeader(String name, String value)
61         throws MessagingException {
62         throw new UnsupportedOperationException("Method not implemented");
63     }
64     /* (non-Javadoc)
65      * @see javax.mail.Message#addRecipients(javax.mail.Message.RecipientType, javax.mail.Address[])
66      */

67     public void addRecipients(RecipientType type, Address[] addresses)
68         throws MessagingException {
69         getList(type).addAll(Arrays.asList(addresses));
70     }
71     /* (non-Javadoc)
72      * @see javax.mail.Part#getAllHeaders()
73      */

74     public Enumeration getAllHeaders() throws MessagingException {
75         throw new UnsupportedOperationException("Method not implemented");
76     }
77     /* (non-Javadoc)
78      * @see javax.mail.Part#getContent()
79      */

80     public Object getContent() throws IOException, MessagingException {
81         return _text;
82     }
83     /* (non-Javadoc)
84      * @see javax.mail.Part#getContentType()
85      */

86     public String getContentType() throws MessagingException {
87         return "text/plain";
88     }
89     /* (non-Javadoc)
90      * @see javax.mail.Part#getDataHandler()
91      */

92     public DataHandler getDataHandler() throws MessagingException {
93         throw new UnsupportedOperationException("Method not implemented");
94     }
95     /* (non-Javadoc)
96      * @see javax.mail.Part#getDescription()
97      */

98     public String getDescription() throws MessagingException {
99         return _description;
100     }
101     /* (non-Javadoc)
102      * @see javax.mail.Part#getDisposition()
103      */

104     public String getDisposition() throws MessagingException {
105         return Part.INLINE;
106     }
107     /* (non-Javadoc)
108      * @see javax.mail.Part#getFileName()
109      */

110     public String getFileName() throws MessagingException {
111         return null;
112     }
113     /* (non-Javadoc)
114      * @see javax.mail.Message#getFlags()
115      */

116     public Flags getFlags() throws MessagingException {
117         return _flags;
118     }
119     /* (non-Javadoc)
120      * @see javax.mail.Message#getFrom()
121      */

122     public Address[] getFrom() throws MessagingException {
123         return (Address[]) _from.toArray(ADDRESS_ARRAY);
124     }
125     /* (non-Javadoc)
126      * @see javax.mail.Part#getHeader(java.lang.String)
127      */

128     public String[] getHeader(String name) throws MessagingException {
129         throw new UnsupportedOperationException("Method not implemented");
130     }
131     /* (non-Javadoc)
132      * @see javax.mail.Part#getInputStream()
133      */

134     public InputStream getInputStream()
135         throws IOException, MessagingException {
136         throw new UnsupportedOperationException("Method not implemented");
137     }
138     /* (non-Javadoc)
139      * @see javax.mail.Part#getLineCount()
140      */

141     public int getLineCount() throws MessagingException {
142         throw new UnsupportedOperationException("Method not implemented");
143     }
144     private List getList(RecipientType type) throws MessagingException {
145         List list;
146         if (type == RecipientType.TO) {
147             list = _to;
148         } else if (type == RecipientType.CC) {
149             list = _cc;
150         } else if (type == RecipientType.BCC) {
151             list = _bcc;
152         } else {
153             throw new MessagingException("Address type not understood");
154         }
155         return list;
156     }
157     /* (non-Javadoc)
158      * @see javax.mail.Part#getMatchingHeaders(java.lang.String[])
159      */

160     public Enumeration getMatchingHeaders(String[] names)
161         throws MessagingException {
162         throw new UnsupportedOperationException("Method not implemented");
163     }
164     /* (non-Javadoc)
165      * @see javax.mail.Part#getNonMatchingHeaders(java.lang.String[])
166      */

167     public Enumeration getNonMatchingHeaders(String[] names)
168         throws MessagingException {
169         throw new UnsupportedOperationException("Method not implemented");
170     }
171     /* (non-Javadoc)
172      * @see javax.mail.Message#getReceivedDate()
173      */

174     public Date getReceivedDate() throws MessagingException {
175         return _received;
176     }
177     /* (non-Javadoc)
178      * @see javax.mail.Message#getRecipients(javax.mail.Message.RecipientType)
179      */

180     public Address[] getRecipients(RecipientType type)
181         throws MessagingException {
182         return (Address[]) getList(type).toArray(ADDRESS_ARRAY);
183     }
184     /* (non-Javadoc)
185      * @see javax.mail.Message#getSentDate()
186      */

187     public Date getSentDate() throws MessagingException {
188         return _sent;
189     }
190     /* (non-Javadoc)
191      * @see javax.mail.Part#getSize()
192      */

193     public int getSize() throws MessagingException {
194         return _text.length();
195     }
196     /* (non-Javadoc)
197      * @see javax.mail.Message#getSubject()
198      */

199     public String getSubject() throws MessagingException {
200         return _subject;
201     }
202     /* (non-Javadoc)
203      * @see javax.mail.Part#isMimeType(java.lang.String)
204      */

205     public boolean isMimeType(String mimeType) throws MessagingException {
206         return mimeType.equals("text/plain") || mimeType.equals("text/*");
207     }
208     /* (non-Javadoc)
209      * @see javax.mail.Part#removeHeader(java.lang.String)
210      */

211     public void removeHeader(String name) throws MessagingException {
212         throw new UnsupportedOperationException("Method not implemented");
213     }
214     /* (non-Javadoc)
215      * @see javax.mail.Message#reply(boolean)
216      */

217     public Message reply(boolean replyToAll) throws MessagingException {
218         try {
219             SimpleTextMessage reply = (SimpleTextMessage) this.clone();
220             reply._to = new LinkedList(_from);
221             if (replyToAll) {
222                 reply._to.addAll(_cc);
223             }
224             return reply;
225         } catch (CloneNotSupportedException e) {
226             throw new MessagingException(e.getMessage());
227         }
228     }
229     /* (non-Javadoc)
230      * @see javax.mail.Message#saveChanges()
231      */

232     public void saveChanges() throws MessagingException {
233         throw new UnsupportedOperationException("Method not implemented");
234     }
235     /* (non-Javadoc)
236      * @see javax.mail.Part#setContent(javax.mail.Multipart)
237      */

238     public void setContent(Multipart content) throws MessagingException {
239         throw new UnsupportedOperationException("Method not implemented");
240     }
241     /* (non-Javadoc)
242      * @see javax.mail.Part#setContent(java.lang.Object, java.lang.String)
243      */

244     public void setContent(Object content, String type)
245         throws MessagingException {
246         setText((String) content);
247     }
248     /* (non-Javadoc)
249      * @see javax.mail.Part#setDataHandler(javax.activation.DataHandler)
250      */

251     public void setDataHandler(DataHandler handler) throws MessagingException {
252         throw new UnsupportedOperationException("Method not implemented");
253     }
254     /* (non-Javadoc)
255      * @see javax.mail.Part#setDescription(java.lang.String)
256      */

257     public void setDescription(String description) throws MessagingException {
258         _description = description;
259     }
260     /* (non-Javadoc)
261      * @see javax.mail.Part#setDisposition(java.lang.String)
262      */

263     public void setDisposition(String disposition) throws MessagingException {
264         throw new UnsupportedOperationException("Method not implemented");
265     }
266     /* (non-Javadoc)
267      * @see javax.mail.Part#setFileName(java.lang.String)
268      */

269     public void setFileName(String name) throws MessagingException {
270         throw new UnsupportedOperationException("Method not implemented");
271     }
272     /* (non-Javadoc)
273      * @see javax.mail.Message#setFlags(javax.mail.Flags, boolean)
274      */

275     public void setFlags(Flags flags, boolean set) throws MessagingException {
276         if (set) {
277             _flags.add(flags);
278         } else {
279             _flags.remove(flags);
280         }
281     }
282     /* (non-Javadoc)
283      * @see javax.mail.Message#setFrom()
284      */

285     public void setFrom() throws MessagingException {
286         setFrom(new InternetAddress("root@localhost"));
287     }
288     /* (non-Javadoc)
289      * @see javax.mail.Message#setFrom(javax.mail.Address)
290      */

291     public void setFrom(Address address) throws MessagingException {
292         _from.clear();
293         _from.add(address);
294     }
295     /* (non-Javadoc)
296      * @see javax.mail.Part#setHeader(java.lang.String, java.lang.String)
297      */

298     public void setHeader(String name, String value)
299         throws MessagingException {
300         throw new UnsupportedOperationException("Method not implemented");
301     }
302     /* (non-Javadoc)
303      * @see javax.mail.Message#setRecipients(javax.mail.Message.RecipientType, javax.mail.Address[])
304      */

305     public void setRecipients(RecipientType type, Address[] addresses)
306         throws MessagingException {
307         List list = getList(type);
308         list.clear();
309         list.addAll(Arrays.asList(addresses));
310     }
311     /* (non-Javadoc)
312      * @see javax.mail.Message#setSentDate(java.util.Date)
313      */

314     public void setSentDate(Date sent) throws MessagingException {
315         _sent = sent;
316     }
317     /* (non-Javadoc)
318      * @see javax.mail.Message#setSubject(java.lang.String)
319      */

320     public void setSubject(String subject) throws MessagingException {
321         _subject = subject;
322     }
323     /* (non-Javadoc)
324      * @see javax.mail.Part#setText(java.lang.String)
325      */

326     public void setText(String content) throws MessagingException {
327         _text = content;
328     }
329     /* (non-Javadoc)
330      * @see javax.mail.Part#writeTo(java.io.OutputStream)
331      */

332     public void writeTo(OutputStream out)
333         throws IOException, MessagingException {
334         throw new UnsupportedOperationException("Method not implemented");
335     }
336 }
337
Popular Tags