KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > mail > Email


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.mail;
4
5 import java.util.ArrayList JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10 import javax.activation.DataHandler JavaDoc;
11 import javax.activation.FileDataSource JavaDoc;
12 import javax.mail.MessagingException JavaDoc;
13 import javax.mail.internet.MimeBodyPart JavaDoc;
14
15 /**
16  * E-mail bean contains all parts of an eamil and handle attachements.
17  *
18  * todo Default values ?
19  * todo Add more attachements handlers
20  */

21 public class Email {
22
23     // ---------------------------------------------------------------- address
24

25     private String JavaDoc from;
26     /**
27      * Sets the FROM address
28      *
29      * @param from FROM addess
30      */

31     public void setFrom(String JavaDoc from) {
32         this.from = from;
33     }
34     /**
35      * Returns FROM address.
36      *
37      * @return FROM address
38      */

39     public String JavaDoc getFrom() {
40         return from;
41     }
42
43
44
45     private String JavaDoc[] to;
46     /**
47      * Sets single TO address.
48      *
49      * @param to single TO address
50      */

51     public void setTo(String JavaDoc to) {
52         this.to = new String JavaDoc[1];
53         this.to[0] = to;
54     }
55     /**
56      * Sets multiple TO addresses.
57      *
58      * @param to array of TO addresses
59      */

60     public void setTo(String JavaDoc[] to) {
61         this.to = to;
62     }
63     /**
64      * Returns the array of TO addresses.
65      *
66      * @return array of TO addresses
67      */

68     public String JavaDoc[] getTo() {
69         return to;
70     }
71
72
73     private String JavaDoc[] cc;
74     /**
75      * Sets single CC address
76      *
77      * @param cc single CC address
78      */

79     public void setCc(String JavaDoc cc) {
80         this.cc = new String JavaDoc[1];
81         this.cc[0] = cc;
82     }
83     /**
84      * Sets multiple CC addresses.
85      *
86      * @param cc array of CC address.
87      */

88     public void setCc(String JavaDoc[] cc) {
89         this.cc = cc;
90     }
91     /**
92      * Returns array of CC addresses.
93      *
94      * @return array of CC addresses
95      */

96     public String JavaDoc[] getCc() {
97         return cc;
98     }
99
100
101     private String JavaDoc[] bcc;
102     /**
103      * Sets single BCC address.
104      *
105      * @param bcc single BCC address
106      */

107     public void setBcc(String JavaDoc bcc) {
108         this.bcc = new String JavaDoc[1];
109         this.bcc[0] = bcc;
110     }
111     /**
112      * Sets multiple BCC addresses.
113      *
114      * @param bcc array of BCC address.
115      */

116     public void setBcc(String JavaDoc[] bcc) {
117         this.bcc = bcc;
118     }
119     /**
120      * Returns array of BCC addresses.
121      *
122      * @return array of BCC addresses
123      */

124     public String JavaDoc[] getBcc() {
125         return bcc;
126     }
127
128     // ---------------------------------------------------------------- content
129

130     private String JavaDoc subject;
131     /**
132      * Sets message subject.
133      *
134      * @param subject subject of a message
135      */

136     public void setSubject(String JavaDoc subject) {
137         this.subject = subject;
138     }
139     /**
140      * Returns message subject.
141      *
142      * @return message subject
143      */

144     public String JavaDoc getSubject() {
145         return this.subject;
146     }
147
148
149     private boolean htmlMessage = false;
150
151     private String JavaDoc message;
152     /**
153      * Sets plain message text.
154      *
155      * @param message plain message text
156      */

157     public void setMessage(String JavaDoc message) {
158         this.htmlMessage = false;
159         this.message = message;
160     }
161     /**
162      * Returns message text, either plain or HTML.
163      *
164      * @return message text
165      */

166     public String JavaDoc getMessage() {
167         return this.message;
168     }
169     /**
170      * Sets HTML message text.
171      *
172      * @param htmlMessage
173      * HTML message text
174      */

175     public void setHtmlMessage(String JavaDoc htmlMessage) {
176         this.htmlMessage = true;
177         this.message = htmlMessage;
178     }
179     /**
180      * Returns true if message is HTML, otherwise false.
181      *
182      * @return true if message is HTML, otherwise false
183      */

184     public boolean isHtmlMessage() {
185         return this.htmlMessage;
186     }
187
188
189     // ---------------------------------------------------------------- attachments
190

191
192     private ArrayList JavaDoc attachments = new ArrayList JavaDoc();
193     /**
194      * Returns total number of attachements.
195      *
196      * @return total number of attachements
197      */

198     public int getTotalAttachments() {
199         return attachments.size();
200     }
201     /**
202      * Returns one attachment body part, for javamail usage.
203      *
204      * @param i index of attachement
205      *
206      * @return attachment body part
207      */

208     public MimeBodyPart JavaDoc getAttachmentBodyPart(int i) {
209         return (MimeBodyPart JavaDoc) attachments.get(i);
210     }
211
212     /**
213      * Adds a generic attachement.
214      *
215      * @param fileName file name of attachment
216      * @param dh DataHandler
217      *
218      * @exception MessagingException
219      */

220     public void addAttachment(String JavaDoc fileName, DataHandler JavaDoc dh) throws MessagingException JavaDoc {
221         MimeBodyPart JavaDoc attBodyPart = new MimeBodyPart JavaDoc();
222         attBodyPart.setFileName(fileName);
223         attBodyPart.setDataHandler(dh);
224         attachments.add(attBodyPart);
225     }
226
227     /**
228      * Adds a HTML attachment.
229      *
230      * @param fileName attachment file name
231      * @param data HTML data
232      *
233      * @exception MessagingException
234      */

235     public void addAttachment(String JavaDoc fileName, String JavaDoc data) throws MessagingException JavaDoc {
236         DataHandler JavaDoc dh = new DataHandler JavaDoc(new ByteArrayDataSource(data, "text/html"));
237         addAttachment(fileName, dh);
238     }
239
240     /**
241      * Adds an existing file as attachment.
242      *
243      * @param fileName local name of a file to attach
244      *
245      * @exception MessagingException
246      */

247     public void addAttachment(String JavaDoc fileName) throws MessagingException JavaDoc {
248         FileDataSource JavaDoc fds = new FileDataSource JavaDoc(fileName);
249         addAttachment(fds.getName(), new DataHandler JavaDoc(fds));
250     }
251     
252
253     // ---------------------------------------------------------------- headers
254

255
256     private HashMap JavaDoc headers = new HashMap JavaDoc();
257
258     /**
259      * Return all headers as a HashMap
260      *
261      * @return defined headers
262      */

263     HashMap JavaDoc getHeaders() {
264         return headers;
265     }
266
267     /**
268      * Add single header.
269      *
270      * @param name header name
271      * @param value header value
272      */

273     public void addHeader(String JavaDoc name, String JavaDoc value) {
274         headers.put(name, value);
275     }
276
277     /**
278      * Add map of headers.
279      *
280      * @param map headers map
281      */

282     public void addHeaders(Map JavaDoc map) {
283         headers.putAll(map);
284         
285     }
286
287
288     // ---------------------------------------------------------------- date
289

290     private Date JavaDoc sentDate = null;
291
292     /**
293      * Sets e-mails sent date. If input parameter is <code>null</code> then date
294      * will be when email is physically sent.
295      *
296      * @param date sent date value
297      */

298     public void setSentDate(Date JavaDoc date) {
299         sentDate = date;
300     }
301     /**
302      * Sets current date as e-mails sent date.
303      */

304     public void setSentDate() {
305         sentDate = new Date JavaDoc();
306     }
307     /**
308      * Returns e-mails sent date. If return value is <code>null</code> then date
309      * will be set during the process of sending.
310      *
311      * @return emails sent date or null if it will be set later.
312      */

313     public Date JavaDoc getSentDate() {
314         return sentDate;
315     }
316
317
318 }
319
Popular Tags