james.java

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

JAVA
978
字号
        return attributes.get( key );    }    public void setAttribute( String key, Object object )    {        attributes.put( key, object );    }    public void removeAttribute( String key )    {        attributes.remove( key );    }    public Iterator getAttributeNames()    {        Vector names = new Vector();        for ( Enumeration e = attributes.keys(); e.hasMoreElements(); ) {            names.add( e.nextElement() );        }        return names.iterator();    }    /**     * This generates a response to the Return-Path address, or the address of     * the message's sender if the Return-Path is not available.  Note that     * this is different than a mail-client's reply, which would use the     * Reply-To or From header. This will send the bounce with the server's     * postmaster as the sender.     */    public void bounce( Mail mail, String message ) throws MessagingException    {        bounce( mail, message, getPostmaster() );    }    /**     * This generates a response to the Return-Path address, or the address of     * the message's sender if the Return-Path is not available.  Note that     * this is different than a mail-client's reply, which would use the     * Reply-To or From header.     */    public void bounce( Mail mail, String message, MailAddress bouncer ) throws MessagingException    {        MimeMessage orig = mail.getMessage();        //Create the reply message        MimeMessage reply = ( MimeMessage ) orig.reply( false );        //If there is a Return-Path header,        if ( orig.getHeader( RFC2822Headers.RETURN_PATH ) != null ) {            //Return the message to that address, not to the Reply-To address            reply.setRecipient( MimeMessage.RecipientType.TO, new InternetAddress( orig.getHeader( RFC2822Headers.RETURN_PATH )[0] ) );        }        //Create the list of recipients in our MailAddress format        Collection recipients = new HashSet();        Address addresses[] = reply.getAllRecipients();        for ( int i = 0; i < addresses.length; i++ ) {            recipients.add( new MailAddress( ( InternetAddress ) addresses[i] ) );        }        //Change the sender...        reply.setFrom( bouncer.toInternetAddress() );        try {            //Create the message body            MimeMultipart multipart = new MimeMultipart();            //Add message as the first mime body part            MimeBodyPart part = new MimeBodyPart();            part.setContent( message, "text/plain" );            part.setHeader( RFC2822Headers.CONTENT_TYPE, "text/plain" );            multipart.addBodyPart( part );            //Add the original message as the second mime body part            part = new MimeBodyPart();            part.setContent( orig.getContent(), orig.getContentType() );            part.setHeader( RFC2822Headers.CONTENT_TYPE, orig.getContentType() );            multipart.addBodyPart( part );            reply.setHeader( RFC2822Headers.DATE, rfc822DateFormat.format( new Date() ) );            reply.setContent( multipart );            reply.setHeader( RFC2822Headers.CONTENT_TYPE, multipart.getContentType() );        }        catch ( IOException ioe ) {            throw new MessagingException( "Unable to create multipart body", ioe );        }        //Send it off...        sendMail( bouncer, recipients, reply );    }    /**     * Returns whether that account has a local inbox on this server     *     * @param name the name to be checked     *     * @return whether the account has a local inbox     */    public boolean isLocalUser( String name )    {        if ( ignoreCase ) {            return localusers.containsCaseInsensitive( name );        }        else {            return localusers.contains( name );        }    }    /**     * Returns the address of the postmaster for this server.     *     * @return the <code>MailAddress</code> for the postmaster     */    public MailAddress getPostmaster()    {        return postmaster;    }    public void storeMail( MailAddress sender, MailAddress recipient, MimeMessage message )            throws MessagingException    {        String username;        if ( recipient == null ) {            throw new IllegalArgumentException( "Recipient for mail to be spooled cannot be null." );        }        if ( message == null ) {            throw new IllegalArgumentException( "Mail message to be spooled cannot be null." );        }        if ( ignoreCase ) {            String originalUsername = recipient.getUser();            username = localusers.getRealName( originalUsername );            if ( username == null ) {                StringBuffer errorBuffer =                        new StringBuffer( 128 )                        .append( "The inbox for user " )                        .append( originalUsername )                        .append( " was not found on this server." );                throw new MessagingException( errorBuffer.toString() );            }        }        else {            username = recipient.getUser();        }        JamesUser user;        if ( enableAliases || enableForwarding ) {            user = ( JamesUser ) localusers.getUserByName( username );            if ( enableAliases && user.getAliasing() ) {                username = user.getAlias();            }            // Forwarding takes precedence over local aliases            if ( enableForwarding && user.getForwarding() ) {                MailAddress forwardTo = user.getForwardingDestination();                if ( forwardTo == null ) {                    StringBuffer errorBuffer =                            new StringBuffer( 128 )                            .append( "Forwarding was enabled for " )                            .append( username )                            .append( " but no forwarding address was set for this account." );                    throw new MessagingException( errorBuffer.toString() );                }                Collection recipients = new HashSet();                recipients.add( forwardTo );                try {                    sendMail( sender, recipients, message );                    if ( getLogger().isInfoEnabled() ) {                        StringBuffer logBuffer =                                new StringBuffer( 128 )                                .append( "Mail for " )                                .append( username )                                .append( " forwarded to " )                                .append( forwardTo.toString() );                        getLogger().info( logBuffer.toString() );                    }                    return;                }                catch ( MessagingException me ) {                    if ( getLogger().isErrorEnabled() ) {                        StringBuffer logBuffer =                                new StringBuffer( 128 )                                .append( "Error forwarding mail to " )                                .append( forwardTo.toString() )                                .append( "attempting local delivery" );                        getLogger().error( logBuffer.toString() );                    }                    throw me;                }            }        }        if ( useIMAPstorage ) {            ImapMailbox mbox = null;            try {                user = ( JamesUser ) localusers.getUserByName( username );                mbox = imapHost.getInbox( user );                MailImpl mail = new MailImpl( message );                mbox.store( mail );                getLogger().info( "Message " + message.getMessageID() +                                  " stored in " +                                  mbox.getFullName() );                mbox = null;            }            catch ( Exception e ) {                getLogger().error( "Exception storing mail: " + e );                e.printStackTrace();                if ( mbox != null ) {                    mbox = null;                }                throw new RuntimeException( "Exception storing mail: " + e );            }        }        else {            Collection recipients = new HashSet();            recipients.add( recipient );            MailImpl mailImpl = new MailImpl( getId(), sender, recipients, message );            MailRepository userInbox = getUserInbox( username );            if ( userInbox == null ) {                StringBuffer errorBuffer =                        new StringBuffer( 128 )                        .append( "The inbox for user " )                        .append( username )                        .append( " was not found on this server." );                throw new MessagingException( errorBuffer.toString() );            }            userInbox.store( mailImpl );        }    }    /**     * Return the major version number for the server     *     * @return the major vesion number for the server     */    public int getMajorVersion()    {        return 2;    }    /**     * Return the minor version number for the server     *     * @return the minor vesion number for the server     */    public int getMinorVersion()    {        return 1;    }    /**     * Check whether the mail domain in question is to be     * handled by this server.     *     * @param serverName the name of the server to check     * @return whether the server is local     */    public boolean isLocalServer( final String serverName )    {        return serverNames.contains( serverName.toLowerCase( Locale.US ) );    }    /**     * Return the type of the server     *     * @return the type of the server     */    public String getServerInfo()    {        return "Apache Jakarta JAMES";    }    /**     * Return the logger for the Mailet API     *     * @return the logger for the Mailet API     */    private Logger getMailetLogger()    {        if ( mailetLogger == null ) {            mailetLogger = getLogger().getChildLogger( "Mailet" );        }        return mailetLogger;    }    /**     * Log a message to the Mailet logger     *     * @param message the message to pass to the Mailet logger     */    public void log( String message )    {        getMailetLogger().info( message );    }    /**     * Log a message and a Throwable to the Mailet logger     *     * @param message the message to pass to the Mailet logger     * @param t the <code>Throwable</code> to be logged     */    public void log( String message, Throwable t )    {        getMailetLogger().info( message, t );    }    /**     * Adds a user to this mail server. Currently just adds user to a     * UsersRepository.     * <p> As we move to IMAP support this will also create mailboxes and     * access control lists.     *     * @param userName String representing user name, that is the portion of     * an email address before the '@<domain>'.     * @param password String plaintext password     * @return boolean true if user added succesfully, else false.     */    public boolean addUser( String userName, String password )    {        boolean success;        DefaultJamesUser user = new DefaultJamesUser( userName, "SHA" );        user.setPassword( password );        user.initialize();        success = localusers.addUser( user );        if ( useIMAPstorage && success ) {            try {                imapHost.createPrivateMailAccount( user );                getLogger().info( "New MailAccount created for" + userName );            }            catch ( MailboxException e ) {                return false;            }        }        return success;    }}

⌨️ 快捷键说明

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