Package com.ericdaugherty.mail.server.info

Examples of com.ericdaugherty.mail.server.info.EmailAddress


            // Initialize a new message with the right file location
            SMTPMessage message = new SMTPMessage();
            message.setMessageLocation( messageFile );

            // Load each variable
            message.setFromAddress( new EmailAddress( reader.readLine() ) );
            message.setToAddresses( inflateAddresses( reader.readLine() ) );
            message.setTimeReceived( new Date( Long.parseLong( reader.readLine() ) ) );
            message.setScheduledDelivery( new Date( Long.parseLong( reader.readLine() ) ) );
            message.setDeliveryAttempts( Integer.parseInt( reader.readLine() ) );
View Full Code Here


     * @return Comma delimited String of the addresses.
     */
    private static String flattenAddresses( Collection addresses )
    {
        StringBuffer toAddresses = new StringBuffer();
        EmailAddress address;
        Iterator addressIterator = addresses.iterator();
        while( addressIterator.hasNext() )
        {
            address = (EmailAddress) addressIterator.next();
            toAddresses.append( address.toString() );
            toAddresses.append( "," );
        }

        // Remove the last comma.
        toAddresses.deleteCharAt( toAddresses.length() - 1 );
View Full Code Here

     */
    private static List inflateAddresses( String addresses )
    {
        StringTokenizer addressTokenizer = new StringTokenizer( addresses, "," );
        List addressList = new ArrayList();
        EmailAddress address;

        try
        {
            while( addressTokenizer.hasMoreTokens() )
            {
                address = new EmailAddress( addressTokenizer.nextToken() );
                addressList.add( address );
            }

            return addressList;
        }
View Full Code Here

        String fromAddress = parseAddress( inputString.substring( 10 ) );

        try {
            //It is legal for the MAIL FROM address to be empty.
            if( fromAddress == null || fromAddress.trim().equals( "" ) ) {
                message.setFromAddress( new EmailAddress() );
                message.setFromAddress( new EmailAddress("unknown@example.com") );
                log.debug( "MAIL FROM is empty, using unknown@example.com" );
            }
            //Although this is the normal case...
            else {
                EmailAddress address = new EmailAddress( fromAddress );
                message.setFromAddress( address );
                if( log.isDebugEnabled() ) { log.debug( "MAIL FROM: " + fromAddress ); }
            }
            write( MESSAGE_OK );
            return true;
View Full Code Here

    private void handleRcptTo( String inputString ) {

        String toAddress = parseAddress( inputString.substring( 8 ) );

        try {
            EmailAddress address = new EmailAddress( toAddress );
            //Check the address to see if we can deliver it.
            DeliveryService deliveryService = DeliveryService.getDeliveryService();
            if( deliveryService.acceptAddress( address, clientIp, message.getFromAddress() ) ) {
                // Check to see if it is a local user.  If so, ask to
                // user object for the delivery addresses.
                User localUser = configurationManager.getUser( address );
                if( localUser!= null ) {
                    EmailAddress[] addresses = localUser.getDeliveryAddresses();
                    for( int index = 0; index < addresses.length; index++ ) {
                        message.addToAddress( addresses[index] );
                    }
                }
                // Otherwise, just add the address.
                else {
                    message.addToAddress( address );
                }
                write( MESSAGE_OK );
                if( log.isDebugEnabled() ) { log.debug( "RCTP TO: " + address.getAddress() + " accepted." ); }
            }
            else {
                if( log.isInfoEnabled() ) log.info( "Invalid delivery address for incoming mail: " + toAddress + " from client: " + clientIp + " / " + message.getFromAddress() );
                throw new InvalidAddressException();
            }
View Full Code Here

    private void deliver( SMTPMessage message ) {

        List toAddresses = message.getToAddresses();
        int numAddress = toAddresses.size();
        Vector failedAddress = new Vector();
        EmailAddress address = null;

        // If the next scheduled delivery attempt is still in the future, skip.
        if( message.getScheduledDelivery().getTime() > System.currentTimeMillis() )
        {
            if( log.isDebugEnabled() ) log.debug( "Skipping delivery of message " + message.getMessageLocation().getName() + " because the scheduled delivery time is still in the future: " + message.getScheduledDelivery() );
            return;
        }

        for( int index = 0; index < numAddress; index++ ) {
            try {
                address = (EmailAddress) toAddresses.get( index );
                if( log.isDebugEnabled()) { log.debug( "Attempting to deliver message from: " + message.getFromAddress().getAddress() + " to: " + address ); }

                DeliveryService deliveryService = DeliveryService.getDeliveryService();

                try {
                    if( deliveryService.isLocalAddress( address ) ) {
                        deliverLocalMessage( address, message );
                    }
                    else {
                        deliverRemoteMessage( address, message );
                    }
                }
                catch (NotFoundException e) {
                    log.info( "Delivery attempted to unknown user: " + address.getAddress() );
                    //The addressee does not exist.  Notify the sender of the error.
                    bounceMessage( address, message );
                }

                if( log.isInfoEnabled() ) { log.info( "Delivery complete for message " + message.getMessageLocation().getName() + " to: " + address ); }
            }
            catch( Throwable throwable ) {
                log.error( "Delivery failed for message from: " + message.getFromAddress().getAddress() + " to: " + address + " - " + throwable, throwable );
                failedAddress.addElement( toAddresses.get( index ) );
            }
        }

        // If all addresses were successful, remove the message from the spool
        if( failedAddress.size() == 0 ) {
            // Log an error if the delete fails.  This will cause the message to get
            // delivered again, but it is too late to roll back the delivery.
            if( !message.getMessageLocation().delete() )
            {
                log.error( "Error removed SMTP message after delivery!  This message may be redelivered. " + message.getMessageLocation().getName() );
            }
        }
        // Update the message with any changes.
        else {

            message.setToAddresses( failedAddress );
            int deliveryAttempts = message.getDeliveryAttempts();


            // If the message is a bounced email, just give up and move it to the failed directory.
            if(message.getFromAddress().getUsername().equalsIgnoreCase("MAILER_DAEMON"))
            {
                try {
                    log.info( "Delivery of message from MAILER_DAEMON failed, moving to failed folder." );
                    message.moveToFailedFolder();
                }
                catch (Exception e) {
                    log.error( "Unable to move failed message to 'failed' folder." );
                }
            }
            // If we have not passed the maximum delivery count, calculate the
            // next delivery time and save the message.
            else ifdeliveryAttempts < configurationManager.getDeliveryAttemptThreshold() )
            {
                message.setDeliveryAttempts( deliveryAttempts + 1 );

                // Reschedule later, 1 min, 2 min, 4 min, 8 min, ... 2^n
                // Cap delivery interval at 2^10 minutes. (about 17 hours)
                if( deliveryAttempts > 10 )
                {
                    deliveryAttempts = 10;
                }
                long offset = (long)Math.pow( 2, deliveryAttempts);
                Date schedTime = new Date(System.currentTimeMillis() + offset*60*1000);
                message.setScheduledDelivery( schedTime );

                try {
                    message.save();
                }
                catch( Exception exception ) {
                    log.error( "Error updating spooled message for next delivery.  Message may be re-delivered.", exception );
                }
            }
            // All delivery attempts failed, bounce message.
            else
            {
                // Send a bounce message to all failed addresses.
                for( int index = 0; index < failedAddress.size(); index++ ) {
                    try {
                        EmailAddress bounce_address = (EmailAddress)( failedAddress.elementAt(index) );
                        bounceMessage(bounce_address, message);
                    }
                    catch(Exception e) {
                        log.error( "Problem bouncing message. " + message.getMessageLocation().getName() );
                    }
View Full Code Here

        {
            log.debug( "User not found, checking for default delivery options" );
            //Check to see if a default delivery mailbox exists, and if so, deliver it.
            //Otherwise, just throw the NotFoundException to bounce the email.
            if( configurationManager.isDefaultUserEnabled() ) {
                EmailAddress defaultAddress = configurationManager.getDefaultUser();
                //If this throws a NotFoundException, go ahead and let it bounce.
                user = configurationManager.getUser( defaultAddress );
                if( user == null ) throw new NotFoundException();
                if( log.isDebugEnabled() ) { log.info( "Delivering message addressed to: " + address + " to default user: " + defaultAddress ); }
            }
View Full Code Here

        if( log.isInfoEnabled() ) { log.info( "Bouncing Messsage from " + message.getFromAddress().getAddress() + " to " + address.getAddress() ); }

        SMTPMessage bounceMessage = new SMTPMessage();

        //Set the from address as mailserver@ the first (default) local domain.
        EmailAddress fromAddress = new EmailAddress( "MAILER_DAEMON", configurationManager.getLocalDomains()[0] );

        bounceMessage.setFromAddress( fromAddress );
        bounceMessage.addToAddress( message.getFromAddress() );
        bounceMessage.addDataLine( "From: Mail Delivery Subsystem <MAILER-DAEMON@" + configurationManager.getLocalDomains()[0] + ">" );
        bounceMessage.addDataLine( "To: " + message.getFromAddress().getAddress() );
View Full Code Here

        // Load default user info
        String defaultUserString = properties.getProperty( DEFAULT_USER, "" ).trim();;
        if( defaultUserString.length() > 0 )
        {
            try {
                defaultUser = new EmailAddress( defaultUserString );
                defaultUserEnabled = true;
            }
            catch (InvalidAddressException e) {
                throw new RuntimeException( "Invalid address for default user: " + defaultUserString );
            }
View Full Code Here

     * @param properties the properties that contain the user parameters.
     * @return a new User instance.
     */
    private User loadUser( String fullAddress, Properties properties ) throws InvalidAddressException
    {
        EmailAddress address = new EmailAddress( fullAddress );
        User user = new User( address );

        // Load the password
        String password = properties.getProperty( USER_DEF_PREFIX + fullAddress );
        // If the password is not hashed, hash it now.
        if( password.length() != 60 ) {
            password = PasswordManager.encryptPassword( password );
            properties.setProperty( USER_DEF_PREFIX + fullAddress, password );
            if( password == null ) {
                log.error( "Error encrypting plaintext password from user.conf for user " + fullAddress );
                throw new RuntimeException( "Error encrypting password for user: " + fullAddress );
            }
            userConfModified = true;
        }
        user.setPassword( password );

        // Load the 'forward' addresses.
        String forwardAddressesString = properties.getProperty( USER_PROPERTY_PREFIX + fullAddress + USER_FILE_FORWARDS );
        String[] forwardAddresses = new String[0];
        if( forwardAddressesString != null && forwardAddressesString.trim().length() >= 0 )
        {
            forwardAddresses = tokenize( forwardAddressesString );
        }
        ArrayList addressList = new ArrayList( forwardAddresses.length );
        for( int index = 0; index < forwardAddresses.length; index++ ) {
            try {
                addressList.add( new EmailAddress( forwardAddresses[index] ) );
            }
            catch (InvalidAddressException e) {
                log.warn( "Forward address: " + forwardAddresses[index] + " for user " + user.getFullUsername() + " is invalid and will be ignored." );
            }
        }
View Full Code Here

TOP

Related Classes of com.ericdaugherty.mail.server.info.EmailAddress

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.