Index: messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/ui/PrivateMessageManagerImpl.java =================================================================== --- messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/ui/PrivateMessageManagerImpl.java (revision 83120) +++ messageforums-component-impl/src/java/org/sakaiproject/component/app/messageforums/ui/PrivateMessageManagerImpl.java (working copy) @@ -30,6 +30,9 @@ import java.util.Map; import java.util.Set; +import javax.mail.MessagingException; +import javax.mail.internet.InternetAddress; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.hibernate.Hibernate; @@ -1103,174 +1106,160 @@ public void sendPrivateMessage(PrivateMessage message, Set recipients, boolean asEmail) { - if (LOG.isDebugEnabled()) - { - LOG.debug("sendPrivateMessage(message: " + message + ", recipients: " - + recipients + ")"); - } + try + { + if (LOG.isDebugEnabled()) + { + LOG.debug("sendPrivateMessage(message: " + message + ", recipients: " + + recipients + ")"); + } - if (message == null || recipients == null) - { - throw new IllegalArgumentException("Null Argument"); - } + if (message == null || recipients == null) + { + throw new IllegalArgumentException("Null Argument"); + } - if (recipients.size() == 0 && !message.getDraft().booleanValue()) - { - /** for no just return out - throw new IllegalArgumentException("Empty recipient list"); - **/ - return; - } + if (recipients.size() == 0 && !message.getDraft().booleanValue()) + { + /** for no just return out + throw new IllegalArgumentException("Empty recipient list"); + **/ + return; + } - String currentUserAsString = getCurrentUser(); - User currentUser = UserDirectoryService.getCurrentUser(); - List recipientList = new UniqueArrayList(); + String currentUserAsString = getCurrentUser(); + User currentUser = UserDirectoryService.getCurrentUser(); + List recipientList = new UniqueArrayList(); - /** test for draft message */ - if (message.getDraft().booleanValue()) - { - PrivateMessageRecipientImpl receiver = new PrivateMessageRecipientImpl( - currentUserAsString, typeManager.getDraftPrivateMessageType(), - getContextId(), Boolean.TRUE); + /** test for draft message */ + if (message.getDraft().booleanValue()) + { + PrivateMessageRecipientImpl receiver = new PrivateMessageRecipientImpl( + currentUserAsString, typeManager.getDraftPrivateMessageType(), + getContextId(), Boolean.TRUE); - recipientList.add(receiver); - message.setRecipients(recipientList); - savePrivateMessage(message, false); - return; - } + recipientList.add(receiver); + message.setRecipients(recipientList); + savePrivateMessage(message, false); + return; + } - //build the message body - List additionalHeaders = new ArrayList(1); - additionalHeaders.add("Content-Type: text/html"); - + //build the message body + List additionalHeaders = new ArrayList(1); + additionalHeaders.add("Content-Type: text/html"); + - /** determines if default in sakai.properties is set, if not will make a reasonable default */ - String defaultEmail = "postmaster@" + ServerConfigurationService.getServerName(); - String systemEmail = ServerConfigurationService.getString("msgcntr.notification.from.address", defaultEmail); - - if (!ServerConfigurationService.getBoolean("msgcntr.notification.user.real.from", false)) { - systemEmail = ServerConfigurationService.getString("msgcntr.notification.from.address", defaultEmail); - } else { - if (currentUser.getEmail() != null) - systemEmail = currentUser.getEmail(); - else - systemEmail = ServerConfigurationService.getString("msgcntr.notification.from.address", defaultEmail); - } - - String bodyString = buildMessageBody(message); - - Area currentArea = null; - List privateForums = null; - Map pfMap = null; - - if (!asEmail) { - currentArea = getAreaByContextIdAndTypeId(typeManager.getPrivateMessageAreaType()); + /** determines if default in sakai.properties is set, if not will make a reasonable default */ + String defaultEmail = "postmaster@" + ServerConfigurationService.getServerName(); + String systemEmail = ServerConfigurationService.getString("msgcntr.notification.from.address", defaultEmail); + + if (!ServerConfigurationService.getBoolean("msgcntr.notification.user.real.from", false)) { + systemEmail = ServerConfigurationService.getString("msgcntr.notification.from.address", defaultEmail); + } else { + if (currentUser.getEmail() != null) + systemEmail = currentUser.getEmail(); + else + systemEmail = ServerConfigurationService.getString("msgcntr.notification.from.address", defaultEmail); + } + + String bodyString = buildMessageBody(message); + + Area currentArea = null; + List privateForums = null; + Map pfMap = null; - //this is fairly inneficient and should realy be a convenience method to lookup - // the users who want to forward their messages - privateForums = currentArea.getPrivateForums(); + currentArea = getAreaByContextIdAndTypeId(typeManager.getPrivateMessageAreaType()); - //create a map for efficient lookup for large sites - pfMap = new HashMap(); - for (int i = 0; i < privateForums.size(); i++) { - PrivateForum pf1 = (PrivateForum)privateForums.get(i); - pfMap.put(pf1.getOwner(), pf1); - } + //this is fairly inneficient and should realy be a convenience method to lookup + // the users who want to forward their messages + privateForums = currentArea.getPrivateForums(); - //this only needs to be done if the message is not being sent - for (Iterator i = recipients.iterator(); i.hasNext();) - { - User u = (User) i.next(); - String userId = u.getId(); + //create a map for efficient lookup for large sites + pfMap = new HashMap(); + for (int i = 0; i < privateForums.size(); i++) { + PrivateForum pf1 = (PrivateForum)privateForums.get(i); + pfMap.put(pf1.getOwner(), pf1); + } + boolean forwardingEnabled = false; + List fAddresses = new ArrayList(); + //this only needs to be done if the message is not being sent + for (Iterator i = recipients.iterator(); i.hasNext();) + { + User u = (User) i.next(); + String userId = u.getId(); - boolean forwardingEnabled = false; - String forwardAddress = null; - //as this is a hefty overhead only do this if we're not sending as email - - PrivateForum pf = null; - if (pfMap.containsKey(userId)) - pf = pfMap.get(userId); - if (pf != null && pf.getAutoForward().booleanValue()){ - forwardingEnabled = true; - forwardAddress = pf.getAutoForwardEmail(); - } - if( pf == null) - { - //only check for default settings if the pf is null - PrivateForum oldPf = forumManager.getPrivateForumByOwnerAreaNull(userId); - if (oldPf != null && oldPf.getAutoForward().booleanValue()) { - forwardAddress = oldPf.getAutoForwardEmail(); - forwardingEnabled = true; - } - } + //boolean forwardingEnabled = false; + //String forwardAddress = null; + + PrivateForum pf = null; + if (pfMap.containsKey(userId)) + pf = pfMap.get(userId); - /** determine if current user is equal to recipient */ - Boolean isRecipientCurrentUser = - (currentUserAsString.equals(userId) ? Boolean.TRUE : Boolean.FALSE); + if (pf != null && pf.getAutoForward().booleanValue() && pf.getAutoForwardEmail() != null){ + forwardingEnabled = true; + fAddresses.add(new InternetAddress(pf.getAutoForwardEmail())); + //forwardAddress = pf.getAutoForwardEmail(); + } + if( pf == null) + { + //only check for default settings if the pf is null + PrivateForum oldPf = forumManager.getPrivateForumByOwnerAreaNull(userId); + if (oldPf != null && oldPf.getAutoForward().booleanValue() && pf.getAutoForwardEmail() != null) { + //forwardAddress = oldPf.getAutoForwardEmail(); + forwardingEnabled = true; + fAddresses.add(new InternetAddress(oldPf.getAutoForwardEmail())); + } + } - if (forwardingEnabled){ - emailService.send(systemEmail, forwardAddress, message.getTitle(), - bodyString, u.getEmail(), null, additionalHeaders); + /** determine if current user is equal to recipient */ + Boolean isRecipientCurrentUser = + (currentUserAsString.equals(userId) ? Boolean.TRUE : Boolean.FALSE); + + PrivateMessageRecipientImpl receiver = new PrivateMessageRecipientImpl( + userId, typeManager.getReceivedPrivateMessageType(), getContextId(), + isRecipientCurrentUser); + recipientList.add(receiver); + } + + if (asEmail) + { + //send as 1 action to all recipients + //we need to add som headers + additionalHeaders.add("From: " + systemEmail); + additionalHeaders.add("Subject: " + message.getTitle()); + emailService.sendToUsers(recipients, additionalHeaders, bodyString); + } + + if(forwardingEnabled) + { + InternetAddress fAddressesArr[] = new InternetAddress[fAddresses.size()]; + fAddressesArr = fAddresses.toArray(fAddressesArr); + emailService.sendMail(new InternetAddress(systemEmail), fAddressesArr, message.getTitle(), + bodyString, fAddressesArr, null, additionalHeaders); + } + + /** add sender as a saved recipient */ + PrivateMessageRecipientImpl sender = new PrivateMessageRecipientImpl( + currentUserAsString, typeManager.getSentPrivateMessageType(), + getContextId(), Boolean.TRUE); - // use forwarded address if set + recipientList.add(sender); - PrivateMessageRecipientImpl receiver = new PrivateMessageRecipientImpl( - userId, typeManager.getReceivedPrivateMessageType(), getContextId(), - isRecipientCurrentUser); - recipientList.add(receiver); - } - else { - PrivateMessageRecipientImpl receiver = new PrivateMessageRecipientImpl( - userId, typeManager.getReceivedPrivateMessageType(), getContextId(), - isRecipientCurrentUser); - recipientList.add(receiver); - } - } + message.setRecipients(recipientList); - } else { - //send as 1 action to all recipients - //we need to add som headers - additionalHeaders.add("From: " + systemEmail); - additionalHeaders.add("Subject: " + message.getTitle()); - emailService.sendToUsers(recipients, additionalHeaders, bodyString); - - //After send message to the campus email, send message to Sakai Message recipient(s)'s receive folder. - for (Iterator i = recipients.iterator(); i.hasNext();) { - User u = (User) i.next(); - String userId = u.getId(); + savePrivateMessage(message, false); + } + catch (MessagingException e) + { + LOG.warn("PrivateMessageManagerImpl.sendPrivateMessage: exception: " + e.getMessage(), e); + } + } - /** determine if current user is equal to recipient */ - Boolean isRecipientCurrentUser = - (currentUserAsString.equals(userId) ? Boolean.TRUE : Boolean.FALSE); - - PrivateMessageRecipientImpl receiver = new PrivateMessageRecipientImpl( - userId, typeManager.getReceivedPrivateMessageType(), getContextId(), - isRecipientCurrentUser); - recipientList.add(receiver); - } - - } - - - - /** add sender as a saved recipient */ - PrivateMessageRecipientImpl sender = new PrivateMessageRecipientImpl( - currentUserAsString, typeManager.getSentPrivateMessageType(), - getContextId(), Boolean.TRUE); - - recipientList.add(sender); - - message.setRecipients(recipientList); - - savePrivateMessage(message, false); - } - - private String buildMessageBody(PrivateMessage message) { User currentUser = UserDirectoryService.getCurrentUser(); StringBuilder body = new StringBuilder(message.getBody()); Index: messageforums-component-impl/pom.xml =================================================================== --- messageforums-component-impl/pom.xml (revision 83120) +++ messageforums-component-impl/pom.xml (working copy) @@ -84,6 +84,12 @@ 1.3 + javax.mail + mail + 1.4.3 + provided + + org.sakaiproject.search search-api