Index: lams_build/lib/lams/lams.jar =================================================================== diff -u -r3cc676d0f0a9f3f4bd0f64a8b0c565239f3808e5 -rc9ca11f026fc11b2e6d41b2e60494ed5f95884a0 Binary files differ Index: lams_common/src/java/org/lamsfoundation/lams/events/AbstractDeliveryMethod.java =================================================================== diff -u -r5773f84ed608838de3521ecde87c52f3c72d478c -rc9ca11f026fc11b2e6d41b2e60494ed5f95884a0 --- lams_common/src/java/org/lamsfoundation/lams/events/AbstractDeliveryMethod.java (.../AbstractDeliveryMethod.java) (revision 5773f84ed608838de3521ecde87c52f3c72d478c) +++ lams_common/src/java/org/lamsfoundation/lams/events/AbstractDeliveryMethod.java (.../AbstractDeliveryMethod.java) (revision c9ca11f026fc11b2e6d41b2e60494ed5f95884a0) @@ -55,6 +55,9 @@ protected abstract String send(Integer fromUserId, Integer toUserId, String subject, String message, boolean isHtmlFormat) throws InvalidParameterException; + protected abstract String send(Integer fromUserId, Integer toUserId, String subject, String message, + boolean isHtmlFormat, String attachmentFilename) throws InvalidParameterException; + protected abstract Logger getLog(); protected abstract boolean lastOperationFailed(Subscription subscription); Index: lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodMail.java =================================================================== diff -u -r5773f84ed608838de3521ecde87c52f3c72d478c -rc9ca11f026fc11b2e6d41b2e60494ed5f95884a0 --- lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodMail.java (.../DeliveryMethodMail.java) (revision 5773f84ed608838de3521ecde87c52f3c72d478c) +++ lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodMail.java (.../DeliveryMethodMail.java) (revision c9ca11f026fc11b2e6d41b2e60494ed5f95884a0) @@ -43,6 +43,10 @@ @Override protected String send(Integer fromUserId, Integer toUserId, String subject, String message, boolean isHtmlFormat) throws InvalidParameterException { + return send(fromUserId, toUserId, subject, message, isHtmlFormat, null); + } + protected String send(Integer fromUserId, Integer toUserId, String subject, String message, boolean isHtmlFormat, String attachmentFilename) + throws InvalidParameterException { try { User toUser = (User) DeliveryMethodMail.userManagementService.findById(User.class, toUserId); if (toUser == null) { @@ -54,7 +58,7 @@ } if (fromUserId == null) { - Emailer.sendFromSupportEmail(subject, toEmail, message, isHtmlFormat); + Emailer.sendFromSupportEmail(subject, toEmail, message, isHtmlFormat, attachmentFilename); } else { User fromUser = (User) DeliveryMethodMail.userManagementService.findById(User.class, fromUserId); if (fromUser == null) { @@ -65,7 +69,7 @@ return "Source user's e-mail address is invalid."; } - Emailer.send(subject, toEmail, "", fromEmail, "", message, isHtmlFormat); + Emailer.send(subject, toEmail, "", fromEmail, "", message, isHtmlFormat, attachmentFilename); } return null; } catch (Exception e) { @@ -107,7 +111,7 @@ DeliveryMethodMail.log.warn( "Could not notify admin as his email is blank. The subject: " + subject + ". The message: " + body); } else { - Emailer.sendFromSupportEmail(subject, adminEmail, body, isHtmlFormat); + Emailer.sendFromSupportEmail(subject, adminEmail, body, isHtmlFormat, null); } } Index: lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodNotification.java =================================================================== diff -u -r5773f84ed608838de3521ecde87c52f3c72d478c -rc9ca11f026fc11b2e6d41b2e60494ed5f95884a0 --- lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodNotification.java (.../DeliveryMethodNotification.java) (revision 5773f84ed608838de3521ecde87c52f3c72d478c) +++ lams_common/src/java/org/lamsfoundation/lams/events/DeliveryMethodNotification.java (.../DeliveryMethodNotification.java) (revision c9ca11f026fc11b2e6d41b2e60494ed5f95884a0) @@ -35,6 +35,12 @@ } @Override + protected String send(Integer fromUserId, Integer toUserId, String subject, String message, boolean isHtmlFormat, String filename) + throws InvalidParameterException { + return DeliveryMethodNotification.LAST_OPERATION_SEEN; + } + + @Override protected boolean lastOperationFailed(Subscription subscription) { return (subscription.getLastOperationMessage() != null) && !DeliveryMethodNotification.LAST_OPERATION_SEEN.equals(subscription.getLastOperationMessage()); Index: lams_common/src/java/org/lamsfoundation/lams/util/Emailer.java =================================================================== diff -u -r5773f84ed608838de3521ecde87c52f3c72d478c -rc9ca11f026fc11b2e6d41b2e60494ed5f95884a0 --- lams_common/src/java/org/lamsfoundation/lams/util/Emailer.java (.../Emailer.java) (revision 5773f84ed608838de3521ecde87c52f3c72d478c) +++ lams_common/src/java/org/lamsfoundation/lams/util/Emailer.java (.../Emailer.java) (revision c9ca11f026fc11b2e6d41b2e60494ed5f95884a0) @@ -3,13 +3,19 @@ import java.io.UnsupportedEncodingException; import java.util.Properties; +import javax.activation.DataHandler; +import javax.activation.FileDataSource; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; +import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import javax.mail.internet.MimeUtility; import org.apache.commons.lang.StringUtils; @@ -33,8 +39,13 @@ */ public static void sendFromSupportEmail(String subject, String to, String body, boolean isHtmlFormat) throws AddressException, MessagingException, UnsupportedEncodingException { + sendFromSupportEmail(subject, to, body, isHtmlFormat, null); + } + + public static void sendFromSupportEmail(String subject, String to, String body, boolean isHtmlFormat, String attachmentFilename) + throws AddressException, MessagingException, UnsupportedEncodingException { String supportEmail = Configuration.get(ConfigurationKeys.LAMS_ADMIN_EMAIL); - Emailer.send(subject, to, "", supportEmail, "", body, isHtmlFormat); + Emailer.send(subject, to, "", supportEmail, "", body, isHtmlFormat, attachmentFilename); } /** @@ -83,16 +94,63 @@ */ public static void send(String subject, String to, String toPerson, String from, String fromPerson, String body, boolean isHtmlFormat) throws AddressException, MessagingException, UnsupportedEncodingException { + + send(subject, to, toPerson, from, fromPerson, body, isHtmlFormat, null); + } + + /** + * Send email to recipients + * + * @param subject + * the subject of the email + * @param to + * the email of the recipient + * @param toPerson + * receiver's name + * @param from + * the email to source the email from + * @param fromPerson + * sender's name + * @param body + * the body of the email + * @param isHtmlFormat + * whether the message is of HTML content-type or plain text + * @param file + * file to attach + */ + public static void send(String subject, String to, String toPerson, String from, String fromPerson, String body, + boolean isHtmlFormat, String filename) throws AddressException, MessagingException, UnsupportedEncodingException { + Session session = Emailer.getMailSession(); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from, fromPerson)); message.addRecipient(RecipientType.TO, new InternetAddress(to, toPerson)); message.setSubject(subject, "UTF-8"); - message.setText(body, "UTF-8"); - String contentType = (isHtmlFormat) ? "text/html;charset=UTF-8" : "text/plain;charset=UTF-8"; - message.addHeader("Content-Type", contentType); + if (filename == null) { + message.setText(body, "UTF-8"); + String contentType = (isHtmlFormat) ? "text/html;charset=UTF-8" : "text/plain;charset=UTF-8"; + message.addHeader("Content-Type", contentType); + } else { + MimeBodyPart msgPart = new MimeBodyPart(); + msgPart.setText(body, "UTF-8"); + String contentType = (isHtmlFormat) ? "text/html;charset=UTF-8" : "text/plain;charset=UTF-8"; + msgPart.addHeader("Content-Type", contentType); + + MimeBodyPart filePart = new MimeBodyPart(); + FileDataSource fds = new FileDataSource(filename); + filePart.setDataHandler(new DataHandler(fds)); + String encodedFilename = MimeUtility.encodeText(fds.getName()); + filePart.setFileName(encodedFilename); + + Multipart mp = new MimeMultipart(); + mp.addBodyPart(msgPart); + mp.addBodyPart(filePart); + message.setContent(mp); + + } + Transport.send(message); } } \ No newline at end of file