1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
42
43 public class SendMailHandler
44 extends AbstractLogEnabled
45 implements MessageHandler, Serviceable {
46
47 private MailServer mailServer;
48
49
50
51
52 public void service(ServiceManager serviceManager) throws ServiceException {
53 mailServer = (MailServer) serviceManager.lookup(MailServer.ROLE);
54 }
55
56
57
58
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
88 Exception e = me.getNextException();
89
90
91 if (e != null && e instanceof MessageSizeException) {
92
93
94
95
96 session.getState().put(SMTPSession.MESG_FAILED, Boolean.TRUE);
97
98
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 }