KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > Mail


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util;
11
12 import java.text.SimpleDateFormat JavaDoc;
13 import java.util.*;
14
15 /**
16  * This mail-object gives persons the functionality to create mail
17  * and send it with the SendMail-module.
18  *
19  * @application Mail
20  * @author Rob Vermeulen
21  * @version $Id: Mail.java,v 1.9 2005/09/02 12:28:46 pierre Exp $
22  */

23 public class Mail {
24
25     private static SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
26
27     static {
28          formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
29     }
30
31     /**
32      * All the mail headers defined for this mail object.
33      */

34     public Hashtable headers = new Hashtable();
35
36     /**
37      * The recipient of the mail
38      */

39     public String JavaDoc to = "";
40     /**
41      * The sender of the mail
42      */

43     public String JavaDoc from = "";
44     /**
45      * The message body text
46      */

47     public String JavaDoc text = "";
48
49     /**
50      * Create a Mail object.
51      * The parameters define recipient and sender, but this does not create any mail headers.
52      * @param to the recipient of teh mail
53      * @param from teh sender of the mail
54      */

55     public Mail(String JavaDoc to, String JavaDoc from) {
56         this.to=to;
57         this.from=from;
58         headers.put("To", to);
59         headers.put("From", from);
60     }
61
62     /**
63      * Set the mail message text.
64      */

65     public void setText(String JavaDoc text) {
66         this.text=text;
67     }
68
69     /**
70      * Sets the subject of the mail
71      */

72     public void setSubject(String JavaDoc subject) {
73         headers.put("Subject",subject);
74     }
75
76     /**
77      * Sets the time of the mail
78      */

79     public void setDate() {
80         Date d = new Date();
81         headers.put("Date",formatter.format(d));
82     }
83
84     /**
85      * Sets given time to the mail
86      */

87     public void setDate(String JavaDoc date) {
88         headers.put("Date",date);
89     }
90
91     /**
92      * Tells the mail from who the mail is coming.
93      * Does not alter the {@link #from} field.
94      */

95     public void setFrom(String JavaDoc from) {
96         headers.put("From",from);
97     }
98
99     /**
100      * Tells the mail for who the mail is.
101      * Does not alter the {@link #to} field.
102      */

103     public void setTo(String JavaDoc to) {
104         headers.put("To",to);
105     }
106
107     /**
108      * Sends the message to all persons mentioned in the CC list.
109      * Recipients of the message can see the names of the other recipients.
110      */

111     public void setCc(String JavaDoc cc) {
112         headers.put("CC",cc);
113     }
114
115     /**
116      * Sends the message to all persons mentioned in the BCC list.
117      * Recipients of the message cannot see the names of the other recipients.
118      */

119     public void setBcc(String JavaDoc bcc) {
120         headers.put("BCC",bcc);
121     }
122
123     /**
124      * Adds a comment to the mail.
125      */

126     public void setComment(String JavaDoc comment) {
127         headers.put("Comment",comment);
128     }
129
130     /**
131      * Sets the Reply-to address
132      */

133     public void setReplyTo(String JavaDoc reply) {
134         headers.put("Reply-to",reply);
135     }
136
137     /**
138      * Sets a mail header to a fixed value
139      * @return the old value of the header (<Code>null</code> if not earlier defined)
140      */

141     public String JavaDoc setHeader(String JavaDoc header,String JavaDoc value) {
142         return (String JavaDoc)headers.put(header,value);
143     }
144
145     /**
146      * Retrieves the value of a mail header.
147      * @return the value of the header (<Code>null</code> if not defined)
148      */

149     public String JavaDoc getHeader(String JavaDoc header) {
150         return (String JavaDoc)headers.get(header);
151     }
152
153     /**
154      * Returns a description of the mail object.
155      * Includes headers and message text.
156      * @return the mail description
157      */

158     public String JavaDoc toString() {
159         return "Mail -> Headers : "+headers+"\nText :\n"+text+"\n-";
160     }
161 }
162
163
Popular Tags