1 16 17 package org.springframework.mail.javamail; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.util.ArrayList ; 22 import java.util.Date ; 23 import java.util.HashMap ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 28 import javax.activation.FileTypeMap ; 29 import javax.mail.AuthenticationFailedException ; 30 import javax.mail.MessagingException ; 31 import javax.mail.NoSuchProviderException ; 32 import javax.mail.Session ; 33 import javax.mail.Transport ; 34 import javax.mail.internet.MimeMessage ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 import org.springframework.mail.MailAuthenticationException; 40 import org.springframework.mail.MailException; 41 import org.springframework.mail.MailParseException; 42 import org.springframework.mail.MailPreparationException; 43 import org.springframework.mail.MailSendException; 44 import org.springframework.mail.SimpleMailMessage; 45 import org.springframework.util.Assert; 46 47 73 public class JavaMailSenderImpl implements JavaMailSender { 74 75 78 public static final String DEFAULT_PROTOCOL = "smtp"; 79 80 83 public static final int DEFAULT_PORT = -1; 84 85 86 protected final Log logger = LogFactory.getLog(getClass()); 87 88 private Session session = Session.getInstance(new Properties (), null); 89 90 private String protocol = DEFAULT_PROTOCOL; 91 92 private String host; 93 94 private int port = DEFAULT_PORT; 95 96 private String username; 97 98 private String password; 99 100 private String defaultEncoding; 101 102 private FileTypeMap defaultFileTypeMap; 103 104 105 110 public JavaMailSenderImpl() { 111 ConfigurableMimeFileTypeMap fileTypeMap = new ConfigurableMimeFileTypeMap(); 112 fileTypeMap.afterPropertiesSet(); 113 this.defaultFileTypeMap = fileTypeMap; 114 } 115 116 123 public void setJavaMailProperties(Properties javaMailProperties) { 124 this.session = Session.getInstance(javaMailProperties, null); 125 } 126 127 135 public void setSession(Session session) { 136 Assert.notNull(session, "Session must not be null"); 137 this.session = session; 138 } 139 140 143 public Session getSession() { 144 return this.session; 145 } 146 147 150 public void setProtocol(String protocol) { 151 this.protocol = protocol; 152 } 153 154 157 public String getProtocol() { 158 return this.protocol; 159 } 160 161 165 public void setHost(String host) { 166 this.host = host; 167 } 168 169 172 public String getHost() { 173 return this.host; 174 } 175 176 181 public void setPort(int port) { 182 this.port = port; 183 } 184 185 188 public int getPort() { 189 return this.port; 190 } 191 192 203 public void setUsername(String username) { 204 this.username = username; 205 } 206 207 210 public String getUsername() { 211 return this.username; 212 } 213 214 225 public void setPassword(String password) { 226 this.password = password; 227 } 228 229 232 public String getPassword() { 233 return this.password; 234 } 235 236 241 public void setDefaultEncoding(String defaultEncoding) { 242 this.defaultEncoding = defaultEncoding; 243 } 244 245 249 public String getDefaultEncoding() { 250 return this.defaultEncoding; 251 } 252 253 266 public void setDefaultFileTypeMap(FileTypeMap defaultFileTypeMap) { 267 this.defaultFileTypeMap = defaultFileTypeMap; 268 } 269 270 274 public FileTypeMap getDefaultFileTypeMap() { 275 return this.defaultFileTypeMap; 276 } 277 278 279 283 public void send(SimpleMailMessage simpleMessage) throws MailException { 284 send(new SimpleMailMessage[] { simpleMessage }); 285 } 286 287 public void send(SimpleMailMessage[] simpleMessages) throws MailException { 288 List mimeMessages = new ArrayList (simpleMessages.length); 289 for (int i = 0; i < simpleMessages.length; i++) { 290 SimpleMailMessage simpleMessage = simpleMessages[i]; 291 if (logger.isDebugEnabled()) { 292 logger.debug("Creating new MIME message using the following mail properties: " + simpleMessage); 293 } 294 MimeMailMessage message = new MimeMailMessage(createMimeMessage()); 295 simpleMessage.copyTo(message); 296 mimeMessages.add(message.getMimeMessage()); 297 } 298 doSend((MimeMessage []) mimeMessages.toArray(new MimeMessage [mimeMessages.size()]), simpleMessages); 299 } 300 301 302 306 314 public MimeMessage createMimeMessage() { 315 return new SmartMimeMessage(getSession(), getDefaultEncoding(), getDefaultFileTypeMap()); 316 } 317 318 public MimeMessage createMimeMessage(InputStream contentStream) throws MailException { 319 try { 320 return new MimeMessage (getSession(), contentStream); 321 } 322 catch (MessagingException ex) { 323 throw new MailParseException("Could not parse raw MIME content", ex); 324 } 325 } 326 327 public void send(MimeMessage mimeMessage) throws MailException { 328 send(new MimeMessage [] { mimeMessage }); 329 } 330 331 public void send(MimeMessage [] mimeMessages) throws MailException { 332 doSend(mimeMessages, null); 333 } 334 335 public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException { 336 send(new MimeMessagePreparator[] { mimeMessagePreparator }); 337 } 338 339 public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException { 340 try { 341 List mimeMessages = new ArrayList (mimeMessagePreparators.length); 342 for (int i = 0; i < mimeMessagePreparators.length; i++) { 343 MimeMessage mimeMessage = createMimeMessage(); 344 mimeMessagePreparators[i].prepare(mimeMessage); 345 mimeMessages.add(mimeMessage); 346 } 347 send((MimeMessage []) mimeMessages.toArray(new MimeMessage [mimeMessages.size()])); 348 } 349 catch (MailException ex) { 350 throw ex; 351 } 352 catch (MessagingException ex) { 353 throw new MailParseException(ex); 354 } 355 catch (IOException ex) { 356 throw new MailPreparationException(ex); 357 } 358 catch (Exception ex) { 359 throw new MailPreparationException(ex); 360 } 361 } 362 363 364 375 protected void doSend(MimeMessage [] mimeMessages, Object [] originalMessages) throws MailException { 376 Map failedMessages = new HashMap (); 377 try { 378 Transport transport = getTransport(getSession()); 379 transport.connect(getHost(), getPort(), getUsername(), getPassword()); 380 try { 381 for (int i = 0; i < mimeMessages.length; i++) { 382 MimeMessage mimeMessage = mimeMessages[i]; 383 try { 384 if (mimeMessage.getSentDate() == null) { 385 mimeMessage.setSentDate(new Date ()); 386 } 387 mimeMessage.saveChanges(); 388 transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients()); 389 } 390 catch (MessagingException ex) { 391 Object original = (originalMessages != null ? originalMessages[i] : mimeMessage); 392 failedMessages.put(original, ex); 393 } 394 } 395 } 396 finally { 397 transport.close(); 398 } 399 } 400 catch (AuthenticationFailedException ex) { 401 throw new MailAuthenticationException(ex); 402 } 403 catch (MessagingException ex) { 404 throw new MailSendException("Mail server connection failed", ex); 405 } 406 if (!failedMessages.isEmpty()) { 407 throw new MailSendException(failedMessages); 408 } 409 } 410 411 418 protected Transport getTransport(Session session) throws NoSuchProviderException { 419 return session.getTransport(getProtocol()); 420 } 421 422 } 423 | Popular Tags |