KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mail > cos > CosMailSenderImpl


1 /*
2  * Copyright 2002-2005 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
17 package org.springframework.mail.cos;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintStream JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import com.oreilly.servlet.MailMessage;
25
26 import org.springframework.mail.MailException;
27 import org.springframework.mail.MailParseException;
28 import org.springframework.mail.MailSendException;
29 import org.springframework.mail.MailSender;
30 import org.springframework.mail.SimpleMailMessage;
31
32 /**
33  * Simple implementation of SMTP mail sending on top of Jason Hunter's
34  * MailMessage class that's included in
35  * <a HREF="http://servlets.com/cos">COS (com.oreilly.servlet)</a>.
36  *
37  * <p>Does not support any richer functionality than MailSender and
38  * SimpleMailMessage, therefore there's no optional richer interface like
39  * the JavaMailSender interface for the JavaMailSenderImpl implementation.
40  *
41  * <p>Does not support "replyTo" and "sentDate" fields; will consequently
42  * throw an exception when encountering either of those.
43  *
44  * @author Juergen Hoeller
45  * @since 09.10.2003
46  * @see com.oreilly.servlet.MailMessage
47  * @see org.springframework.mail.javamail.JavaMailSenderImpl
48  */

49 public class CosMailSenderImpl implements MailSender {
50
51     private String JavaDoc host;
52
53     /**
54      * Set the SMTP mail host.
55      */

56     public void setHost(String JavaDoc host) {
57         this.host = host;
58     }
59
60     public void send(SimpleMailMessage simpleMessage) throws MailException {
61         send(new SimpleMailMessage[] {simpleMessage});
62     }
63
64     public void send(SimpleMailMessage[] simpleMessages) throws MailException {
65         Map JavaDoc failedMessages = new HashMap JavaDoc();
66
67         for (int i = 0; i < simpleMessages.length; i++) {
68             SimpleMailMessage simpleMessage = simpleMessages[i];
69
70             if (simpleMessage.getReplyTo() != null) {
71                 throw new MailParseException("CosMailSenderImpl does not support replyTo field - " + simpleMessage);
72             }
73             if (simpleMessage.getSentDate() != null) {
74                 throw new MailParseException("CosMailSenderImpl does not support sentDate field - " + simpleMessage);
75             }
76
77             try {
78                 MailMessage cosMessage = new MailMessage(this.host);
79                 cosMessage.from(simpleMessage.getFrom());
80                 if (simpleMessage.getTo() != null) {
81                     for (int j = 0; j < simpleMessage.getTo().length; j++) {
82                         cosMessage.to(simpleMessage.getTo()[j]);
83                     }
84                 }
85                 if (simpleMessage.getCc() != null) {
86                     for (int j = 0; j < simpleMessage.getCc().length; j++) {
87                         cosMessage.cc(simpleMessage.getCc()[j]);
88                     }
89                 }
90                 if (simpleMessage.getBcc() != null) {
91                     for (int j = 0; j < simpleMessage.getBcc().length; j++) {
92                         cosMessage.bcc(simpleMessage.getBcc()[j]);
93                     }
94                 }
95                 cosMessage.setSubject(simpleMessage.getSubject());
96                 PrintStream JavaDoc textStream = cosMessage.getPrintStream();
97                 textStream.print(simpleMessage.getText());
98                 cosMessage.sendAndClose();
99             }
100             catch (IOException JavaDoc ex) {
101                 failedMessages.put(simpleMessage, ex);
102             }
103         }
104
105         if (!failedMessages.isEmpty()) {
106             throw new MailSendException(failedMessages);
107         }
108     }
109
110 }
111
Popular Tags