aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/mail
diff options
context:
space:
mode:
authorYuriy Piskarev <yuriy.piskarev@gmail.com>2023-08-24 14:16:17 +0300
committerGitHub <noreply@github.com>2023-08-24 14:16:17 +0300
commitae406c7b49a72de24d81fd74386d9638342c90ee (patch)
tree6fbcf557375b98e926c78af9c757e62c79d72a1b /src/main/java/org/traccar/mail
parent56ff656c908b19feb2fa3dcffa48cc3bcdfe9b3b (diff)
parent9aeedc90da24848ff97227d6f281eb4d1e1506ef (diff)
downloadtrackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.tar.gz
trackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.tar.bz2
trackermap-server-ae406c7b49a72de24d81fd74386d9638342c90ee.zip
Merge branch 'traccar:master' into master
Diffstat (limited to 'src/main/java/org/traccar/mail')
-rw-r--r--src/main/java/org/traccar/mail/LogMailManager.java48
-rw-r--r--src/main/java/org/traccar/mail/MailManager.java33
-rw-r--r--src/main/java/org/traccar/mail/SmtpMailManager.java162
3 files changed, 243 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/mail/LogMailManager.java b/src/main/java/org/traccar/mail/LogMailManager.java
new file mode 100644
index 000000000..90de3bcce
--- /dev/null
+++ b/src/main/java/org/traccar/mail/LogMailManager.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2022 - 2023 Anton Tananaev (anton@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.mail;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.traccar.model.User;
+
+import jakarta.mail.MessagingException;
+import jakarta.mail.internet.MimeBodyPart;
+
+public class LogMailManager implements MailManager {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(LogMailManager.class);
+
+ @Override
+ public boolean getEmailEnabled() {
+ return true;
+ }
+
+ @Override
+ public void sendMessage(
+ User user, boolean system, String subject, String body) throws MessagingException {
+ sendMessage(user, system, subject, body, null);
+ }
+
+ @Override
+ public void sendMessage(
+ User user, boolean system, String subject, String body, MimeBodyPart attachment) throws MessagingException {
+ LOGGER.info(
+ "Email sent\nTo: {}\nSubject: {}\nAttachment: {}\nBody:\n{}",
+ user.getEmail(), subject, attachment != null ? attachment.getFileName() : null, body);
+ }
+
+}
diff --git a/src/main/java/org/traccar/mail/MailManager.java b/src/main/java/org/traccar/mail/MailManager.java
new file mode 100644
index 000000000..d05a07de9
--- /dev/null
+++ b/src/main/java/org/traccar/mail/MailManager.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2022 - 2023 Anton Tananaev (anton@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.mail;
+
+import org.traccar.model.User;
+
+import jakarta.mail.MessagingException;
+import jakarta.mail.internet.MimeBodyPart;
+
+public interface MailManager {
+
+ boolean getEmailEnabled();
+
+ void sendMessage(
+ User user, boolean system, String subject, String body) throws MessagingException;
+
+ void sendMessage(
+ User user, boolean system, String subject, String body, MimeBodyPart attachment) throws MessagingException;
+
+}
diff --git a/src/main/java/org/traccar/mail/SmtpMailManager.java b/src/main/java/org/traccar/mail/SmtpMailManager.java
new file mode 100644
index 000000000..70099d879
--- /dev/null
+++ b/src/main/java/org/traccar/mail/SmtpMailManager.java
@@ -0,0 +1,162 @@
+/*
+ * 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.mail;
+
+import org.traccar.config.Config;
+import org.traccar.config.ConfigKey;
+import org.traccar.config.Keys;
+import org.traccar.database.StatisticsManager;
+import org.traccar.model.User;
+import org.traccar.notification.PropertiesProvider;
+
+import jakarta.mail.BodyPart;
+import jakarta.mail.Message;
+import jakarta.mail.MessagingException;
+import jakarta.mail.Multipart;
+import jakarta.mail.Session;
+import jakarta.mail.Transport;
+import jakarta.mail.internet.InternetAddress;
+import jakarta.mail.internet.MimeBodyPart;
+import jakarta.mail.internet.MimeMessage;
+import jakarta.mail.internet.MimeMultipart;
+import java.io.UnsupportedEncodingException;
+import java.util.Date;
+import java.util.Properties;
+
+public final class SmtpMailManager implements MailManager {
+
+ private static final String CONTENT_TYPE = "text/html; charset=utf-8";
+
+ private final Config config;
+ private final StatisticsManager statisticsManager;
+
+ public SmtpMailManager(Config config, StatisticsManager statisticsManager) {
+ this.config = config;
+ this.statisticsManager = statisticsManager;
+ }
+
+ private static void copyBooleanProperty(
+ Properties properties, PropertiesProvider provider, ConfigKey<Boolean> 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<String> 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);
+ }
+
+ @Override
+ public void sendMessage(
+ User user, boolean system, String subject, String body) throws MessagingException {
+ sendMessage(user, system, subject, body, null);
+ }
+
+ @Override
+ public void sendMessage(
+ User user, boolean system, 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 && (system || !config.getBoolean(Keys.MAIL_SMTP_SYSTEM_ONLY))) {
+ 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());
+ }
+ }
+
+}