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
This commit is contained in:
Massimiliano Assante 2016-07-05 12:10:31 +00:00
parent 5b5f785b4b
commit ae24cd0d7d
1 changed files with 21 additions and 12 deletions

View File

@ -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<InternetAddress> 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<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;
@ -89,7 +91,7 @@ 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));
@ -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<emailrecipients.length; i++) {
Address address = new InternetAddress(emailrecipients[i]);
@ -126,7 +135,7 @@ public class EmailNotification {
mimeMessage.setSubject(emailSubject);
mimeMessage.setContent(emailBody.toString(), "text/html; charset=UTF-8");
mimeMessage.setSentDate(new Date());
try {
Transport.send(mimeMessage);
}
@ -139,8 +148,8 @@ public class EmailNotification {
_log.error("Failed to send the email message.", e);
}
}
}