1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.mail;
  17. import java.io.PrintStream;
  18. import java.io.PrintWriter;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.Map;
  22. /**
  23. * Exception thrown when a mail sending error is encountered.
  24. * Can register failed messages with their exceptions.
  25. * @author Dmitriy Kopylenko
  26. * @author Juergen Hoeller
  27. * @version $Id: MailSendException.java,v 1.6 2004/03/18 02:46:05 trisberg Exp $
  28. */
  29. public class MailSendException extends MailException {
  30. private Map failedMessages = new HashMap();
  31. public MailSendException(String msg) {
  32. super(msg);
  33. }
  34. public MailSendException(String msg, Throwable ex) {
  35. super(msg, ex);
  36. }
  37. /**
  38. * Constructor for registration of failed messages, with the
  39. * messages that failed as keys, and the thrown exceptions as values.
  40. * <p>The messages should be the same that were originally passed
  41. * to the invoked send method.
  42. */
  43. public MailSendException(Map failedMessages) {
  44. super(null);
  45. this.failedMessages.putAll(failedMessages);
  46. }
  47. /**
  48. * Return a Map with the failed messages as keys, and the thrown exceptions
  49. * as values. Note that a general mail server connection failure will not
  50. * result in failed messages being returned here: A message will only be
  51. * contained here if actually sending it was attempted but failed.
  52. * <p>The messages will be the same that were originally passed to the
  53. * invoked send method, i.e. SimpleMailMessages in case of using the
  54. * generic MailSender interface.
  55. * <p>In case of sending MimeMessage instances via JavaMailSender,
  56. * the messages will be of type MimeMessage.
  57. * @return the Map of failed messages as keys and thrown exceptions as
  58. * values, or an empty Map if no failed messages
  59. */
  60. public Map getFailedMessages() {
  61. return failedMessages;
  62. }
  63. public String getMessage() {
  64. StringBuffer msg = new StringBuffer();
  65. String superMsg = super.getMessage();
  66. msg.append(superMsg != null ? superMsg : "Could not send mails: ");
  67. for (Iterator subExs = this.failedMessages.values().iterator(); subExs.hasNext();) {
  68. Exception subEx = (Exception) subExs.next();
  69. msg.append(subEx.getMessage());
  70. if (subExs.hasNext()) {
  71. msg.append("; ");
  72. }
  73. }
  74. return msg.toString();
  75. }
  76. public void printStackTrace(PrintStream ps) {
  77. if (this.failedMessages.isEmpty()) {
  78. super.printStackTrace(ps);
  79. }
  80. else {
  81. ps.println(this);
  82. for (Iterator subExs = this.failedMessages.values().iterator(); subExs.hasNext();) {
  83. Exception subEx = (Exception) subExs.next();
  84. subEx.printStackTrace(ps);
  85. if (subExs.hasNext()) {
  86. ps.println();
  87. }
  88. }
  89. }
  90. }
  91. public void printStackTrace(PrintWriter pw) {
  92. if (this.failedMessages.isEmpty()) {
  93. super.printStackTrace(pw);
  94. }
  95. else {
  96. pw.println(this);
  97. for (Iterator subExs = this.failedMessages.values().iterator(); subExs.hasNext();) {
  98. Exception subEx = (Exception) subExs.next();
  99. subEx.printStackTrace(pw);
  100. if (subExs.hasNext()) {
  101. pw.println();
  102. }
  103. }
  104. }
  105. }
  106. }