From da570dc81ca840d34fdcae52603075de26eb4a6b Mon Sep 17 00:00:00 2001
From: Massimiliano Assante
Date: Tue, 26 Apr 2016 14:26:17 +0000
Subject: [PATCH] added request to email notification
git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@128316 82a268e6-3cf1-43bd-a215-b396298e98cf
---
.../portal/mailing/EmailNotification.java | 99 +++++++++++++++----
1 file changed, 81 insertions(+), 18 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 1f4828e..8aed030 100644
--- a/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java
+++ b/src/main/java/org/gcube/common/portal/mailing/EmailNotification.java
@@ -1,14 +1,19 @@
package org.gcube.common.portal.mailing;
+import java.util.ArrayList;
import java.util.Date;
+import java.util.List;
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;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
+import javax.servlet.http.HttpServletRequest;
import org.gcube.common.portal.PortalContext;
import org.slf4j.Logger;
@@ -21,15 +26,12 @@ import org.slf4j.LoggerFactory;
*/
public class EmailNotification {
private static Logger _log = LoggerFactory.getLogger(EmailNotification.class);
- /**
- * The sender of the email
- */
- private static final String EMAIL_SENDER = PortalContext.getConfiguration().getSenderEmail();
- private static final String PORTAL_NAME = PortalContext.getConfiguration().getGatewayName();
/**
* The recipients of the email
*/
private String emailrecipients[];
+
+ private List emailRecipientsInCC;
/**
* Email's subject
*/
@@ -39,14 +41,46 @@ public class EmailNotification {
*/
private StringBuffer emailBody;
/**
- * @param recipients
- * @param subject
- * @param body
+ *
*/
- public EmailNotification(String recipients[], String subject, String body) {
+ private HttpServletRequest request;
+
+ private final String MAIL_SERVICE_HOST = "localhost";
+ private String MAIL_SERVICE_PORT = "25";
+ /**
+ *
+ * @param recipient an email address
+ * @param subject the subject of your email
+ * @param body the body of your email
+ */
+ public EmailNotification(String recipient, String subject, String body, HttpServletRequest httpServletRequest) {
+ String[] emailRecipients = new String[1];
+ emailRecipients[0] = recipient;
+ init(httpServletRequest, emailRecipients, subject, body);
+ }
+ /**
+ * @param recipients an array of email addresses
+ * @param subject the subject of your email
+ * @param body the body of your email
+ */
+ 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
+ */
+ 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;
emailSubject = subject;
-
+ emailRecipientsInCC = new ArrayList();
emailBody = new StringBuffer(body);
emailBody.append("")
.append("
")
@@ -55,36 +89,65 @@ 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));
+ } catch (AddressException e) {
+ e.printStackTrace();
+ }
+ }
public void sendEmail() {
Properties props = System.getProperties();
- String mailServiceHost = "localhost";
- props.put("mail.smtp.host", mailServiceHost);
- String mailServicePort = "25";
- props.put("mail.smtp.port", mailServicePort);
+ props.put("mail.smtp.host", MAIL_SERVICE_HOST);
+ props.put("mail.smtp.port", MAIL_SERVICE_PORT);
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
Message mimeMessage = new MimeMessage(session);
-
+ Transport t = null;
try {
+ t = session.getTransport("smtp");;
// EMAIL SENDER
- Address from = new InternetAddress(EMAIL_SENDER, PORTAL_NAME);
+ String emailSender = PortalContext.getConfiguration().getSenderEmail(request);
+ String siteName = PortalContext.getConfiguration().getGatewayName(request);
+ Address from = new InternetAddress(emailSender, siteName);
mimeMessage.setHeader("Content-Type", "text/html; charset=UTF-8");
mimeMessage.setFrom(from);
+ t.connect();
+
// EMAIL RECIPIENTS
for (int i=0; i