From ae24cd0d7d69167161e84d9255d866248577d5e0 Mon Sep 17 00:00:00 2001
From: Massimiliano Assante
Date: Tue, 5 Jul 2016 12:10:31 +0000
Subject: [PATCH] updated EmailNotification class to allow sending with default
email sender if no httpservler request is available
git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@129918 82a268e6-3cf1-43bd-a215-b396298e98cf
---
.../portal/mailing/EmailNotification.java | 33 ++++++++++++-------
1 file changed, 21 insertions(+), 12 deletions(-)
diff --git a/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java b/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java
index 7aa8031..fbd1bd3 100644
--- a/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java
+++ b/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java
@@ -7,7 +7,6 @@ import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
-import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
@@ -30,7 +29,7 @@ public class EmailNotification {
* The recipients of the email
*/
private String emailrecipients[];
-
+
private List emailRecipientsInCC;
/**
* Email's subject
@@ -44,7 +43,7 @@ public class EmailNotification {
*
*/
private HttpServletRequest request;
-
+
private final String MAIL_SERVICE_HOST = "localhost";
private String MAIL_SERVICE_PORT = "25";
/**
@@ -52,6 +51,7 @@ public class EmailNotification {
* @param recipient an email address
* @param subject the subject of your email
* @param body the body of your email
+ * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)
*/
public EmailNotification(String recipient, String subject, String body, HttpServletRequest httpServletRequest) {
String[] emailRecipients = new String[1];
@@ -62,20 +62,22 @@ public class EmailNotification {
* @param recipients an array of email addresses
* @param subject the subject of your email
* @param body the body of your email
+ * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)
*/
public EmailNotification(String recipients[], String subject, String body, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients, subject, body);
}
-
+
/**
* @param recipients a list of email addresses
* @param subject the subject of your email
* @param body the body of your email
+ * @param httpServletRequest the httpServletRequest object if you have it, null otherwise (but the default sender will be applied in this case)
*/
public EmailNotification(List recipients, String subject, String body, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients.toArray(new String[recipients.size()]), subject, body);
}
-
+
private void init(HttpServletRequest httpServletRequest, String recipients[], String subject, String body) {
request = httpServletRequest;
emailrecipients = recipients;
@@ -89,7 +91,7 @@ public class EmailNotification {
.append("If you have received this communication in error, please notify the and destroy and delete any copies you may have received.")
.append("
");
}
-
+
public void addRecipientInCC(String email) {
try {
emailRecipientsInCC.add(new InternetAddress(email));
@@ -106,13 +108,20 @@ public class EmailNotification {
session.setDebug(true);
Message mimeMessage = new MimeMessage(session);
try {
+ String emailSender = "";
+ String siteName = "";
// EMAIL SENDER
- String emailSender = PortalContext.getConfiguration().getSenderEmail(request);
- String siteName = PortalContext.getConfiguration().getGatewayName(request);
+ if (request != null) {
+ emailSender = PortalContext.getConfiguration().getSenderEmail(request);
+ siteName = PortalContext.getConfiguration().getGatewayName(request);
+ } else {
+ emailSender = PortalContext.getConfiguration().getSenderEmail();
+ siteName = PortalContext.getConfiguration().getGatewayName();
+ }
Address from = new InternetAddress(emailSender, siteName);
mimeMessage.setHeader("Content-Type", "text/html; charset=UTF-8");
mimeMessage.setFrom(from);
-
+
// EMAIL RECIPIENTS
for (int i=0; i