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.cos;
  17. import java.io.IOException;
  18. import java.io.PrintStream;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import com.oreilly.servlet.MailMessage;
  22. import org.springframework.mail.MailException;
  23. import org.springframework.mail.MailSendException;
  24. import org.springframework.mail.MailSender;
  25. import org.springframework.mail.SimpleMailMessage;
  26. /**
  27. * Simple implementation of SMTP mail sending on top of Jason Hunter's
  28. * MailMessage class that's included in
  29. * <a href="http://servlets.com/cos">COS (com.oreilly.servlet)</a>.
  30. *
  31. * <p>Does not support any richer functionality than MailSender and
  32. * SimpleMailMessage, therefore there's no optional richer interface like
  33. * the JavaMailSender interface for the JavaMailSenderImpl implementation.
  34. *
  35. * @author Juergen Hoeller
  36. * @since 09.10.2003
  37. * @see com.oreilly.servlet.MailMessage
  38. * @see org.springframework.mail.javamail.JavaMailSenderImpl
  39. * @version $Id: CosMailSenderImpl.java,v 1.6 2004/03/18 02:46:10 trisberg Exp $
  40. */
  41. public class CosMailSenderImpl implements MailSender {
  42. private String host;
  43. /**
  44. * Set the SMTP mail host.
  45. */
  46. public void setHost(String host) {
  47. this.host = host;
  48. }
  49. public void send(SimpleMailMessage simpleMessage) throws MailException {
  50. send(new SimpleMailMessage[] {simpleMessage});
  51. }
  52. public void send(SimpleMailMessage[] simpleMessages) throws MailException {
  53. Map failedMessages = new HashMap();
  54. for (int i = 0; i < simpleMessages.length; i++) {
  55. try {
  56. MailMessage cosMessage = new MailMessage(this.host);
  57. cosMessage.from(simpleMessages[i].getFrom());
  58. if (simpleMessages[i].getTo() != null) {
  59. for (int j = 0; j < simpleMessages[i].getTo().length; j++) {
  60. cosMessage.to(simpleMessages[i].getTo()[j]);
  61. }
  62. }
  63. if (simpleMessages[i].getCc() != null) {
  64. for (int j = 0; j < simpleMessages[i].getCc().length; j++) {
  65. cosMessage.cc(simpleMessages[i].getCc()[j]);
  66. }
  67. }
  68. if (simpleMessages[i].getBcc() != null) {
  69. for (int j = 0; j < simpleMessages[i].getBcc().length; j++) {
  70. cosMessage.bcc(simpleMessages[i].getBcc()[j]);
  71. }
  72. }
  73. cosMessage.setSubject(simpleMessages[i].getSubject());
  74. PrintStream textStream = cosMessage.getPrintStream();
  75. textStream.print(simpleMessages[i].getText());
  76. cosMessage.sendAndClose();
  77. }
  78. catch (IOException ex) {
  79. failedMessages.put(simpleMessages[i], ex);
  80. }
  81. }
  82. if (!failedMessages.isEmpty()) {
  83. throw new MailSendException(failedMessages);
  84. }
  85. }
  86. }