aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-11-30 23:14:14 +1300
committerGitHub <noreply@github.com>2016-11-30 23:14:14 +1300
commit3d25b6c29ad346120f1fecbe7130800da417bc41 (patch)
tree9a58b8c14df8aebf0952236a4a2a5e91d9621b52
parent963adbba43d2849c968da85e51112f834b550f8d (diff)
parentc2d479e2acfa4f89b01d80ca902938dc2e37c1b6 (diff)
downloadtraccar-server-3d25b6c29ad346120f1fecbe7130800da417bc41.tar.gz
traccar-server-3d25b6c29ad346120f1fecbe7130800da417bc41.tar.bz2
traccar-server-3d25b6c29ad346120f1fecbe7130800da417bc41.zip
Merge pull request #2619 from Abyss777/test_mail
Implement API to test mail
-rw-r--r--src/org/traccar/api/resource/NotificationResource.java10
-rw-r--r--src/org/traccar/notification/NotificationMail.java63
2 files changed, 41 insertions, 32 deletions
diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java
index c43cca122..6c9725f37 100644
--- a/src/org/traccar/api/resource/NotificationResource.java
+++ b/src/org/traccar/api/resource/NotificationResource.java
@@ -18,6 +18,7 @@ package org.traccar.api.resource;
import java.sql.SQLException;
import java.util.Collection;
+import javax.mail.MessagingException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
@@ -29,7 +30,9 @@ import javax.ws.rs.core.Response;
import org.traccar.Context;
import org.traccar.api.BaseResource;
+import org.traccar.model.Event;
import org.traccar.model.Notification;
+import org.traccar.notification.NotificationMail;
@Path("users/notifications")
@Produces(MediaType.APPLICATION_JSON)
@@ -57,4 +60,11 @@ public class NotificationResource extends BaseResource {
return Response.ok(entity).build();
}
+ @Path("test")
+ @POST
+ public Response testMail() throws MessagingException {
+ NotificationMail.sendMailSync(getUserId(), new Event("test", 0), null);
+ return Response.noContent().build();
+ }
+
}
diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java
index c7c1d689c..17e4d6be4 100644
--- a/src/org/traccar/notification/NotificationMail.java
+++ b/src/org/traccar/notification/NotificationMail.java
@@ -79,52 +79,51 @@ public final class NotificationMail {
return properties;
}
- private static void sendMailSync(long userId, Event event, Position position) {
- try {
- User user = Context.getPermissionsManager().getUser(userId);
+ public static void sendMailSync(long userId, Event event, Position position) throws MessagingException {
+ User user = Context.getPermissionsManager().getUser(userId);
- Properties properties = getProperties(new PropertiesProvider(Context.getConfig()));
+ Properties properties = getProperties(new PropertiesProvider(Context.getConfig()));
+ if (!properties.containsKey("mail.smtp.host")) {
+ properties = getProperties(new PropertiesProvider(user));
if (!properties.containsKey("mail.smtp.host")) {
- properties = getProperties(new PropertiesProvider(user));
- if (!properties.containsKey("mail.smtp.host")) {
- Log.warning("No SMTP configuration found");
- return;
- }
+ Log.warning("No SMTP configuration found");
+ return;
}
+ }
- Session session = Session.getInstance(properties);
+ Session session = Session.getInstance(properties);
- MimeMessage message = new MimeMessage(session);
+ MimeMessage message = new MimeMessage(session);
- String from = properties.getProperty("mail.smtp.from");
- if (from != null) {
- message.setFrom(new InternetAddress(from));
- }
+ String from = properties.getProperty("mail.smtp.from");
+ if (from != null) {
+ message.setFrom(new InternetAddress(from));
+ }
- message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
- message.setSubject(NotificationFormatter.formatTitle(userId, event, position));
- message.setText(NotificationFormatter.formatMessage(userId, event, position));
-
- Transport transport = session.getTransport();
- try {
- 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();
- }
+ message.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail()));
+ message.setSubject(NotificationFormatter.formatTitle(userId, event, position));
+ message.setText(NotificationFormatter.formatMessage(userId, event, position));
- } catch (MessagingException error) {
- Log.warning(error);
+ Transport transport = session.getTransport();
+ try {
+ 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();
}
}
public static void sendMailAsync(final long userId, final Event event, final Position position) {
new Thread(new Runnable() {
public void run() {
- sendMailSync(userId, event, position);
+ try {
+ sendMailSync(userId, event, position);
+ } catch (MessagingException error) {
+ Log.warning(error);
+ }
}
}).start();
}