~registry/dhis2-academy/group11

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java

  • Committer: Lars Helge Overland
  • Date: 2011-11-22 18:29:45 UTC
  • Revision ID: larshelge@gmail.com-20111122182945-8sgw3gg6zbuzelxx
Fix to EmailMessageSender class

Show diffs side-by-side

added added

removed removed

Lines of Context:
101
101
            ( sender.getPhoneNumber() != null ? ( sender.getPhoneNumber() + LB ) : StringUtils.EMPTY ) );
102
102
        
103
103
        Map<User,Serializable> settings = userService.getUserSettings( KEY_MESSAGE_EMAIL_NOTIFICATION, false );
104
 
        
105
 
        for ( User user : users )
 
104
 
 
105
        try
106
106
        {
107
 
            boolean emailNotification = settings.get( user ) != null && (Boolean) settings.get( user ) == true;
108
 
 
109
 
            if ( emailNotification && user.getEmail() != null && !user.getEmail().isEmpty() )
 
107
            Email email = getEmail( hostName, username, password );
 
108
            email.setSubject( SUBJECT_PREFX + subject );
 
109
            email.setMsg( text );
 
110
            
 
111
            boolean hasRecipients = false;
 
112
            
 
113
            for ( User user : users )
110
114
            {
111
 
                try
 
115
                boolean emailNotification = settings.get( user ) != null && (Boolean) settings.get( user ) == true;
 
116
    
 
117
                if ( emailNotification && user.getEmail() != null && !user.getEmail().trim().isEmpty() )
112
118
                {
113
 
                    String toAddress = StringUtils.trimToNull( user.getEmail() );
114
 
                    
115
 
                    Email email = getEmail( hostName, username, password );
116
 
                    email.setSubject( SUBJECT_PREFX + subject );
117
 
                    email.setMsg( text );
118
 
                    email.addTo( toAddress );
119
 
                    email.send();
 
119
                    email.addTo( user.getEmail() );
120
120
                    
121
121
                    log.debug( "Sent email to user: " + user + " with email address: " + user.getEmail() );
122
 
                }
123
 
                catch ( EmailException ex )
124
 
                {
125
 
                    log.warn( "Could not send email to user: " + user + " with email address: " + user.getEmail() + " for reason: " + ex.getMessage() );
126
 
                }
127
 
            }
 
122
                    
 
123
                    hasRecipients = true;
 
124
                }
 
125
            }
 
126
 
 
127
            if ( hasRecipients )
 
128
            {
 
129
                email.send();
 
130
            }
 
131
        }
 
132
        catch ( EmailException ex )
 
133
        {
 
134
            log.warn( "Could not send email for reason: " + ex.getMessage() );
128
135
        }
129
136
    }
130
137