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
This commit is contained in:
Massimiliano Assante 2016-04-26 14:26:17 +00:00
parent 75905b622d
commit da570dc81c
1 changed files with 81 additions and 18 deletions

View File

@ -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<InternetAddress> 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<String> 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<InternetAddress>();
emailBody = new StringBuffer(body);
emailBody.append("<p>")
.append("<p><div style=\"color:#999999; font-size:10px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif; padding-top:15px;\">")
@ -55,36 +89,65 @@ public class EmailNotification {
.append("If you have received this communication in error, please notify the <sender> and destroy and delete any copies you may have received.")
.append("</div></p>");
}
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<emailrecipients.length; i++) {
Address address = new InternetAddress(emailrecipients[i]);
mimeMessage.addRecipient(Message.RecipientType.TO, address);
}
}
// EMAIL CC Recipients
for (InternetAddress email : emailRecipientsInCC) {
mimeMessage.addRecipient(Message.RecipientType.CC, email);
}
mimeMessage.setSubject(emailSubject);
mimeMessage.setContent(emailBody.toString(), "text/html; charset=UTF-8");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
Address[] addresses = mimeMessage.getAllRecipients();
try {
t.sendMessage(mimeMessage, addresses);
}
catch (com.sun.mail.smtp.SMTPSendFailedException ex) {
_log.error("Error while trying to send emails");
ex.printStackTrace();
}
t.close();
} catch (Exception e) {
e.printStackTrace();
_log.error("Failed to send the email message.", e);
try {
t.close();
} catch (MessagingException e1) {
e1.printStackTrace();
}
}
}