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:
parent
75905b622d
commit
da570dc81c
|
@ -1,14 +1,19 @@
|
||||||
package org.gcube.common.portal.mailing;
|
package org.gcube.common.portal.mailing;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.mail.Address;
|
import javax.mail.Address;
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
|
import javax.mail.MessagingException;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import javax.mail.Transport;
|
import javax.mail.Transport;
|
||||||
|
import javax.mail.internet.AddressException;
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
import org.gcube.common.portal.PortalContext;
|
import org.gcube.common.portal.PortalContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -21,15 +26,12 @@ import org.slf4j.LoggerFactory;
|
||||||
*/
|
*/
|
||||||
public class EmailNotification {
|
public class EmailNotification {
|
||||||
private static Logger _log = LoggerFactory.getLogger(EmailNotification.class);
|
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
|
* The recipients of the email
|
||||||
*/
|
*/
|
||||||
private String emailrecipients[];
|
private String emailrecipients[];
|
||||||
|
|
||||||
|
private List<InternetAddress> emailRecipientsInCC;
|
||||||
/**
|
/**
|
||||||
* Email's subject
|
* Email's subject
|
||||||
*/
|
*/
|
||||||
|
@ -39,14 +41,46 @@ public class EmailNotification {
|
||||||
*/
|
*/
|
||||||
private StringBuffer emailBody;
|
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;
|
emailrecipients = recipients;
|
||||||
emailSubject = subject;
|
emailSubject = subject;
|
||||||
|
emailRecipientsInCC = new ArrayList<InternetAddress>();
|
||||||
emailBody = new StringBuffer(body);
|
emailBody = new StringBuffer(body);
|
||||||
emailBody.append("<p>")
|
emailBody.append("<p>")
|
||||||
.append("<p><div style=\"color:#999999; font-size:10px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif; padding-top:15px;\">")
|
.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("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>");
|
.append("</div></p>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addRecipientInCC(String email) {
|
||||||
|
try {
|
||||||
|
emailRecipientsInCC.add(new InternetAddress(email));
|
||||||
|
} catch (AddressException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void sendEmail() {
|
public void sendEmail() {
|
||||||
Properties props = System.getProperties();
|
Properties props = System.getProperties();
|
||||||
String mailServiceHost = "localhost";
|
props.put("mail.smtp.host", MAIL_SERVICE_HOST);
|
||||||
props.put("mail.smtp.host", mailServiceHost);
|
props.put("mail.smtp.port", MAIL_SERVICE_PORT);
|
||||||
String mailServicePort = "25";
|
|
||||||
props.put("mail.smtp.port", mailServicePort);
|
|
||||||
Session session = Session.getDefaultInstance(props, null);
|
Session session = Session.getDefaultInstance(props, null);
|
||||||
session.setDebug(true);
|
session.setDebug(true);
|
||||||
Message mimeMessage = new MimeMessage(session);
|
Message mimeMessage = new MimeMessage(session);
|
||||||
|
Transport t = null;
|
||||||
try {
|
try {
|
||||||
|
t = session.getTransport("smtp");;
|
||||||
// EMAIL SENDER
|
// 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.setHeader("Content-Type", "text/html; charset=UTF-8");
|
||||||
mimeMessage.setFrom(from);
|
mimeMessage.setFrom(from);
|
||||||
|
|
||||||
|
t.connect();
|
||||||
|
|
||||||
// EMAIL RECIPIENTS
|
// EMAIL RECIPIENTS
|
||||||
for (int i=0; i<emailrecipients.length; i++) {
|
for (int i=0; i<emailrecipients.length; i++) {
|
||||||
Address address = new InternetAddress(emailrecipients[i]);
|
Address address = new InternetAddress(emailrecipients[i]);
|
||||||
mimeMessage.addRecipient(Message.RecipientType.TO, address);
|
mimeMessage.addRecipient(Message.RecipientType.TO, address);
|
||||||
}
|
}
|
||||||
|
// EMAIL CC Recipients
|
||||||
|
for (InternetAddress email : emailRecipientsInCC) {
|
||||||
|
mimeMessage.addRecipient(Message.RecipientType.CC, email);
|
||||||
|
}
|
||||||
|
|
||||||
mimeMessage.setSubject(emailSubject);
|
mimeMessage.setSubject(emailSubject);
|
||||||
mimeMessage.setContent(emailBody.toString(), "text/html; charset=UTF-8");
|
mimeMessage.setContent(emailBody.toString(), "text/html; charset=UTF-8");
|
||||||
mimeMessage.setSentDate(new Date());
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
_log.error("Failed to send the email message.", e);
|
_log.error("Failed to send the email message.", e);
|
||||||
|
try {
|
||||||
|
t.close();
|
||||||
|
} catch (MessagingException e1) {
|
||||||
|
e1.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue