📄 smtphandler.java
字号:
theWatchdog, theConfigData.getResetLength()); // if the message size limit has been set, we'll // wrap msgIn with a SizeLimitedInputStream long maxMessageSize = theConfigData.getMaxMessageSize(); if (maxMessageSize > 0) { if (getLogger().isDebugEnabled()) { StringBuffer logBuffer = new StringBuffer(128) .append("Using SizeLimitedInputStream ") .append(" with max message size: ") .append(maxMessageSize); getLogger().debug(logBuffer.toString()); } msgIn = new SizeLimitedInputStream(msgIn, maxMessageSize); } // Removes the dot stuffing msgIn = new DotStuffingInputStream(msgIn); // Parse out the message headers MailHeaders headers = new MailHeaders(msgIn); headers = processMailHeaders(headers); processMail(headers, msgIn); headers = null; } catch (MessagingException me) { // Grab any exception attached to this one. Exception e = me.getNextException(); // If there was an attached exception, and it's a // MessageSizeException if (e != null && e instanceof MessageSizeException) { // Add an item to the state to suppress // logging of extra lines of data // that are sent after the size limit has // been hit. state.put(MESG_FAILED, Boolean.TRUE); // then let the client know that the size // limit has been hit. responseString = "552 Error processing message: " + e.getMessage(); StringBuffer errorBuffer = new StringBuffer(256) .append("Rejected message from ") .append(state.get(SENDER).toString()) .append(" from host ") .append(remoteHost) .append(" (") .append(remoteIP) .append(") exceeding system maximum message size of ") .append(theConfigData.getMaxMessageSize()); getLogger().error(errorBuffer.toString()); } else { responseString = "451 Error processing message: " + me.getMessage(); getLogger().error("Unknown error occurred while processing DATA.", me); } writeLoggedFlushedResponse(responseString); return; } finally { if (msgIn != null) { try { msgIn.close(); } catch (Exception e) { // Ignore close exception } msgIn = null; } } resetState(); responseString = "250 Message received"; writeLoggedFlushedResponse(responseString); } } private MailHeaders processMailHeaders(MailHeaders headers) throws MessagingException { // If headers do not contains minimum REQUIRED headers fields, // add them if (!headers.isSet(RFC2822Headers.DATE)) { headers.setHeader(RFC2822Headers.DATE, rfc822DateFormat.format(new Date())); } if (!headers.isSet(RFC2822Headers.FROM) && state.get(SENDER) != null) { headers.setHeader(RFC2822Headers.FROM, state.get(SENDER).toString()); } // Determine the Return-Path String returnPath = headers.getHeader(RFC2822Headers.RETURN_PATH, "\r\n"); headers.removeHeader(RFC2822Headers.RETURN_PATH); StringBuffer headerLineBuffer = new StringBuffer(512); if (returnPath == null) { if (state.get(SENDER) == null) { returnPath = "<>"; } else { headerLineBuffer.append("<") .append(state.get(SENDER)) .append(">"); returnPath = headerLineBuffer.toString(); headerLineBuffer.delete(0, headerLineBuffer.length()); } } // We will rebuild the header object to put Return-Path and our // Received header at the top Enumeration headerLines = headers.getAllHeaderLines(); MailHeaders newHeaders = new MailHeaders(); // Put the Return-Path first // JAMES-281 fix for messages that improperly have multiple // Return-Path headers StringTokenizer tokenizer = new StringTokenizer(returnPath, "\r\n"); while(tokenizer.hasMoreTokens()) { String path = tokenizer.nextToken(); newHeaders.addHeaderLine(RFC2822Headers.RETURN_PATH + ": " + path); } // Put our Received header next headerLineBuffer.append(RFC2822Headers.RECEIVED + ": from ") .append(remoteHost) .append(" ([") .append(remoteIP) .append("])"); newHeaders.addHeaderLine(headerLineBuffer.toString()); headerLineBuffer.delete(0, headerLineBuffer.length()); headerLineBuffer.append(" by ") .append(theConfigData.getHelloName()) .append(" (") .append(SOFTWARE_TYPE) .append(") with SMTP ID ") .append(smtpID); if (((Collection) state.get(RCPT_LIST)).size() == 1) { // Only indicate a recipient if they're the only recipient // (prevents email address harvesting and large headers in // bulk email) newHeaders.addHeaderLine(headerLineBuffer.toString()); headerLineBuffer.delete(0, headerLineBuffer.length()); headerLineBuffer.append(" for <") .append(((List) state.get(RCPT_LIST)).get(0).toString()) .append(">;"); newHeaders.addHeaderLine(headerLineBuffer.toString()); headerLineBuffer.delete(0, headerLineBuffer.length()); } else { // Put the ; on the end of the 'by' line headerLineBuffer.append(";"); newHeaders.addHeaderLine(headerLineBuffer.toString()); headerLineBuffer.delete(0, headerLineBuffer.length()); } headerLineBuffer = null; newHeaders.addHeaderLine(" " + rfc822DateFormat.format(new Date())); // Add all the original message headers back in next while (headerLines.hasMoreElements()) { newHeaders.addHeaderLine((String) headerLines.nextElement()); } return newHeaders; } /** * Processes the mail message coming in off the wire. Reads the * content and delivers to the spool. * * @param headers the headers of the mail being read * @param msgIn the stream containing the message content */ private void processMail(MailHeaders headers, InputStream msgIn) throws MessagingException { ByteArrayInputStream headersIn = null; MailImpl mail = null; List recipientCollection = null; try { headersIn = new ByteArrayInputStream(headers.toByteArray()); recipientCollection = (List) state.get(RCPT_LIST); mail = new MailImpl(theConfigData.getMailServer().getId(), (MailAddress) state.get(SENDER), recipientCollection, new SequenceInputStream(headersIn, msgIn)); // Call mail.getSize() to force the message to be // loaded. Need to do this to enforce the size limit if (theConfigData.getMaxMessageSize() > 0) { mail.getMessageSize(); } mail.setRemoteHost(remoteHost); mail.setRemoteAddr(remoteIP); if (getUser() != null) { mail.setAttribute(SMTP_AUTH_USER_ATTRIBUTE_NAME, getUser()); } theConfigData.getMailServer().sendMail(mail); Collection theRecipients = mail.getRecipients(); String recipientString = ""; if (theRecipients != null) { recipientString = theRecipients.toString(); } if (getLogger().isInfoEnabled()) { StringBuffer infoBuffer = new StringBuffer(256) .append("Successfully spooled mail ") .append(mail.getName()) .append(" from ") .append(mail.getSender()) .append(" for ") .append(recipientString); getLogger().info(infoBuffer.toString()); } } finally { if (recipientCollection != null) { recipientCollection.clear(); } recipientCollection = null; if (mail != null) { mail.dispose(); } mail = null; if (headersIn != null) { try { headersIn.close(); } catch (IOException ioe) { // Ignore exception on close. } } headersIn = null; } } /** * Handler method called upon receipt of a QUIT command. * This method informs the client that the connection is * closing. * * @param argument the argument passed in with the command by the SMTP client */ private void doQUIT(String argument) { String responseString = ""; if ((argument == null) || (argument.length() == 0)) { responseBuffer.append("221 ") .append(theConfigData.getHelloName()) .append(" Service closing transmission channel"); responseString = clearResponseBuffer(); } else { responseString = "500 Unexpected argument provided with QUIT command"; } writeLoggedFlushedResponse(responseString); } /** * Handler method called upon receipt of a VRFY command. * This method informs the client that the command is * not implemented. * * @param argument the argument passed in with the command by the SMTP client */ private void doVRFY(String argument) { String responseString = "502 VRFY is not supported"; writeLoggedFlushedResponse(responseString); } /** * Handler method called upon receipt of a EXPN command. * This method informs the client that the command is * not implemented. * * @param argument the argument passed in with the command by the SMTP client */ private void doEXPN(String argument) { String responseString = "502 EXPN is not supported"; writeLoggedFlushedResponse(responseString); } /** * Handler method called upon receipt of a HELP command. * This method informs the client that the command is * not implemented. * * @param argument the argument passed in with the command by the SMTP client */ private void doHELP(String argument) { String responseString = "502 HELP is not supported"; writeLoggedFlushedResponse(responseString); } /** * Handler method called upon receipt of an unrecognized command. * Returns an error response and logs the command. * * @param command the command parsed by the SMTP client * @param argument the argument passed in with the command by the SMTP client */ private void doUnknownCmd(String command, String argument) { responseBuffer.append("500 ") .append(theConfigData.getHelloName()) .append(" Syntax error, command unrecognized: ") .append(command); String responseString = clearResponseBuffer(); writeLoggedFlushedResponse(responseString); } /** * A private inner class which serves as an adaptor * between the WatchdogTarget interface and this * handler class. */ private class SMTPWatchdogTarget implements WatchdogTarget { /** * @see org.apache.james.util.watchdog.WatchdogTarget#execute() */ public void execute() { SMTPHandler.this.idleClose(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -