1 16 17 package org.springframework.mail; 18 19 import java.io.Serializable ; 20 import java.util.Date ; 21 22 import org.springframework.util.ObjectUtils; 23 import org.springframework.util.StringUtils; 24 import org.springframework.util.Assert; 25 26 42 public class SimpleMailMessage implements MailMessage, Serializable { 43 44 private String from; 45 46 private String replyTo; 47 48 private String [] to; 49 50 private String [] cc; 51 52 private String [] bcc; 53 54 private Date sentDate; 55 56 private String subject; 57 58 private String text; 59 60 61 64 public SimpleMailMessage() { 65 } 66 67 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 from) { 92 this.from = from; 93 } 94 95 public String getFrom() { 96 return this.from; 97 } 98 99 public void setReplyTo(String replyTo) { 100 this.replyTo = replyTo; 101 } 102 103 public String getReplyTo() { 104 return replyTo; 105 } 106 107 public void setTo(String to) { 108 this.to = new String [] {to}; 109 } 110 111 public void setTo(String [] to) { 112 this.to = to; 113 } 114 115 public String [] getTo() { 116 return this.to; 117 } 118 119 public void setCc(String cc) { 120 this.cc = new String [] {cc}; 121 } 122 123 public void setCc(String [] cc) { 124 this.cc = cc; 125 } 126 127 public String [] getCc() { 128 return cc; 129 } 130 131 public void setBcc(String bcc) { 132 this.bcc = new String [] {bcc}; 133 } 134 135 public void setBcc(String [] bcc) { 136 this.bcc = bcc; 137 } 138 139 public String [] getBcc() { 140 return bcc; 141 } 142 143 public void setSentDate(Date sentDate) { 144 this.sentDate = sentDate; 145 } 146 147 public Date getSentDate() { 148 return sentDate; 149 } 150 151 public void setSubject(String subject) { 152 this.subject = subject; 153 } 154 155 public String getSubject() { 156 return this.subject; 157 } 158 159 public void setText(String text) { 160 this.text = text; 161 } 162 163 public String getText() { 164 return this.text; 165 } 166 167 168 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 toString() { 203 StringBuffer sb = new StringBuffer ("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 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 [] copy(String [] state) { 253 String [] copy = new String [state.length]; 254 System.arraycopy(state, 0, copy, 0, state.length); 255 return copy; 256 } 257 258 } 259 | Popular Tags |