001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mail.cos;
018:
019: import java.io.IOException;
020: import java.io.PrintStream;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import com.oreilly.servlet.MailMessage;
025:
026: import org.springframework.mail.MailException;
027: import org.springframework.mail.MailParseException;
028: import org.springframework.mail.MailSendException;
029: import org.springframework.mail.MailSender;
030: import org.springframework.mail.SimpleMailMessage;
031:
032: /**
033: * Simple implementation of SMTP mail sending on top of Jason Hunter's
034: * MailMessage class that's included in
035: * <a href="http://servlets.com/cos">COS (com.oreilly.servlet)</a>.
036: *
037: * <p>Does not support any richer functionality than MailSender and
038: * SimpleMailMessage, therefore there's no optional richer interface like
039: * the JavaMailSender interface for the JavaMailSenderImpl implementation.
040: *
041: * <p>Does not support "replyTo" and "sentDate" fields; will consequently
042: * throw an exception when encountering either of those.
043: *
044: * @author Juergen Hoeller
045: * @since 09.10.2003
046: * @see com.oreilly.servlet.MailMessage
047: * @see org.springframework.mail.javamail.JavaMailSenderImpl
048: */
049: public class CosMailSenderImpl implements MailSender {
050:
051: private String host;
052:
053: /**
054: * Set the SMTP mail host.
055: */
056: public void setHost(String host) {
057: this .host = host;
058: }
059:
060: public void send(SimpleMailMessage simpleMessage)
061: throws MailException {
062: send(new SimpleMailMessage[] { simpleMessage });
063: }
064:
065: public void send(SimpleMailMessage[] simpleMessages)
066: throws MailException {
067: Map failedMessages = new HashMap();
068:
069: for (int i = 0; i < simpleMessages.length; i++) {
070: SimpleMailMessage simpleMessage = simpleMessages[i];
071:
072: if (simpleMessage.getReplyTo() != null) {
073: throw new MailParseException(
074: "CosMailSenderImpl does not support replyTo field - "
075: + simpleMessage);
076: }
077: if (simpleMessage.getSentDate() != null) {
078: throw new MailParseException(
079: "CosMailSenderImpl does not support sentDate field - "
080: + simpleMessage);
081: }
082:
083: try {
084: MailMessage cosMessage = new MailMessage(this .host);
085: cosMessage.from(simpleMessage.getFrom());
086: if (simpleMessage.getTo() != null) {
087: for (int j = 0; j < simpleMessage.getTo().length; j++) {
088: cosMessage.to(simpleMessage.getTo()[j]);
089: }
090: }
091: if (simpleMessage.getCc() != null) {
092: for (int j = 0; j < simpleMessage.getCc().length; j++) {
093: cosMessage.cc(simpleMessage.getCc()[j]);
094: }
095: }
096: if (simpleMessage.getBcc() != null) {
097: for (int j = 0; j < simpleMessage.getBcc().length; j++) {
098: cosMessage.bcc(simpleMessage.getBcc()[j]);
099: }
100: }
101: cosMessage.setSubject(simpleMessage.getSubject());
102: PrintStream textStream = cosMessage.getPrintStream();
103: textStream.print(simpleMessage.getText());
104: cosMessage.sendAndClose();
105: } catch (IOException ex) {
106: failedMessages.put(simpleMessage, ex);
107: }
108: }
109:
110: if (!failedMessages.isEmpty()) {
111: throw new MailSendException(failedMessages);
112: }
113: }
114:
115: }
|