From f5033ce11b823a4fce080211368b8c6473d81e7b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 26 Jul 2022 16:59:58 -0700 Subject: Rename mail manager --- .../java/org/traccar/database/MailManager.java | 161 --------------------- 1 file changed, 161 deletions(-) delete mode 100644 src/main/java/org/traccar/database/MailManager.java (limited to 'src/main/java/org/traccar/database') diff --git a/src/main/java/org/traccar/database/MailManager.java b/src/main/java/org/traccar/database/MailManager.java deleted file mode 100644 index ec1681dcb..000000000 --- a/src/main/java/org/traccar/database/MailManager.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright 2016 - 2022 Anton Tananaev (anton@traccar.org) - * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.database; - -import org.traccar.config.Config; -import org.traccar.config.ConfigKey; -import org.traccar.config.Keys; -import org.traccar.model.User; -import org.traccar.notification.PropertiesProvider; - -import javax.inject.Inject; -import javax.mail.BodyPart; -import javax.mail.Message; -import javax.mail.MessagingException; -import javax.mail.Multipart; -import javax.mail.Session; -import javax.mail.Transport; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeBodyPart; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; -import java.io.UnsupportedEncodingException; -import java.util.Date; -import java.util.Properties; - -public final class MailManager { - - private static final String CONTENT_TYPE = "text/html; charset=utf-8"; - - private final Config config; - private final StatisticsManager statisticsManager; - - @Inject - public MailManager(Config config, StatisticsManager statisticsManager) { - this.config = config; - this.statisticsManager = statisticsManager; - } - - private static void copyBooleanProperty( - Properties properties, PropertiesProvider provider, ConfigKey key) { - Boolean value = provider.getBoolean(key); - if (value != null) { - properties.put(key.getKey(), String.valueOf(value)); - } - } - - private static void copyStringProperty( - Properties properties, PropertiesProvider provider, ConfigKey key) { - String value = provider.getString(key); - if (value != null) { - properties.put(key.getKey(), value); - } - } - - private static Properties getProperties(PropertiesProvider provider) { - String host = provider.getString(Keys.MAIL_SMTP_HOST); - if (host != null) { - Properties properties = new Properties(); - - properties.put(Keys.MAIL_TRANSPORT_PROTOCOL.getKey(), provider.getString(Keys.MAIL_TRANSPORT_PROTOCOL)); - properties.put(Keys.MAIL_SMTP_HOST.getKey(), host); - properties.put(Keys.MAIL_SMTP_PORT.getKey(), String.valueOf(provider.getInteger(Keys.MAIL_SMTP_PORT))); - - copyBooleanProperty(properties, provider, Keys.MAIL_SMTP_STARTTLS_ENABLE); - copyBooleanProperty(properties, provider, Keys.MAIL_SMTP_STARTTLS_REQUIRED); - copyBooleanProperty(properties, provider, Keys.MAIL_SMTP_SSL_ENABLE); - copyStringProperty(properties, provider, Keys.MAIL_SMTP_SSL_TRUST); - copyStringProperty(properties, provider, Keys.MAIL_SMTP_SSL_PROTOCOLS); - copyStringProperty(properties, provider, Keys.MAIL_SMTP_USERNAME); - copyStringProperty(properties, provider, Keys.MAIL_SMTP_PASSWORD); - copyStringProperty(properties, provider, Keys.MAIL_SMTP_FROM); - copyStringProperty(properties, provider, Keys.MAIL_SMTP_FROM_NAME); - - return properties; - } - return null; - } - - public boolean getEmailEnabled() { - return config.hasKey(Keys.MAIL_SMTP_HOST); - } - - public void sendMessage( - User user, String subject, String body) throws MessagingException { - sendMessage(user, subject, body, null); - } - - public void sendMessage( - User user, String subject, String body, MimeBodyPart attachment) throws MessagingException { - - Properties properties = null; - if (!config.getBoolean(Keys.MAIL_SMTP_IGNORE_USER_CONFIG)) { - properties = getProperties(new PropertiesProvider(user)); - } - if (properties == null) { - properties = getProperties(new PropertiesProvider(config)); - } - if (properties == null) { - throw new MessagingException("No SMTP configuration found"); - } - - Session session = Session.getInstance(properties); - - MimeMessage message = new MimeMessage(session); - - String from = properties.getProperty(Keys.MAIL_SMTP_FROM.getKey()); - if (from != null) { - String fromName = properties.getProperty(Keys.MAIL_SMTP_FROM_NAME.getKey()); - if (fromName != null) { - try { - message.setFrom(new InternetAddress(from, fromName)); - } catch (UnsupportedEncodingException e) { - throw new MessagingException("Email address issue"); - } - } else { - message.setFrom(new InternetAddress(from)); - } - } - - message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); - message.setSubject(subject); - message.setSentDate(new Date()); - - if (attachment != null) { - Multipart multipart = new MimeMultipart(); - - BodyPart messageBodyPart = new MimeBodyPart(); - messageBodyPart.setContent(body, CONTENT_TYPE); - multipart.addBodyPart(messageBodyPart); - multipart.addBodyPart(attachment); - - message.setContent(multipart); - } else { - message.setContent(body, CONTENT_TYPE); - } - - try (Transport transport = session.getTransport()) { - statisticsManager.registerMail(); - transport.connect( - properties.getProperty(Keys.MAIL_SMTP_HOST.getKey()), - properties.getProperty(Keys.MAIL_SMTP_USERNAME.getKey()), - properties.getProperty(Keys.MAIL_SMTP_PASSWORD.getKey())); - transport.sendMessage(message, message.getAllRecipients()); - } - } - -} -- cgit v1.2.3