001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mail;
018:
019: import java.io.Serializable;
020: import java.util.Date;
021:
022: import org.springframework.util.ObjectUtils;
023: import org.springframework.util.StringUtils;
024: import org.springframework.util.Assert;
025:
026: /**
027: * Models a simple mail message, including data such as the from, to, cc, subject, and text fields.
028: *
029: * <p>Consider <code>JavaMailSender</code> and JavaMail <code>MimeMessages</code> for creating
030: * more sophisticated messages, for example messages with attachments, special
031: * character encodings, or personal names that accompany mail addresses.
032: *
033: * @author Dmitriy Kopylenko
034: * @author Juergen Hoeller
035: * @since 10.09.2003
036: * @see MailSender
037: * @see org.springframework.mail.javamail.JavaMailSender
038: * @see org.springframework.mail.javamail.MimeMessagePreparator
039: * @see org.springframework.mail.javamail.MimeMessageHelper
040: * @see org.springframework.mail.javamail.MimeMailMessage
041: */
042: public class SimpleMailMessage implements MailMessage, Serializable {
043:
044: private String from;
045:
046: private String replyTo;
047:
048: private String[] to;
049:
050: private String[] cc;
051:
052: private String[] bcc;
053:
054: private Date sentDate;
055:
056: private String subject;
057:
058: private String text;
059:
060: /**
061: * Create a new <code>SimpleMailMessage</code>.
062: */
063: public SimpleMailMessage() {
064: }
065:
066: /**
067: * Copy constructor for creating a new <code>SimpleMailMessage</code> from the state
068: * of an existing <code>SimpleMailMessage</code> instance.
069: * @throws IllegalArgumentException if the supplied message is <code>null</code>
070: */
071: public SimpleMailMessage(SimpleMailMessage original) {
072: Assert.notNull(original,
073: "The 'original' message argument cannot be null");
074: this .from = original.getFrom();
075: this .replyTo = original.getReplyTo();
076: if (original.getTo() != null) {
077: this .to = copy(original.getTo());
078: }
079: if (original.getCc() != null) {
080: this .cc = copy(original.getCc());
081: }
082: if (original.getBcc() != null) {
083: this .bcc = copy(original.getBcc());
084: }
085: this .sentDate = original.getSentDate();
086: this .subject = original.getSubject();
087: this .text = original.getText();
088: }
089:
090: public void setFrom(String from) {
091: this .from = from;
092: }
093:
094: public String getFrom() {
095: return this .from;
096: }
097:
098: public void setReplyTo(String replyTo) {
099: this .replyTo = replyTo;
100: }
101:
102: public String getReplyTo() {
103: return replyTo;
104: }
105:
106: public void setTo(String to) {
107: this .to = new String[] { to };
108: }
109:
110: public void setTo(String[] to) {
111: this .to = to;
112: }
113:
114: public String[] getTo() {
115: return this .to;
116: }
117:
118: public void setCc(String cc) {
119: this .cc = new String[] { cc };
120: }
121:
122: public void setCc(String[] cc) {
123: this .cc = cc;
124: }
125:
126: public String[] getCc() {
127: return cc;
128: }
129:
130: public void setBcc(String bcc) {
131: this .bcc = new String[] { bcc };
132: }
133:
134: public void setBcc(String[] bcc) {
135: this .bcc = bcc;
136: }
137:
138: public String[] getBcc() {
139: return bcc;
140: }
141:
142: public void setSentDate(Date sentDate) {
143: this .sentDate = sentDate;
144: }
145:
146: public Date getSentDate() {
147: return sentDate;
148: }
149:
150: public void setSubject(String subject) {
151: this .subject = subject;
152: }
153:
154: public String getSubject() {
155: return this .subject;
156: }
157:
158: public void setText(String text) {
159: this .text = text;
160: }
161:
162: public String getText() {
163: return this .text;
164: }
165:
166: /**
167: * Copy the contents of this message to the given target message.
168: * @param target the <code>MailMessage</code> to copy to
169: * @throws IllegalArgumentException if the supplied <code>target</code> is <code>null</code>
170: */
171: public void copyTo(MailMessage target) {
172: Assert.notNull(target,
173: "The 'target' message argument cannot be null");
174: if (getFrom() != null) {
175: target.setFrom(getFrom());
176: }
177: if (getReplyTo() != null) {
178: target.setReplyTo(getReplyTo());
179: }
180: if (getTo() != null) {
181: target.setTo(getTo());
182: }
183: if (getCc() != null) {
184: target.setCc(getCc());
185: }
186: if (getBcc() != null) {
187: target.setBcc(getBcc());
188: }
189: if (getSentDate() != null) {
190: target.setSentDate(getSentDate());
191: }
192: if (getSubject() != null) {
193: target.setSubject(getSubject());
194: }
195: if (getText() != null) {
196: target.setText(getText());
197: }
198: }
199:
200: public String toString() {
201: StringBuffer sb = new StringBuffer("SimpleMailMessage: ");
202: sb.append("from=").append(this .from).append("; ");
203: sb.append("replyTo=").append(this .replyTo).append("; ");
204: sb.append("to=").append(
205: StringUtils.arrayToCommaDelimitedString(this .to))
206: .append("; ");
207: sb.append("cc=").append(
208: StringUtils.arrayToCommaDelimitedString(this .cc))
209: .append("; ");
210: sb.append("bcc=").append(
211: StringUtils.arrayToCommaDelimitedString(this .bcc))
212: .append("; ");
213: sb.append("sentDate=").append(this .sentDate).append("; ");
214: sb.append("subject=").append(this .subject).append("; ");
215: sb.append("text=").append(this .text);
216: return sb.toString();
217: }
218:
219: public boolean equals(Object other) {
220: if (this == other) {
221: return true;
222: }
223: if (!(other instanceof SimpleMailMessage)) {
224: return false;
225: }
226: SimpleMailMessage otherMessage = (SimpleMailMessage) other;
227: return (ObjectUtils
228: .nullSafeEquals(this .from, otherMessage.from)
229: && ObjectUtils.nullSafeEquals(this .replyTo,
230: otherMessage.replyTo)
231: && java.util.Arrays.equals(this .to, otherMessage.to)
232: && java.util.Arrays.equals(this .cc, otherMessage.cc)
233: && java.util.Arrays.equals(this .bcc, otherMessage.bcc)
234: && ObjectUtils.nullSafeEquals(this .sentDate,
235: otherMessage.sentDate)
236: && ObjectUtils.nullSafeEquals(this .subject,
237: otherMessage.subject) && ObjectUtils
238: .nullSafeEquals(this .text, otherMessage.text));
239: }
240:
241: public int hashCode() {
242: int hashCode = (this .from == null ? 0 : this .from.hashCode());
243: hashCode = 29 * hashCode
244: + (this .replyTo == null ? 0 : this .replyTo.hashCode());
245: for (int i = 0; this .to != null && i < this .to.length; i++) {
246: hashCode = 29 * hashCode
247: + (this .to == null ? 0 : this .to[i].hashCode());
248: }
249: for (int i = 0; this .cc != null && i < this .cc.length; i++) {
250: hashCode = 29 * hashCode
251: + (this .cc == null ? 0 : this .cc[i].hashCode());
252: }
253: for (int i = 0; this .bcc != null && i < this .bcc.length; i++) {
254: hashCode = 29 * hashCode
255: + (this .bcc == null ? 0 : this .bcc[i].hashCode());
256: }
257: hashCode = 29
258: * hashCode
259: + (this .sentDate == null ? 0 : this .sentDate.hashCode());
260: hashCode = 29 * hashCode
261: + (this .subject == null ? 0 : this .subject.hashCode());
262: hashCode = 29 * hashCode
263: + (this .text == null ? 0 : this .text.hashCode());
264: return hashCode;
265: }
266:
267: private static String[] copy(String[] state) {
268: String[] copy = new String[state.length];
269: System.arraycopy(state, 0, copy, 0, state.length);
270: return copy;
271: }
272:
273: }
|