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 package org.apache.james.smtpserver;
21
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.service.ServiceException;
24 import org.apache.avalon.framework.service.ServiceManager;
25 import org.apache.avalon.framework.service.Serviceable;
26 import org.apache.james.services.MailServer;
27 import org.apache.james.util.mail.dsn.DSNStatus;
28 import org.apache.mailet.Mail;
29
30 import javax.mail.MessagingException;
31
32 import java.util.Collection;
33
34
35 /***
36 * Adds the header to the message
37 */
38 public class SendMailHandler
39 extends AbstractLogEnabled
40 implements MessageHandler, Serviceable {
41
42 private MailServer mailServer;
43
44 /***
45 * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
46 */
47 public void service(ServiceManager serviceManager) throws ServiceException {
48 mailServer = (MailServer) serviceManager.lookup(MailServer.ROLE);
49 }
50
51 /***
52 * Adds header to the message
53 * @see org.apache.james.smtpserver#onMessage(SMTPSession)
54 */
55 public void onMessage(SMTPSession session) {
56 getLogger().debug("sending mail");
57
58 Mail mail = session.getMail();
59
60 String responseString = null;
61 try {
62 mailServer.sendMail(mail);
63 Collection theRecipients = mail.getRecipients();
64 String recipientString = "";
65 if (theRecipients != null) {
66 recipientString = theRecipients.toString();
67 }
68 if (getLogger().isInfoEnabled()) {
69 StringBuffer infoBuffer =
70 new StringBuffer(256)
71 .append("Successfully spooled mail ")
72 .append(mail.getName())
73 .append(" from ")
74 .append(mail.getSender())
75 .append(" on ")
76 .append(session.getRemoteIPAddress())
77 .append(" for ")
78 .append(recipientString);
79 getLogger().info(infoBuffer.toString());
80 }
81 } catch (MessagingException me) {
82
83 Exception e = me.getNextException();
84
85
86 if (e != null && e instanceof MessageSizeException) {
87
88
89
90
91 session.getState().put(SMTPSession.MESG_FAILED, Boolean.TRUE);
92
93
94 responseString = "552 "+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.SYSTEM_MSG_TOO_BIG)+" Error processing message.";
95 StringBuffer errorBuffer =
96 new StringBuffer(256)
97 .append("Rejected message from ")
98 .append(session.getState().get(SMTPSession.SENDER).toString())
99 .append(" from host ")
100 .append(session.getRemoteHost())
101 .append(" (")
102 .append(session.getRemoteIPAddress())
103 .append(") exceeding system maximum message size of ")
104 .append(session.getConfigurationData().getMaxMessageSize());
105 getLogger().error(errorBuffer.toString());
106 } else {
107 responseString = "451 "+DSNStatus.getStatus(DSNStatus.TRANSIENT,DSNStatus.UNDEFINED_STATUS)+" Error processing message.";
108 getLogger().error("Unknown error occurred while processing DATA.", me);
109 }
110 session.writeResponse(responseString);
111 return;
112 }
113 responseString = "250 "+DSNStatus.getStatus(DSNStatus.SUCCESS,DSNStatus.CONTENT_OTHER)+" Message received";
114 session.writeResponse(responseString);
115
116
117 }
118
119 }