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.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.AddressException;
@ -30,7 +29,7 @@ public class EmailNotification {
* The recipients of the email * The recipients of the email
*/ */
private String emailrecipients[]; private String emailrecipients[];
private List<InternetAddress> emailRecipientsInCC; private List<InternetAddress> emailRecipientsInCC;
/** /**
* Email's subject * Email's subject
@ -44,7 +43,7 @@ public class EmailNotification {
* *
*/ */
private HttpServletRequest request; private HttpServletRequest request;
private final String MAIL_SERVICE_HOST = "localhost"; private final String MAIL_SERVICE_HOST = "localhost";
private String MAIL_SERVICE_PORT = "25"; private String MAIL_SERVICE_PORT = "25";
/** /**
@ -52,6 +51,7 @@ public class EmailNotification {
* @param recipient an email address * @param recipient an email address
* @param subject the subject of your email * @param subject the subject of your email
* @param body the body 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) { public EmailNotification(String recipient, String subject, String body, HttpServletRequest httpServletRequest) {
String[] emailRecipients = new String[1]; String[] emailRecipients = new String[1];
@ -62,20 +62,22 @@ public class EmailNotification {
* @param recipients an array of email addresses * @param recipients an array of email addresses
* @param subject the subject of your email * @param subject the subject of your email
* @param body the body 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) { public EmailNotification(String recipients[], String subject, String body, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients, subject, body); init(httpServletRequest, recipients, subject, body);
} }
/** /**
* @param recipients a list of email addresses * @param recipients a list of email addresses
* @param subject the subject of your email * @param subject the subject of your email
* @param body the body 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) { public EmailNotification(List<String> recipients, String subject, String body, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients.toArray(new String[recipients.size()]), subject, body); init(httpServletRequest, recipients.toArray(new String[recipients.size()]), subject, body);
} }
private void init(HttpServletRequest httpServletRequest, String recipients[], String subject, String body) { private void init(HttpServletRequest httpServletRequest, String recipients[], String subject, String body) {
request = httpServletRequest; request = httpServletRequest;
emailrecipients = recipients; 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("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) { public void addRecipientInCC(String email) {
try { try {
emailRecipientsInCC.add(new InternetAddress(email)); emailRecipientsInCC.add(new InternetAddress(email));
@ -106,13 +108,20 @@ public class EmailNotification {
session.setDebug(true); session.setDebug(true);
Message mimeMessage = new MimeMessage(session); Message mimeMessage = new MimeMessage(session);
try { try {
String emailSender = "";
String siteName = "";
// EMAIL SENDER // EMAIL SENDER
String emailSender = PortalContext.getConfiguration().getSenderEmail(request); if (request != null) {
String siteName = PortalContext.getConfiguration().getGatewayName(request); 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); 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);
// 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]);
@ -126,7 +135,7 @@ public class EmailNotification {
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());
try { try {
Transport.send(mimeMessage); Transport.send(mimeMessage);
} }
@ -139,8 +148,8 @@ public class EmailNotification {
_log.error("Failed to send the email message.", e); _log.error("Failed to send the email message.", e);
} }
} }
} }