Added support for email plugin multi part (text and html) handling

git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/portal/portal-manager@148367 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Massimiliano Assante 2017-05-08 13:26:00 +00:00
parent 51ed4a70ea
commit 82528e23fc
4 changed files with 94 additions and 24 deletions

View File

@ -20,7 +20,7 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="owner.project.facets" value="java"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>

View File

@ -1,5 +1,8 @@
<ReleaseNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xsd/changelog.xsd">
<Changeset component="org.gcube.common.portal.portal-manager.2-3-0" date="2017-05-08">
<Change>Added support for email plugin multi part (text and html) handling</Change>
</Changeset>
<Changeset component="org.gcube.common.portal.portal-manager.2-1-0" date="2016-11-18">
<Change>Added support for ASLSession methods replacement</Change>
</Changeset>

View File

@ -10,7 +10,7 @@
<groupId>org.gcube.common.portal</groupId>
<artifactId>portal-manager</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.3.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>gCube Portal Manager</name>
<description>

View File

@ -7,11 +7,14 @@ import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServletRequest;
import org.gcube.common.portal.PortalContext;
@ -37,9 +40,13 @@ public class EmailNotification {
*/
private String emailSubject;
/**
* Email's body message
* Email's body message in text/html
*/
private StringBuffer emailBody;
private StringBuffer emailBodyTextHTML;
/**
* Email's body message in text/plain
*/
private StringBuffer emailBodyTextPlain;
/**
*
*/
@ -50,48 +57,77 @@ public class EmailNotification {
/**
*
* @param recipient an email address
* @param subject the subject of your email
* @param body the body of your email
* @param subject the subject of your email
* @param bodyTextHTML the body of your email in HTML
* @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 bodyTextHTML, HttpServletRequest httpServletRequest) {
String[] emailRecipients = new String[1];
emailRecipients[0] = recipient;
init(httpServletRequest, emailRecipients, subject, body);
init(httpServletRequest, emailRecipients, subject, bodyTextHTML, null);
}
/**
*
* @param recipient an email address
* @param subject the subject of your email
* @param bodyTextHTML the body of your email in text/html
* @param bodyTextPlain the body of your email in text/plain
* @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 bodyTextHTML, String bodyTextPlain, HttpServletRequest httpServletRequest) {
String[] emailRecipients = new String[1];
emailRecipients[0] = recipient;
init(httpServletRequest, emailRecipients, subject, bodyTextHTML, bodyTextPlain);
}
/**
* @param recipients an array of email addresses
* @param subject the subject of your email
* @param body the body of your email
* @param bodyTextHTML the body of your email in HTML
* @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);
public EmailNotification(String recipients[], String subject, String bodyTextHTML, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients, subject, bodyTextHTML, null);
}
/**
* @param recipients an array of email addresses
* @param subject the subject of your email
* @param bodyTextHTML the body of your email in text/html
* @param bodyTextPlain the body of your email in text/plain
* @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 bodyTextHTML, String bodyTextPlain, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients, subject, bodyTextHTML, bodyTextPlain);
}
/**
* @param recipients a list of email addresses
* @param subject the subject of your email
* @param body the body of your email
* @param bodyTextHTML the body of your email in text/html
* @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);
public EmailNotification(List<String> recipients, String subject, String bodyTextHTML, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients.toArray(new String[recipients.size()]), subject, bodyTextHTML, null);
}
/**
* @param recipients a list of email addresses
* @param subject the subject of your email
* @param bodyTextHTML the body of your email in text/html
* @param bodyTextPlain the body of your email in text/plain
* @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 bodyTextHTML, String bodyTextPlain, HttpServletRequest httpServletRequest) {
init(httpServletRequest, recipients.toArray(new String[recipients.size()]), subject, bodyTextHTML, bodyTextPlain);
}
private void init(HttpServletRequest httpServletRequest, String recipients[], String subject, String body) {
private void init(HttpServletRequest httpServletRequest, String recipients[], String subject, String bodyHTML, String bodyPlainText) {
request = httpServletRequest;
emailrecipients = recipients;
emailSubject = subject;
emailRecipientsInCC = new ArrayList<InternetAddress>();
emailRecipientsInBCC = 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;\">")
.append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain ")
.append("information which is privileged, confidential, proprietary, or exempt from disclosure under applicable law. If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, you are strictly prohibited from disclosing, distributing, copying, or in any way using this message. ")
.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>");
emailBodyTextHTML = new StringBuffer(bodyHTML).append(getWarningLegalText(false));
emailBodyTextPlain = (bodyPlainText != null) ? new StringBuffer(bodyPlainText).append(getWarningLegalText(true)) : null;
}
public void addRecipientInCC(String email) {
@ -146,7 +182,22 @@ public class EmailNotification {
mimeMessage.addRecipient(Message.RecipientType.BCC, email);
}
mimeMessage.setSubject(emailSubject);
mimeMessage.setContent(emailBody.toString(), "text/html; charset=UTF-8");
if (emailBodyTextPlain != null) {
final MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent(emailBodyTextPlain.toString(), "text/plain; charset=UTF-8");
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(emailBodyTextHTML.toString(), "text/html; charset=UTF-8");
final Multipart mp = new MimeMultipart("alternative");
mp.addBodyPart(textPart);
mp.addBodyPart(htmlPart);
// Set Multipart as the message's content
mimeMessage.setContent(mp);
} else {
mimeMessage.setContent(emailBodyTextHTML.toString(), "text/html; charset=UTF-8");
}
mimeMessage.setSentDate(new Date());
try {
@ -162,7 +213,23 @@ public class EmailNotification {
}
}
public static StringBuffer getWarningLegalText(boolean plainText) {
StringBuffer toReturn = new StringBuffer();
if (!plainText) {
toReturn.append("<p>")
.append("<p><div style=\"color:#999999; font-size:10px; font-family:'lucida grande',tahoma,verdana,arial,sans-serif; padding-top:15px;\">")
.append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain ")
.append("information which is privileged, confidential, proprietary, or exempt from disclosure under applicable law. If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, you are strictly prohibited from disclosing, distributing, copying, or in any way using this message. ")
.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>");
} else {
toReturn.append("\n\n---------\n")
.append("WARNING / LEGAL TEXT: This message is intended only for the use of the individual or entity to which it is addressed and may contain ")
.append("information which is privileged, confidential, proprietary, or exempt from disclosure under applicable law. \n If you are not the intended recipient or the person responsible for delivering the message to the intended recipient, you are strictly prohibited from disclosing, distributing, copying, or in any way using this message. ")
.append("If you have received this communication in error, please notify the sender and destroy and delete any copies you may have received.");
}
return toReturn;
}
}