View Javadoc

1   /************************************************************************
2    * Copyright (c) 1999-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.smtpserver;
19  
20  import org.apache.avalon.framework.logger.AbstractLogEnabled;
21  import org.apache.avalon.framework.service.ServiceException;
22  import org.apache.avalon.framework.service.ServiceManager;
23  import org.apache.avalon.framework.service.Serviceable;
24  import org.apache.james.services.MailServer;
25  import org.apache.james.util.mail.dsn.DSNStatus;
26  import org.apache.mailet.Mail;
27  
28  import javax.mail.MessagingException;
29  
30  import java.util.Collection;
31  
32  
33  /***
34    * Adds the header to the message
35    */
36  public class SendMailHandler
37      extends AbstractLogEnabled
38      implements MessageHandler, Serviceable {
39  
40      private MailServer mailServer;
41  
42      /***
43       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
44       */
45      public void service(ServiceManager serviceManager) throws ServiceException {
46          mailServer = (MailServer) serviceManager.lookup(MailServer.ROLE);
47      }
48  
49      /***
50       * Adds header to the message
51       * @see org.apache.james.smtpserver#onMessage(SMTPSession)
52       */
53      public void onMessage(SMTPSession session) {
54          getLogger().debug("sending mail");
55  
56          Mail mail = session.getMail();
57          
58          String responseString = null;
59          try {
60              mailServer.sendMail(mail);
61              Collection theRecipients = mail.getRecipients();
62              String recipientString = "";
63              if (theRecipients != null) {
64                  recipientString = theRecipients.toString();
65              }
66              if (getLogger().isInfoEnabled()) {
67                  StringBuffer infoBuffer =
68                       new StringBuffer(256)
69                           .append("Successfully spooled mail ")
70                           .append(mail.getName())
71                           .append(" from ")
72                           .append(mail.getSender())
73                           .append(" on ")
74                           .append(session.getRemoteIPAddress())
75                           .append(" for ")
76                           .append(recipientString);
77                  getLogger().info(infoBuffer.toString());
78              }
79           } catch (MessagingException me) {
80                // Grab any exception attached to this one.
81                Exception e = me.getNextException();
82                // If there was an attached exception, and it's a
83                // MessageSizeException
84                if (e != null && e instanceof MessageSizeException) {
85                     // Add an item to the state to suppress
86                     // logging of extra lines of data
87                     // that are sent after the size limit has
88                     // been hit.
89                     session.getState().put(SMTPSession.MESG_FAILED, Boolean.TRUE);
90                     // then let the client know that the size
91                     // limit has been hit.
92                     responseString = "552 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SYSTEM_MSG_TOO_BIG)+" Error processing message.";
93                     StringBuffer errorBuffer =
94                       new StringBuffer(256)
95                           .append("Rejected message from ")
96                           .append(session.getState().get(SMTPSession.SENDER).toString())
97                           .append(" from host ")
98                           .append(session.getRemoteHost())
99                           .append(" (")
100                          .append(session.getRemoteIPAddress())
101                          .append(") exceeding system maximum message size of ")
102                          .append(session.getConfigurationData().getMaxMessageSize());
103                    getLogger().error(errorBuffer.toString());
104               } else {
105                    responseString = "451 "+DSNStatus.getStatus(DSNStatus.TRANSIENT,DSNStatus.UNDEFINED_STATUS)+" Error processing message.";
106                    getLogger().error("Unknown error occurred while processing DATA.", me);
107               }
108               session.writeResponse(responseString);
109               return;
110          }
111          responseString = "250 "+DSNStatus.getStatus(DSNStatus.SUCCESS,DSNStatus.CONTENT_OTHER)+" Message received";
112          session.writeResponse(responseString);
113 
114     
115     }
116 
117 }