james.java

来自「java 开发的邮件服务器平台。支持以下协议。 协议可以修改为自己的专门标识」· Java 代码 · 共 978 行 · 第 1/3 页

JAVA
978
字号
        }        context.put( Constants.SERVER_NAMES, this.serverNames );        attributes.put( Constants.SERVER_NAMES, this.serverNames );        // Get postmaster        String postMasterAddress = conf.getChild( "postmaster" ).getValue( "postmaster" );        // 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 );        enableAliases = userNamesConf.getAttributeAsBoolean( "enableAliases", false );        enableForwarding = userNamesConf.getAttributeAsBoolean( "enableForwarding", false );        //Get localusers        try {            localusers = ( UsersRepository ) usersStore.getRepository( "LocalUsers" );        }        catch ( Exception e ) {            getLogger().error( "Cannot open private UserRepository" );            throw e;        }        //}        compMgr.put( UsersRepository.ROLE, ( Component ) localusers );        getLogger().info( "Local users repository opened" );        try {            // Get storage config param            if ( conf.getChild( "storage" ).getValue().equals( "IMAP" ) ) {                useIMAPstorage = true;                getLogger().info( "Using IMAP Store-System" );            }        }        catch ( Exception e ) {            // No storage entry found in config file        }        // Get the LocalInbox repository        if ( useIMAPstorage ) {            try {                // We will need to use a no-args constructor for flexibility                imapHost = ( ImapHost ) compMgr.lookup( ImapHost.ROLE );            }            catch ( Exception e ) {                getLogger().error( "Exception in IMAP Storage init: " + e.getMessage() );                throw e;            }        }        else {            Configuration inboxConf = conf.getChild( "inboxRepository" );            Configuration inboxRepConf = inboxConf.getChild( "repository" );            try {                localInbox = ( MailRepository ) mailstore.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 );        spool = mailstore.getInboundSpool();        if ( getLogger().isDebugEnabled() ) {            getLogger().debug( "Got spool" );        }        // 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 );        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();        for ( int i = 0; i < addresses.length; i++ ) {            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 );        mail.setState( state );        sendMail( 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    {        MailImpl mailimpl = ( MailImpl ) mail;        try {            spool.store( mailimpl );        }        catch ( Exception e ) {            try {                spool.remove( mailimpl );            }            catch ( Exception ignored ) {            }            throw new MessagingException( "Exception spooling message: " + e.getMessage(), e );        }        if ( getLogger().isDebugEnabled() ) {            StringBuffer logBuffer =                    new StringBuffer( 64 )                    .append( "Mail " )                    .append( mailimpl.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 = ( MailRepository ) 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 ) mailstore.select( mboxConf );                mailboxes.put( userName, userInbox );            }            catch ( Exception e ) {                e.printStackTrace();                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( count++ );        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." );        System.out.println( "Please refer to the Readme file to know how to run James." );    }    //Methods for MailetContext    /**     * <p>Get the prioritized list of mail servers for a given host.</p>     *     * <p>TODO: This needs to be made a more specific ordered subtype of Collection.</p>     *     * @param host     */    public Collection getMailServers( String host )    {        DNSServer dnsServer = null;        try {            dnsServer = ( DNSServer ) compMgr.lookup( DNSServer.ROLE );        }        catch ( final ComponentException cme ) {            getLogger().error( "Fatal configuration error - DNS Servers lost!", cme );            throw new RuntimeException( "Fatal configuration error - DNS Servers lost!" );        }        return dnsServer.findMXRecords( host );    }    public Object getAttribute( String key )    {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?