← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 5199: Fix to EmailMessageSender class

 

------------------------------------------------------------
revno: 5199
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2011-11-22 19:29:45 +0100
message:
  Fix to EmailMessageSender class
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java	2011-10-25 10:29:21 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/message/EmailMessageSender.java	2011-11-22 18:29:45 +0000
@@ -101,30 +101,37 @@
             ( sender.getPhoneNumber() != null ? ( sender.getPhoneNumber() + LB ) : StringUtils.EMPTY ) );
         
         Map<User,Serializable> settings = userService.getUserSettings( KEY_MESSAGE_EMAIL_NOTIFICATION, false );
-        
-        for ( User user : users )
+
+        try
         {
-            boolean emailNotification = settings.get( user ) != null && (Boolean) settings.get( user ) == true;
-
-            if ( emailNotification && user.getEmail() != null && !user.getEmail().isEmpty() )
+            Email email = getEmail( hostName, username, password );
+            email.setSubject( SUBJECT_PREFX + subject );
+            email.setMsg( text );
+            
+            boolean hasRecipients = false;
+            
+            for ( User user : users )
             {
-                try
+                boolean emailNotification = settings.get( user ) != null && (Boolean) settings.get( user ) == true;
+    
+                if ( emailNotification && user.getEmail() != null && !user.getEmail().trim().isEmpty() )
                 {
-                    String toAddress = StringUtils.trimToNull( user.getEmail() );
-                    
-                    Email email = getEmail( hostName, username, password );
-                    email.setSubject( SUBJECT_PREFX + subject );
-                    email.setMsg( text );
-                    email.addTo( toAddress );
-                    email.send();
+                    email.addTo( user.getEmail() );
                     
                     log.debug( "Sent email to user: " + user + " with email address: " + user.getEmail() );
-                }
-                catch ( EmailException ex )
-                {
-                    log.warn( "Could not send email to user: " + user + " with email address: " + user.getEmail() + " for reason: " + ex.getMessage() );
-                }
-            }
+                    
+                    hasRecipients = true;
+                }
+            }
+
+            if ( hasRecipients )
+            {
+                email.send();
+            }
+        }
+        catch ( EmailException ex )
+        {
+            log.warn( "Could not send email for reason: " + ex.getMessage() );
         }
     }