📄 james.java
字号:
throw new ConfigurationException( "Fatal configuration error: no servernames specified!"); } if (getLogger().isInfoEnabled()) { for (Iterator i = serverNames.iterator(); i.hasNext(); ) { getLogger().info("Handling mail for: " + i.next()); } } String defaultDomain = (String) serverNames.iterator().next(); context.put(Constants.DEFAULT_DOMAIN, defaultDomain); attributes.put(Constants.DEFAULT_DOMAIN, defaultDomain); // Get postmaster String postMasterAddress = conf.getChild("postmaster").getValue("postmaster").toLowerCase(Locale.US); // if there is no @domain part, then add the first one from the // list of supported domains that isn't localhost. If that // doesn't work, use the hostname, even if it is localhost. if (postMasterAddress.indexOf('@') < 0) { String domainName = null; // the domain to use // loop through candidate domains until we find one or exhaust the list for ( int i = 0; domainName == null && i < serverNameConfs.length ; i++ ) { String serverName = serverNameConfs[i].getValue().toLowerCase(Locale.US); if (!("localhost".equals(serverName))) { domainName = serverName; // ok, not localhost, so use it } } // if we found a suitable domain, use it. Otherwise fallback to the host name. postMasterAddress = postMasterAddress + "@" + (domainName != null ? domainName : hostName); } this.postmaster = new MailAddress( postMasterAddress ); context.put( Constants.POSTMASTER, postmaster ); if (!isLocalServer(postmaster.getHost())) { StringBuffer warnBuffer = new StringBuffer(320) .append("The specified postmaster address ( ") .append(postmaster) .append(" ) is not a local address. This is not necessarily a problem, but it does mean that emails addressed to the postmaster will be routed to another server. For some configurations this may cause problems."); getLogger().warn(warnBuffer.toString()); } Configuration userNamesConf = conf.getChild("usernames"); ignoreCase = userNamesConf.getAttributeAsBoolean("ignoreCase", false); boolean enableAliases = userNamesConf.getAttributeAsBoolean("enableAliases", false); boolean enableForwarding = userNamesConf.getAttributeAsBoolean("enableForwarding", false); attributes.put(Constants.DEFAULT_ENABLE_ALIASES,new Boolean(enableAliases)); attributes.put(Constants.DEFAULT_ENABLE_FORWARDING,new Boolean(enableForwarding)); attributes.put(Constants.DEFAULT_IGNORE_USERNAME_CASE,new Boolean(ignoreCase)); //Get localusers try { localusers = (UsersRepository) compMgr.lookup(UsersRepository.ROLE); } catch (Exception e) { getLogger().error("Cannot open private UserRepository"); throw e; } //} compMgr.put( UsersRepository.ROLE, localusers); getLogger().info("Local users repository opened"); Configuration inboxConf = conf.getChild("inboxRepository"); Configuration inboxRepConf = inboxConf.getChild("repository"); // we could delete this block. I didn't remove this because I'm not sure // wether we need the "check" of the inbox repository here, or not. try { store.select(inboxRepConf); } catch (Exception e) { getLogger().error("Cannot open private MailRepository"); throw e; } inboxRootURL = inboxRepConf.getAttribute("destinationURL"); getLogger().info("Private Repository LocalInbox opened"); // Add this to comp compMgr.put( MailServer.ROLE, this); // For mailet engine provide MailetContext //compMgr.put("org.apache.mailet.MailetContext", this); // For AVALON aware mailets and matchers, we put the Component object as // an attribute attributes.put(Constants.AVALON_COMPONENT_MANAGER, compMgr); //Temporary get out to allow complex mailet config files to stop blocking sergei sozonoff's work on bouce processing java.io.File configDir = AvalonContextUtilities.getFile(myContext, "file://conf/"); attributes.put("confDir", configDir.getCanonicalPath()); // We can safely remove this and the localDeliveryField when we // remove the storeMail method from James and from the MailetContext DefaultConfiguration conf = new DefaultConfiguration("mailet", "generated:James.initialize()"); MailetConfigImpl configImpl = new MailetConfigImpl(); configImpl.setMailetName("LocalDelivery"); configImpl.setConfiguration(conf); configImpl.setMailetContext(this); localDeliveryMailet = new LocalDelivery(); localDeliveryMailet.init(configImpl); System.out.println(SOFTWARE_NAME_VERSION); getLogger().info("JAMES ...init end"); } /** * Place a mail on the spool for processing * * @param message the message to send * * @throws MessagingException if an exception is caught while placing the mail * on the spool */ public void sendMail(MimeMessage message) throws MessagingException { MailAddress sender = new MailAddress((InternetAddress)message.getFrom()[0]); Collection recipients = new HashSet(); Address addresses[] = message.getAllRecipients(); if (addresses != null) { for (int i = 0; i < addresses.length; i++) { // Javamail treats the "newsgroups:" header field as a // recipient, so we want to filter those out. if ( addresses[i] instanceof InternetAddress ) { recipients.add(new MailAddress((InternetAddress)addresses[i])); } } } sendMail(sender, recipients, message); } /** * Place a mail on the spool for processing * * @param sender the sender of the mail * @param recipients the recipients of the mail * @param message the message to send * * @throws MessagingException if an exception is caught while placing the mail * on the spool */ public void sendMail(MailAddress sender, Collection recipients, MimeMessage message) throws MessagingException { sendMail(sender, recipients, message, Mail.DEFAULT); } /** * Place a mail on the spool for processing * * @param sender the sender of the mail * @param recipients the recipients of the mail * @param message the message to send * @param state the state of the message * * @throws MessagingException if an exception is caught while placing the mail * on the spool */ public void sendMail(MailAddress sender, Collection recipients, MimeMessage message, String state) throws MessagingException { MailImpl mail = new MailImpl(getId(), sender, recipients, message); try { mail.setState(state); sendMail(mail); } finally { ContainerUtil.dispose(mail); } } /** * Place a mail on the spool for processing * * @param sender the sender of the mail * @param recipients the recipients of the mail * @param msg an <code>InputStream</code> containing the message * * @throws MessagingException if an exception is caught while placing the mail * on the spool */ public void sendMail(MailAddress sender, Collection recipients, InputStream msg) throws MessagingException { // parse headers MailHeaders headers = new MailHeaders(msg); // if headers do not contains minimum REQUIRED headers fields throw Exception if (!headers.isValid()) { throw new MessagingException("Some REQURED header field is missing. Invalid Message"); } ByteArrayInputStream headersIn = new ByteArrayInputStream(headers.toByteArray()); sendMail(new MailImpl(getId(), sender, recipients, new SequenceInputStream(headersIn, msg))); } /** * Place a mail on the spool for processing * * @param mail the mail to place on the spool * * @throws MessagingException if an exception is caught while placing the mail * on the spool */ public void sendMail(Mail mail) throws MessagingException { try { spool.store(mail); } catch (Exception e) { getLogger().error("Error storing message: " + e.getMessage(),e); try { spool.remove(mail); } catch (Exception ignored) { getLogger().error("Error removing message after an error storing it: " + e.getMessage(),e); } throw new MessagingException("Exception spooling message: " + e.getMessage(), e); } if (getLogger().isDebugEnabled()) { StringBuffer logBuffer = new StringBuffer(64) .append("Mail ") .append(mail.getName()) .append(" pushed in spool"); getLogger().debug(logBuffer.toString()); } } /** * <p>Retrieve the mail repository for a user</p> * * <p>For POP3 server only - at the moment.</p> * * @param userName the name of the user whose inbox is to be retrieved * * @return the POP3 inbox for the user */ public synchronized MailRepository getUserInbox(String userName) { MailRepository userInbox = null; userInbox = (MailRepository) mailboxes.get(userName); if (userInbox != null) { return userInbox; } else if (mailboxes.containsKey(userName)) { // we have a problem getLogger().error("Null mailbox for non-null key"); throw new RuntimeException("Error in getUserInbox."); } else { // need mailbox object if (getLogger().isDebugEnabled()) { getLogger().debug("Retrieving and caching inbox for " + userName ); } StringBuffer destinationBuffer = new StringBuffer(192) .append(inboxRootURL) .append(userName) .append("/"); String destination = destinationBuffer.toString(); DefaultConfiguration mboxConf = new DefaultConfiguration("repository", "generated:AvalonFileRepository.compose()"); mboxConf.setAttribute("destinationURL", destination); mboxConf.setAttribute("type", "MAIL"); try { userInbox = (MailRepository) store.select(mboxConf); if (userInbox!=null) { mailboxes.put(userName, userInbox); } } catch (Exception e) { if (getLogger().isErrorEnabled()) { getLogger().error("Cannot open user Mailbox" + e); } throw new RuntimeException("Error in getUserInbox." + e); } return userInbox; } } /** * Return a new mail id. * * @return a new mail id */ public String getId() { long localCount = -1; synchronized (James.class) { localCount = count++; } StringBuffer idBuffer = new StringBuffer(64) .append("Mail") .append(System.currentTimeMillis()) .append("-") .append(localCount); return idBuffer.toString(); } /** * The main method. Should never be invoked, as James must be called * from within an Avalon framework container. * * @param args the command line arguments */ public static void main(String[] args) { System.out.println("ERROR!"); System.out.println("Cannot execute James as a stand alone application."); System.out.println("To run James, you need to have the Avalon framework installed.");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -