View Javadoc

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