View Javadoc
1   /*
2    * Copyright 2002-2012 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;
20  import java.util.Date;
21  
22  import org.springframework.util.Assert;
23  import org.springframework.util.ObjectUtils;
24  import org.springframework.util.StringUtils;
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} and JavaMail {@code MimeMessages} 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  @SuppressWarnings("serial")
43  public class SimpleMailMessage implements MailMessage, Serializable {
44  
45  	private String from;
46  
47  	private String replyTo;
48  
49  	private String[] to;
50  
51  	private String[] cc;
52  
53  	private String[] bcc;
54  
55  	private Date sentDate;
56  
57  	private String subject;
58  
59  	private String text;
60  
61  
62  	/**
63  	 * Create a new {@code SimpleMailMessage}.
64  	 */
65  	public SimpleMailMessage() {
66  	}
67  
68  	/**
69  	 * Copy constructor for creating a new {@code SimpleMailMessage} from the state
70  	 * of an existing {@code SimpleMailMessage} instance.
71  	 * @throws IllegalArgumentException if the supplied message is {@code null}
72  	 */
73  	public SimpleMailMessage(SimpleMailMessage original) {
74  		Assert.notNull(original, "The 'original' message argument cannot be null");
75  		this.from = original.getFrom();
76  		this.replyTo = original.getReplyTo();
77  		if (original.getTo() != null) {
78  			this.to = copy(original.getTo());
79  		}
80  		if (original.getCc() != null) {
81  			this.cc = copy(original.getCc());
82  		}
83  		if (original.getBcc() != null) {
84  			this.bcc = copy(original.getBcc());
85  		}
86  		this.sentDate = original.getSentDate();
87  		this.subject = original.getSubject();
88  		this.text = original.getText();
89  	}
90  
91  
92  	@Override
93  	public void setFrom(String from) {
94  		this.from = from;
95  	}
96  
97  	public String getFrom() {
98  		return this.from;
99  	}
100 
101 	@Override
102 	public void setReplyTo(String replyTo) {
103 		this.replyTo = replyTo;
104 	}
105 
106 	public String getReplyTo() {
107 		return replyTo;
108 	}
109 
110 	@Override
111 	public void setTo(String to) {
112 		this.to = new String[] {to};
113 	}
114 
115 	@Override
116 	public void setTo(String[] to) {
117 		this.to = to;
118 	}
119 
120 	public String[] getTo() {
121 		return this.to;
122 	}
123 
124 	@Override
125 	public void setCc(String cc) {
126 		this.cc = new String[] {cc};
127 	}
128 
129 	@Override
130 	public void setCc(String[] cc) {
131 		this.cc = cc;
132 	}
133 
134 	public String[] getCc() {
135 		return cc;
136 	}
137 
138 	@Override
139 	public void setBcc(String bcc) {
140 		this.bcc = new String[] {bcc};
141 	}
142 
143 	@Override
144 	public void setBcc(String[] bcc) {
145 		this.bcc = bcc;
146 	}
147 
148 	public String[] getBcc() {
149 		return bcc;
150 	}
151 
152 	@Override
153 	public void setSentDate(Date sentDate) {
154 		this.sentDate = sentDate;
155 	}
156 
157 	public Date getSentDate() {
158 		return sentDate;
159 	}
160 
161 	@Override
162 	public void setSubject(String subject) {
163 		this.subject = subject;
164 	}
165 
166 	public String getSubject() {
167 		return this.subject;
168 	}
169 
170 	@Override
171 	public void setText(String text) {
172 		this.text = text;
173 	}
174 
175 	public String getText() {
176 		return this.text;
177 	}
178 
179 
180 	/**
181 	 * Copy the contents of this message to the given target message.
182 	 * @param target the {@code MailMessage} to copy to
183 	 * @throws IllegalArgumentException if the supplied {@code target} is {@code null}
184 	 */
185 	public void copyTo(MailMessage target) {
186 		Assert.notNull(target, "The 'target' message argument cannot be null");
187 		if (getFrom() != null) {
188 			target.setFrom(getFrom());
189 		}
190 		if (getReplyTo() != null) {
191 			target.setReplyTo(getReplyTo());
192 		}
193 		if (getTo() != null) {
194 			target.setTo(getTo());
195 		}
196 		if (getCc() != null) {
197 			target.setCc(getCc());
198 		}
199 		if (getBcc() != null) {
200 			target.setBcc(getBcc());
201 		}
202 		if (getSentDate() != null) {
203 			target.setSentDate(getSentDate());
204 		}
205 		if (getSubject() != null) {
206 			target.setSubject(getSubject());
207 		}
208 		if (getText() != null) {
209 			target.setText(getText());
210 		}
211 	}
212 
213 
214 	@Override
215 	public String toString() {
216 		StringBuilder sb = new StringBuilder("SimpleMailMessage: ");
217 		sb.append("from=").append(this.from).append("; ");
218 		sb.append("replyTo=").append(this.replyTo).append("; ");
219 		sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(this.to)).append("; ");
220 		sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(this.cc)).append("; ");
221 		sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(this.bcc)).append("; ");
222 		sb.append("sentDate=").append(this.sentDate).append("; ");
223 		sb.append("subject=").append(this.subject).append("; ");
224 		sb.append("text=").append(this.text);
225 		return sb.toString();
226 	}
227 
228 	@Override
229 	public boolean equals(Object other) {
230 		if (this == other) {
231 			return true;
232 		}
233 		if (!(other instanceof SimpleMailMessage)) {
234 			return false;
235 		}
236 		SimpleMailMessage otherMessage = (SimpleMailMessage) other;
237 		return (ObjectUtils.nullSafeEquals(this.from, otherMessage.from) &&
238 				ObjectUtils.nullSafeEquals(this.replyTo, otherMessage.replyTo) &&
239 				java.util.Arrays.equals(this.to, otherMessage.to) &&
240 				java.util.Arrays.equals(this.cc, otherMessage.cc) &&
241 				java.util.Arrays.equals(this.bcc, otherMessage.bcc) &&
242 				ObjectUtils.nullSafeEquals(this.sentDate, otherMessage.sentDate) &&
243 				ObjectUtils.nullSafeEquals(this.subject, otherMessage.subject) &&
244 				ObjectUtils.nullSafeEquals(this.text, otherMessage.text));
245 	}
246 
247 	@Override
248 	public int hashCode() {
249 		int hashCode = (this.from == null ? 0 : this.from.hashCode());
250 		hashCode = 29 * hashCode + (this.replyTo == null ? 0 : this.replyTo.hashCode());
251 		for (int i = 0; this.to != null && i < this.to.length; i++) {
252 			hashCode = 29 * hashCode + (this.to == null ? 0 : this.to[i].hashCode());
253 		}
254 		for (int i = 0; this.cc != null && i < this.cc.length; i++) {
255 			hashCode = 29 * hashCode + (this.cc == null ? 0 : this.cc[i].hashCode());
256 		}
257 		for (int i = 0; this.bcc != null && i < this.bcc.length; i++) {
258 			hashCode = 29 * hashCode + (this.bcc == null ? 0 : this.bcc[i].hashCode());
259 		}
260 		hashCode = 29 * hashCode + (this.sentDate == null ? 0 : this.sentDate.hashCode());
261 		hashCode = 29 * hashCode + (this.subject == null ? 0 : this.subject.hashCode());
262 		hashCode = 29 * hashCode + (this.text == null ? 0 : this.text.hashCode());
263 		return hashCode;
264 	}
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 }