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