KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mail > SimpleMailMessage


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

16
17 package org.springframework.mail;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Date JavaDoc;
21
22 import org.springframework.util.ObjectUtils;
23 import org.springframework.util.StringUtils;
24 import org.springframework.util.Assert;
25
26 /**
27  * Models a simple mail message, including data such as the from, to, cc, subject, and text fields.
28  *
29  * <p>Consider <code>JavaMailSender</code> and JavaMail <code>MimeMessages</code> for creating
30  * more sophisticated messages, for example messages with attachments, special
31  * character encodings, or personal names that accompany mail addresses.
32  *
33  * @author Dmitriy Kopylenko
34  * @author Juergen Hoeller
35  * @since 10.09.2003
36  * @see MailSender
37  * @see org.springframework.mail.javamail.JavaMailSender
38  * @see org.springframework.mail.javamail.MimeMessagePreparator
39  * @see org.springframework.mail.javamail.MimeMessageHelper
40  * @see org.springframework.mail.javamail.MimeMailMessage
41  */

42 public class SimpleMailMessage implements MailMessage, Serializable JavaDoc {
43
44     private String JavaDoc from;
45
46     private String JavaDoc replyTo;
47
48     private String JavaDoc[] to;
49
50     private String JavaDoc[] cc;
51
52     private String JavaDoc[] bcc;
53
54     private Date JavaDoc sentDate;
55
56     private String JavaDoc subject;
57
58     private String JavaDoc text;
59
60
61     /**
62      * Create a new <code>SimpleMailMessage</code>.
63      */

64     public SimpleMailMessage() {
65     }
66
67     /**
68      * Copy constructor for creating a new <code>SimpleMailMessage</code> from the state
69      * of an existing <code>SimpleMailMessage</code> instance.
70      * @throws IllegalArgumentException if the supplied message is <code>null</code>
71      */

72     public SimpleMailMessage(SimpleMailMessage original) {
73         Assert.notNull(original, "The 'original' message argument cannot be null");
74         this.from = original.getFrom();
75         this.replyTo = original.getReplyTo();
76         if (original.getTo() != null) {
77             this.to = copy(original.getTo());
78         }
79         if (original.getCc() != null) {
80             this.cc = copy(original.getCc());
81         }
82         if (original.getBcc() != null) {
83             this.bcc = copy(original.getBcc());
84         }
85         this.sentDate = original.getSentDate();
86         this.subject = original.getSubject();
87         this.text = original.getText();
88     }
89
90
91     public void setFrom(String JavaDoc from) {
92         this.from = from;
93     }
94
95     public String JavaDoc getFrom() {
96         return this.from;
97     }
98
99     public void setReplyTo(String JavaDoc replyTo) {
100         this.replyTo = replyTo;
101     }
102
103     public String JavaDoc getReplyTo() {
104         return replyTo;
105     }
106
107     public void setTo(String JavaDoc to) {
108         this.to = new String JavaDoc[] {to};
109     }
110
111     public void setTo(String JavaDoc[] to) {
112         this.to = to;
113     }
114
115     public String JavaDoc[] getTo() {
116         return this.to;
117     }
118
119     public void setCc(String JavaDoc cc) {
120         this.cc = new String JavaDoc[] {cc};
121     }
122
123     public void setCc(String JavaDoc[] cc) {
124         this.cc = cc;
125     }
126
127     public String JavaDoc[] getCc() {
128         return cc;
129     }
130
131     public void setBcc(String JavaDoc bcc) {
132         this.bcc = new String JavaDoc[] {bcc};
133     }
134
135     public void setBcc(String JavaDoc[] bcc) {
136         this.bcc = bcc;
137     }
138
139     public String JavaDoc[] getBcc() {
140         return bcc;
141     }
142
143     public void setSentDate(Date JavaDoc sentDate) {
144         this.sentDate = sentDate;
145     }
146
147     public Date JavaDoc getSentDate() {
148         return sentDate;
149     }
150
151     public void setSubject(String JavaDoc subject) {
152         this.subject = subject;
153     }
154
155     public String JavaDoc getSubject() {
156         return this.subject;
157     }
158
159     public void setText(String JavaDoc text) {
160         this.text = text;
161     }
162
163     public String JavaDoc getText() {
164         return this.text;
165     }
166
167
168     /**
169      * Copy the contents of this message to the given target message.
170      * @param target the <code>MailMessage</code> to copy to
171      * @throws IllegalArgumentException if the supplied <code>target</code> is <code>null</code>
172      */

173     public void copyTo(MailMessage target) {
174         Assert.notNull(target, "The 'target' message argument cannot be null");
175         if (getFrom() != null) {
176             target.setFrom(getFrom());
177         }
178         if (getReplyTo() != null) {
179             target.setReplyTo(getReplyTo());
180         }
181         if (getTo() != null) {
182             target.setTo(getTo());
183         }
184         if (getCc() != null) {
185             target.setCc(getCc());
186         }
187         if (getBcc() != null) {
188             target.setBcc(getBcc());
189         }
190         if (getSentDate() != null) {
191             target.setSentDate(getSentDate());
192         }
193         if (getSubject() != null) {
194             target.setSubject(getSubject());
195         }
196         if (getText() != null) {
197             target.setText(getText());
198         }
199     }
200
201
202     public String JavaDoc toString() {
203         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("SimpleMailMessage: ");
204         sb.append("from=").append(this.from).append("; ");
205         sb.append("replyTo=").append(this.replyTo).append("; ");
206         sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(this.to)).append("; ");
207         sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(this.cc)).append("; ");
208         sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(this.bcc)).append("; ");
209         sb.append("sentDate=").append(this.sentDate).append("; ");
210         sb.append("subject=").append(this.subject).append("; ");
211         sb.append("text=").append(this.text);
212         return sb.toString();
213     }
214
215     public boolean equals(Object JavaDoc other) {
216         if (this == other) {
217             return true;
218         }
219         if (!(other instanceof SimpleMailMessage)) {
220             return false;
221         }
222         SimpleMailMessage otherMessage = (SimpleMailMessage) other;
223         return (ObjectUtils.nullSafeEquals(this.from, otherMessage.from) &&
224                 ObjectUtils.nullSafeEquals(this.replyTo, otherMessage.replyTo) &&
225                 java.util.Arrays.equals(this.to, otherMessage.to) &&
226                 java.util.Arrays.equals(this.cc, otherMessage.cc) &&
227                 java.util.Arrays.equals(this.bcc, otherMessage.bcc) &&
228                 ObjectUtils.nullSafeEquals(this.sentDate, otherMessage.sentDate) &&
229                 ObjectUtils.nullSafeEquals(this.subject, otherMessage.subject) &&
230                 ObjectUtils.nullSafeEquals(this.text, otherMessage.text));
231     }
232
233     public int hashCode() {
234         int hashCode = (this.from == null ? 0 : this.from.hashCode());
235         hashCode = 29 * hashCode + (this.replyTo == null ? 0 : this.replyTo.hashCode());
236         for (int i = 0; this.to != null && i < this.to.length; i++) {
237             hashCode = 29 * hashCode + (this.to == null ? 0 : this.to[i].hashCode());
238         }
239         for (int i = 0; this.cc != null && i < this.cc.length; i++) {
240             hashCode = 29 * hashCode + (this.cc == null ? 0 : this.cc[i].hashCode());
241         }
242         for (int i = 0; this.bcc != null && i < this.bcc.length; i++) {
243             hashCode = 29 * hashCode + (this.bcc == null ? 0 : this.bcc[i].hashCode());
244         }
245         hashCode = 29 * hashCode + (this.sentDate == null ? 0 : this.sentDate.hashCode());
246         hashCode = 29 * hashCode + (this.subject == null ? 0 : this.subject.hashCode());
247         hashCode = 29 * hashCode + (this.text == null ? 0 : this.text.hashCode());
248         return hashCode;
249     }
250
251
252     private static String JavaDoc[] copy(String JavaDoc[] state) {
253         String JavaDoc[] copy = new String JavaDoc[state.length];
254         System.arraycopy(state, 0, copy, 0, state.length);
255         return copy;
256     }
257
258 }
259
Popular Tags