aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/notification/NotificationMail.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2018-06-28 17:07:25 +1200
committerGitHub <noreply@github.com>2018-06-28 17:07:25 +1200
commite849d3a56f55c83dc0ebed02165a124e30e22dbd (patch)
tree730e542145652ca1d06c64a6944dfa70e0b31b6b /src/org/traccar/notification/NotificationMail.java
parent4d6229c7daa4a7b8f28337c5eca9a422876c98b1 (diff)
parent14840af2abd9976b8f5634af8e77f4a7126dfac1 (diff)
downloadtrackermap-server-e849d3a56f55c83dc0ebed02165a124e30e22dbd.tar.gz
trackermap-server-e849d3a56f55c83dc0ebed02165a124e30e22dbd.tar.bz2
trackermap-server-e849d3a56f55c83dc0ebed02165a124e30e22dbd.zip
Merge pull request #3950 from Abyss777/notification_refactor
Notification refactor to allow custom "notificators"
Diffstat (limited to 'src/org/traccar/notification/NotificationMail.java')
-rw-r--r--src/org/traccar/notification/NotificationMail.java66
1 files changed, 28 insertions, 38 deletions
diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java
index 6c81ecc79..c2ee67299 100644
--- a/src/org/traccar/notification/NotificationMail.java
+++ b/src/org/traccar/notification/NotificationMail.java
@@ -1,6 +1,6 @@
/*
- * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org)
- * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
+ * Copyright 2016 - 2018 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.
@@ -32,10 +32,7 @@ import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.model.User;
-public final class NotificationMail {
-
- private NotificationMail() {
- }
+public final class NotificationMail extends Notificator {
private static Properties getProperties(PropertiesProvider provider) {
Properties properties = new Properties();
@@ -84,7 +81,8 @@ public final class NotificationMail {
return properties;
}
- public static void sendMailSync(long userId, Event event, Position position) throws MessagingException {
+ @Override
+ public void sendSync(long userId, Event event, Position position) throws MessageException {
User user = Context.getPermissionsManager().getUser(userId);
Properties properties = null;
@@ -103,40 +101,32 @@ public final class NotificationMail {
MimeMessage message = new MimeMessage(session);
- String from = properties.getProperty("mail.smtp.from");
- if (from != null) {
- message.setFrom(new InternetAddress(from));
- }
-
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
- MailMessage mailMessage = NotificationFormatter.formatMailMessage(userId, event, position);
- message.setSubject(mailMessage.getSubject());
- message.setSentDate(new Date());
- message.setContent(mailMessage.getBody(), "text/html; charset=utf-8");
-
- Transport transport = session.getTransport();
try {
- Context.getStatisticsManager().registerMail();
- transport.connect(
- properties.getProperty("mail.smtp.host"),
- properties.getProperty("mail.smtp.username"),
- properties.getProperty("mail.smtp.password"));
- transport.sendMessage(message, message.getAllRecipients());
- } finally {
- transport.close();
- }
- }
+ String from = properties.getProperty("mail.smtp.from");
+ if (from != null) {
+ message.setFrom(new InternetAddress(from));
+ }
- public static void sendMailAsync(final long userId, final Event event, final Position position) {
- new Thread(new Runnable() {
- public void run() {
- try {
- sendMailSync(userId, event, position);
- } catch (MessagingException error) {
- Log.warning(error);
- }
+ message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
+ FullMessage fullMessage = NotificationFormatter.formatFullMessage(userId, event, position);
+ message.setSubject(fullMessage.getSubject());
+ message.setSentDate(new Date());
+ message.setContent(fullMessage.getBody(), "text/html; charset=utf-8");
+
+ Transport transport = session.getTransport();
+ try {
+ Context.getStatisticsManager().registerMail();
+ transport.connect(
+ properties.getProperty("mail.smtp.host"),
+ properties.getProperty("mail.smtp.username"),
+ properties.getProperty("mail.smtp.password"));
+ transport.sendMessage(message, message.getAllRecipients());
+ } finally {
+ transport.close();
}
- }).start();
+ } catch (MessagingException e) {
+ throw new MessageException(e);
+ }
}
}