| /** |
| * |
| * 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 email; |
| private List<String> toAddresses; |
| private String from; |
| |
| |
| public Email() { |
| |
| log.debug("Instantiate Email"); |
| email = new MultiPartEmail(); |
| email.setDebug(log.isDebugEnabled()); |
| |
| email.setHostName(ProductConfiguration.getEmailServerIp()); |
| |
| if (ProductConfiguration.isEmailUseSslOnConnect()) { |
| Integer port = ProductConfiguration.getEmailSmtpPort(); |
| email.setSslSmtpPort(port.toString()); |
| email.setSSLOnConnect(true); |
| } else { |
| email.setSmtpPort(ProductConfiguration.getEmailSmtpPort()); |
| email.setSSLOnConnect(false); |
| } |
| |
| if (!ProductConfiguration.getAdminEmailUsername().isEmpty()) { |
| email.setAuthenticator(new DefaultAuthenticator(ProductConfiguration.getAdminEmailUsername(), |
| ProductConfiguration.getAdminEmailPassword())); |
| } |
| |
| String charset = "utf-8"; |
| email.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); |
| email.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); |
| email.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); |
| email.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); |
| email.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); |
| email.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(); |
| textPart.setText(body.replaceAll("</*b>", "*").replaceAll("<br>", "").replaceAll("</*t[r|h|d]>", "").replaceAll("</*table>", ""), "utf-8"); |
| |
| MimeBodyPart htmlPart = new MimeBodyPart(); |
| htmlPart.setContent(body, "text/html; charset=utf-8"); |
| |
| multiPart.addBodyPart(textPart); // <-- first |
| multiPart.addBodyPart(htmlPart); // <-- second |
| email.addPart(multiPart); |
| } catch (Exception e) { |
| log.error("set Body: {}", e.getMessage()); |
| } |
| } |
| |
| |
| public void setHighPriority(boolean b) { |
| try { |
| if (b) { |
| log.debug("set Highprio "); |
| email.addHeader("X-Priority","1"); |
| email.addHeader("X-MSMail-Priority","High"); |
| email.addHeader("Importance","High"); |
| } |
| } catch (Exception e) { |
| log.error("set Highprio: {}", e.getMessage()); |
| } |
| |
| |
| } |
| |
| public void send() { |
| try { |
| // Send message |
| log.debug("send mail ..."); |
| email.setSentDate(new Date()); |
| email.send(); |
| } catch (Exception e) { |
| log.error("Send mail: {}", e.getMessage()); |
| } |
| |
| } |
| } |