📄 remotedelivery.java
字号:
try { if (getInitParameter("timeout") != null) { smtpTimeout = Integer.parseInt(getInitParameter("timeout")); } } catch (Exception e) { log("Invalid timeout setting: " + getInitParameter("timeout")); } try { if (getInitParameter("connectiontimeout") != null) { connectionTimeout = Integer.parseInt(getInitParameter("connectiontimeout")); } } catch (Exception e) { log("Invalid timeout setting: " + getInitParameter("timeout")); } sendPartial = (getInitParameter("sendpartial") == null) ? false : new Boolean(getInitParameter("sendpartial")).booleanValue(); bounceProcessor = getInitParameter("bounceProcessor"); String gateway = getInitParameter("gateway"); String gatewayPort = getInitParameter("gatewayPort"); if (gateway != null) { gatewayServer = new ArrayList(); StringTokenizer st = new StringTokenizer(gateway, ",") ; while (st.hasMoreTokens()) { String server = st.nextToken().trim() ; if (server.indexOf(':') < 0 && gatewayPort != null) { server += ":"; server += gatewayPort; } if (isDebug) log("Adding SMTP gateway: " + server) ; gatewayServer.add(server); } authUser = getInitParameter("gatewayusername"); authPass = getInitParameter("gatewayPassword"); } ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER); String outgoingPath = getInitParameter("outgoing"); if (outgoingPath == null) { outgoingPath = "file:///../var/mail/outgoing"; } try { // Instantiate the a MailRepository for outgoing mails Store mailstore = (Store) compMgr.lookup(Store.ROLE); DefaultConfiguration spoolConf = new DefaultConfiguration("repository", "generated:RemoteDelivery.java"); spoolConf.setAttribute("destinationURL", outgoingPath); spoolConf.setAttribute("type", "SPOOL"); outgoing = (SpoolRepository) mailstore.select(spoolConf); } catch (ServiceException cnfe) { log("Failed to retrieve Store component:" + cnfe.getMessage()); } catch (Exception e) { log("Failed to retrieve Store component:" + e.getMessage()); } //Start up a number of threads try { deliveryThreadCount = Integer.parseInt(getInitParameter("deliveryThreads")); } catch (Exception e) { } for (int i = 0; i < deliveryThreadCount; i++) { StringBuffer nameBuffer = new StringBuffer(32) .append("Remote delivery thread (") .append(i) .append(")"); Thread t = new Thread(this, nameBuffer.toString()); t.start(); deliveryThreads.add(t); } bindAddress = getInitParameter("bind"); isBindUsed = bindAddress != null; try { if (isBindUsed) RemoteDeliverySocketFactory.setBindAdress(bindAddress); } catch (UnknownHostException e) { log("Invalid bind setting (" + bindAddress + "): " + e.toString()); } Iterator i = getInitParameterNames(); while (i.hasNext()) { String name = (String) i.next(); if (name.startsWith("mail.")) { defprops.put(name,getInitParameter(name)); } } } /* * private method to log the extended SendFailedException introduced in JavaMail 1.3.2. */ private void logSendFailedException(SendFailedException sfe) { if (isDebug) { MessagingException me = sfe; if (me instanceof SMTPSendFailedException) { SMTPSendFailedException ssfe = (SMTPSendFailedException)me; log("SMTP SEND FAILED:"); log(ssfe.toString()); log(" Command: " + ssfe.getCommand()); log(" RetCode: " + ssfe.getReturnCode()); log(" Response: " + ssfe.getMessage()); } else { log("Send failed: " + me.toString()); } Exception ne; while ((ne = me.getNextException()) != null && ne instanceof MessagingException) { me = (MessagingException)ne; if (me instanceof SMTPAddressFailedException) { SMTPAddressFailedException e = (SMTPAddressFailedException)me; log("ADDRESS FAILED:"); log(e.toString()); log(" Address: " + e.getAddress()); log(" Command: " + e.getCommand()); log(" RetCode: " + e.getReturnCode()); log(" Response: " + e.getMessage()); } else if (me instanceof SMTPAddressSucceededException) { log("ADDRESS SUCCEEDED:"); SMTPAddressSucceededException e = (SMTPAddressSucceededException)me; log(e.toString()); log(" Address: " + e.getAddress()); log(" Command: " + e.getCommand()); log(" RetCode: " + e.getReturnCode()); log(" Response: " + e.getMessage()); } } } } /** * We can assume that the recipients of this message are all going to the same * mail server. We will now rely on the DNS server to do DNS MX record lookup * and try to deliver to the multiple mail servers. If it fails, it should * throw an exception. * * Creation date: (2/24/00 11:25:00 PM) * @param mail org.apache.james.core.MailImpl * @param session javax.mail.Session * @return boolean Whether the delivery was successful and the message can be deleted */ private boolean deliver(Mail mail, Session session) { try { if (isDebug) { log("Attempting to deliver " + mail.getName()); } MimeMessage message = mail.getMessage(); //Create an array of the recipients as InternetAddress objects Collection recipients = mail.getRecipients(); InternetAddress addr[] = new InternetAddress[recipients.size()]; int j = 0; for (Iterator i = recipients.iterator(); i.hasNext(); j++) { MailAddress rcpt = (MailAddress)i.next(); addr[j] = rcpt.toInternetAddress(); } if (addr.length <= 0) { log("No recipients specified... not sure how this could have happened."); return true; } //Figure out which servers to try to send to. This collection // will hold all the possible target servers Iterator targetServers = null; if (gatewayServer == null) { MailAddress rcpt = (MailAddress) recipients.iterator().next(); String host = rcpt.getHost(); //Lookup the possible targets targetServers = getMailetContext().getSMTPHostAddresses(host); if (!targetServers.hasNext()) { log("No mail server found for: " + host); StringBuffer exceptionBuffer = new StringBuffer(128) .append("There are no DNS entries for the hostname ") .append(host) .append(". I cannot determine where to send this message."); return failMessage(mail, new MessagingException(exceptionBuffer.toString()), false); } } else { targetServers = getGatewaySMTPHostAddresses(gatewayServer); } MessagingException lastError = null; while ( targetServers.hasNext()) { try { HostAddress outgoingMailServer = (HostAddress) targetServers.next(); StringBuffer logMessageBuffer = new StringBuffer(256) .append("Attempting delivery of ") .append(mail.getName()) .append(" to host ") .append(outgoingMailServer.getHostName()) .append(" at ") .append(outgoingMailServer.getHost()) .append(" for addresses ") .append(Arrays.asList(addr)); log(logMessageBuffer.toString()); Properties props = session.getProperties(); if (mail.getSender() == null) { props.put("mail.smtp.from", "<>"); } else { String sender = mail.getSender().toString(); props.put("mail.smtp.from", sender); } //Many of these properties are only in later JavaMail versions //"mail.smtp.ehlo" //default true //"mail.smtp.auth" //default false //"mail.smtp.dsn.ret" //default to nothing... appended as RET= after MAIL FROM line. //"mail.smtp.dsn.notify" //default to nothing...appended as NOTIFY= after RCPT TO line. Transport transport = null; try { transport = session.getTransport(outgoingMailServer); try { if (authUser != null) { transport.connect(outgoingMailServer.getHostName(), authUser, authPass); } else { transport.connect(); } } catch (MessagingException me) { // Any error on connect should cause the mailet to attempt // to connect to the next SMTP server associated with this // MX record. Just log the exception. We'll worry about // failing the message at the end of the loop. log(me.getMessage()); continue; } // if the transport is a SMTPTransport (from sun) some // performance enhancement can be done. if (transport instanceof SMTPTransport) { SMTPTransport smtpTransport = (SMTPTransport) transport; // if the message is alredy 8bit or binary and the // server doesn't support the 8bit extension it has // to be converted to 7bit. Javamail api doesn't perform // that conversion, but it is required to be a // rfc-compliant smtp server. // Temporarily disabled. See JAMES-638 /* if (!smtpTransport.supportsExtension("8BITMIME")) { try { convertTo7Bit(message); } catch (IOException e) { // An error has occured during the 7bit conversion. // The error is logged and the message is sent anyway. log("Error during the conversion to 7 bit.", e); } } */ /* * Workaround for a javamail 1.3.2 bug: if * a message is sent without encoding information * and the 8bit allow property is set an exception * is trown during the mail delivery. */ try { setEncodingIfMissing(message); } catch (IOException e) { log("Error while adding encoding information to the message", e); } } else { // If the transport is not the one // developed by Sun we are not sure of how it // handles the 8 bit mime stuff, // so I convert the message to 7bit. try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -