Merge pull request #1144 from fzs/logSendFailedException

Improve logging when sending email fails.
diff --git a/src/main/java/com/gitblit/service/MailService.java b/src/main/java/com/gitblit/service/MailService.java
index ec3a84c..58acc9c 100644
--- a/src/main/java/com/gitblit/service/MailService.java
+++ b/src/main/java/com/gitblit/service/MailService.java
@@ -17,6 +17,7 @@
 

 import java.io.File;

 import java.util.ArrayList;

+import java.util.Arrays;

 import java.util.Date;

 import java.util.List;

 import java.util.Properties;

@@ -31,6 +32,7 @@
 import javax.mail.Message;

 import javax.mail.MessagingException;

 import javax.mail.PasswordAuthentication;

+import javax.mail.SendFailedException;

 import javax.mail.Session;

 import javax.mail.Transport;

 import javax.mail.internet.InternetAddress;

@@ -272,9 +274,22 @@
 				while ((message = queue.poll()) != null) {

 					try {

 						if (settings.getBoolean(Keys.mail.debug, false)) {

-							logger.info("send: " + StringUtils.trimString(message.getSubject(), 60));

+							logger.info("send: '" + StringUtils.trimString(message.getSubject(), 60)

+									    + "' to:" + StringUtils.trimString(Arrays.toString(message.getAllRecipients()), 300));

 						}

 						Transport.send(message);

+					} catch (SendFailedException sfe) {

+						if (settings.getBoolean(Keys.mail.debug, false)) {

+							logger.error("Failed to send message: {}", sfe.getMessage());

+							logger.info("   Invalid addresses: {}", Arrays.toString(sfe.getInvalidAddresses()));

+							logger.info("   Valid sent addresses: {}", Arrays.toString(sfe.getValidSentAddresses()));

+							logger.info("   Valid unset addresses: {}", Arrays.toString(sfe.getValidUnsentAddresses()));

+							logger.info("", sfe);

+						}

+						else {

+							logger.error("Failed to send message: {}", sfe.getMessage(), sfe.getNextException());

+						}

+						failures.add(message);

 					} catch (Throwable e) {

 						logger.error("Failed to send message", e);

 						failures.add(message);