1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -330,7 +330,7 @@ public MimeMessage createMimeMessage(InputStream contentStream) throws MailExcep
330
330
try {
331
331
return new MimeMessage (getSession (), contentStream );
332
332
}
333
- catch (MessagingException ex ) {
333
+ catch (Exception ex ) {
334
334
throw new MailParseException ("Could not parse raw MIME content" , ex );
335
335
}
336
336
}
@@ -385,35 +385,40 @@ public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailExc
385
385
* in case of failure when sending a message
386
386
*/
387
387
protected void doSend (MimeMessage [] mimeMessages , Object [] originalMessages ) throws MailException {
388
- String username = getUsername ();
389
- String password = getPassword ();
390
- if ("" .equals (username )) { // probably from a placeholder
391
- username = null ;
392
- if ("" .equals (password )) { // in conjunction with "" username, this means no password to use
393
- password = null ;
394
- }
395
- }
396
-
397
388
Map <Object , Exception > failedMessages = new LinkedHashMap <Object , Exception >();
398
- Transport transport ;
399
- try {
400
- transport = getTransport (getSession ());
401
- transport .connect (getHost (), getPort (), username , password );
402
- }
403
- catch (AuthenticationFailedException ex ) {
404
- throw new MailAuthenticationException (ex );
405
- }
406
- catch (MessagingException ex ) {
407
- // Effectively, all messages failed...
408
- for (int i = 0 ; i < mimeMessages .length ; i ++) {
409
- Object original = (originalMessages != null ? originalMessages [i ] : mimeMessages [i ]);
410
- failedMessages .put (original , ex );
411
- }
412
- throw new MailSendException ("Mail server connection failed" , ex , failedMessages );
413
- }
389
+ Transport transport = null ;
414
390
415
391
try {
416
392
for (int i = 0 ; i < mimeMessages .length ; i ++) {
393
+
394
+ // Check transport connection first...
395
+ if (transport == null || !transport .isConnected ()) {
396
+ if (transport != null ) {
397
+ try {
398
+ transport .close ();
399
+ }
400
+ catch (Exception ex ) {
401
+ // Ignore - we're reconnecting anyway
402
+ }
403
+ transport = null ;
404
+ }
405
+ try {
406
+ transport = connectTransport ();
407
+ }
408
+ catch (AuthenticationFailedException ex ) {
409
+ throw new MailAuthenticationException (ex );
410
+ }
411
+ catch (Exception ex ) {
412
+ // Effectively, all remaining messages failed...
413
+ for (int j = i ; j < mimeMessages .length ; j ++) {
414
+ Object original = (originalMessages != null ? originalMessages [j ] : mimeMessages [j ]);
415
+ failedMessages .put (original , ex );
416
+ }
417
+ throw new MailSendException ("Mail server connection failed" , ex , failedMessages );
418
+ }
419
+ }
420
+
421
+ // Send message via current transport...
417
422
MimeMessage mimeMessage = mimeMessages [i ];
418
423
try {
419
424
if (mimeMessage .getSentDate () == null ) {
@@ -427,17 +432,19 @@ protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) thr
427
432
}
428
433
transport .sendMessage (mimeMessage , mimeMessage .getAllRecipients ());
429
434
}
430
- catch (MessagingException ex ) {
435
+ catch (Exception ex ) {
431
436
Object original = (originalMessages != null ? originalMessages [i ] : mimeMessage );
432
437
failedMessages .put (original , ex );
433
438
}
434
439
}
435
440
}
436
441
finally {
437
442
try {
438
- transport .close ();
443
+ if (transport != null ) {
444
+ transport .close ();
445
+ }
439
446
}
440
- catch (MessagingException ex ) {
447
+ catch (Exception ex ) {
441
448
if (!failedMessages .isEmpty ()) {
442
449
throw new MailSendException ("Failed to close server connection after message failures" , ex ,
443
450
failedMessages );
@@ -453,11 +460,39 @@ protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) thr
453
460
}
454
461
}
455
462
463
+ /**
464
+ * Obtain and connect a Transport from the underlying JavaMail Session,
465
+ * passing in the specified host, port, username, and password.
466
+ * @return the connected Transport object
467
+ * @throws MessagingException if the connect attempt failed
468
+ * @since 4.1.2
469
+ * @see #getTransport
470
+ * @see #getHost()
471
+ * @see #getPort()
472
+ * @see #getUsername()
473
+ * @see #getPassword()
474
+ */
475
+ protected Transport connectTransport () throws MessagingException {
476
+ String username = getUsername ();
477
+ String password = getPassword ();
478
+ if ("" .equals (username )) { // probably from a placeholder
479
+ username = null ;
480
+ if ("" .equals (password )) { // in conjunction with "" username, this means no password to use
481
+ password = null ;
482
+ }
483
+ }
484
+
485
+ Transport transport = getTransport (getSession ());
486
+ transport .connect (getHost (), getPort (), username , password );
487
+ return transport ;
488
+ }
489
+
456
490
/**
457
491
* Obtain a Transport object from the given JavaMail Session,
458
492
* using the configured protocol.
459
493
* <p>Can be overridden in subclasses, e.g. to return a mock Transport object.
460
494
* @see javax.mail.Session#getTransport(String)
495
+ * @see #getSession()
461
496
* @see #getProtocol()
462
497
*/
463
498
protected Transport getTransport (Session session ) throws NoSuchProviderException {
0 commit comments