blob: 733438680c603241f8219f6223e4e76f06be1d23 [file] [log] [blame]
/**
*
* Copyright (c) 2019 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*
*/
package org.eclipse.osbp.utils.email.common;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.MultiPartEmail;
import org.eclipse.osbp.preferences.ProductConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Email {
private static Logger log = LoggerFactory.getLogger(Email.class);
private MultiPartEmail mail;
private List<String> toAddresses;
private String from;
public Email() {
log.debug("Instantiate Email");
mail = new MultiPartEmail();
mail.setDebug(log.isDebugEnabled());
mail.setHostName(ProductConfiguration.getEmailServerIp());
if (ProductConfiguration.isEmailUseSslOnConnect()) {
Integer port = ProductConfiguration.getEmailSmtpPort();
mail.setSslSmtpPort(port.toString());
mail.setSSLOnConnect(true);
} else {
mail.setSmtpPort(ProductConfiguration.getEmailSmtpPort());
mail.setSSLOnConnect(false);
}
if (!ProductConfiguration.getAdminEmailUsername().isEmpty()) {
mail.setAuthenticator(new DefaultAuthenticator(ProductConfiguration.getAdminEmailUsername(),
ProductConfiguration.getAdminEmailPassword()));
}
String charset = "utf-8";
mail.setCharset(charset);
setFrom(ProductConfiguration.getAdminEmail());
toAddresses = new ArrayList<>();
}
public void setFrom(String from) {
// Set From: header field of the header.
try {
if (!from.isEmpty()) {
log.debug("set From: {}", from);
mail.setFrom(from);
this.from = from;
}
} catch (Exception e) {
log.error("set From: {}", e.getMessage());
}
}
public String getFrom() {
return from;
}
public void setTo(String to) {
try {
if (!to.isEmpty() && !(toAddresses.contains(to))) {
log.debug("set To: {}", to);
toAddresses.add(to);
mail.addTo(to);
} else {
log.debug("skip To: {}", to);
}
} catch (Exception e) {
log.error("set To: {}", e.getMessage());
}
}
public void setCc(String to) {
try {
if (!to.isEmpty() && !(toAddresses.contains(to))) {
log.debug("set CC: {}", to);
toAddresses.add(to);
mail.addCc(to);
} else {
log.debug("skip CC: {}", to);
}
} catch (Exception e) {
log.error("set CC: {}", e.getMessage());
}
}
public void setBcc(String to) {
try {
if (!to.isEmpty() && !(toAddresses.contains(to))) {
log.debug("set BCC: {}", to);
toAddresses.add(to);
mail.addBcc(to);
} else {
log.debug("skip BCC: {}", to);
}
} catch (Exception e) {
log.error("set BCC: {}", e.getMessage());
}
}
public void setSubject(String subject) {
try {
log.debug("set Subject: {}", subject);
mail.setSubject(subject);
} catch (Exception e) {
log.error("set Subject: {}", e.getMessage());
}
}
public void setBody(String body) {
try {
log.debug("set Body: {}", body);
MimeMultipart multiPart = new MimeMultipart("alternative");
MimeBodyPart textPart = new MimeBodyPart();
String text=body.replaceAll("</*b>", "").replaceAll("</div>", "\n ").replaceAll("<br>", "\n ").replaceAll("</*t[r|h|d]>", "").replaceAll("</*table>", "");
text=text.replaceAll("(?s)<[^>]*>(\\s*<[^>]*>)*", "");
textPart.setText(text, "utf-8");
textPart.setHeader("Content-Transfer-Encoding", "base64");
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(body, "text/html; charset=utf-8");
htmlPart.setHeader("Content-Transfer-Encoding", "base64");
multiPart.addBodyPart(textPart); // <-- first
multiPart.addBodyPart(htmlPart); // <-- second
mail.addPart(multiPart);
} catch (Exception e) {
log.error("set Body: {}", e.getMessage());
}
}
public void setHighPriority(boolean b) {
try {
if (b) {
log.debug("set Highprio ");
mail.addHeader("X-Priority","1");
mail.addHeader("X-MSMail-Priority","High");
mail.addHeader("Importance","High");
}
} catch (Exception e) {
log.error("set Highprio: {}", e.getMessage());
}
}
public void send() {
try {
// Send message
log.debug("send mail ...");
mail.setSentDate(new Date());
mail.send();
} catch (Exception e) {
log.error("Send mail: {}", e.getMessage());
}
}
}