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