Skip to content

Commit

Permalink
Implement API to test mail
Browse files Browse the repository at this point in the history
  • Loading branch information
Abyss777 committed Nov 30, 2016
1 parent 963adbb commit b6d9d99
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
10 changes: 10 additions & 0 deletions src/org/traccar/api/resource/NotificationResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -29,7 +30,9 @@

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)
Expand Down Expand Up @@ -57,4 +60,11 @@ public Response update(Notification entity) throws SQLException {
return Response.ok(entity).build();
}

@Path("testmail")
@GET
public Response testMail() throws MessagingException {
NotificationMail.sendMailSync(getUserId(), new Event("unknown", 0), null);
return Response.noContent().build();
}

}
63 changes: 31 additions & 32 deletions src/org/traccar/notification/NotificationMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,52 +79,51 @@ private static Properties getProperties(PropertiesProvider provider) {
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();
}
Expand Down

0 comments on commit b6d9d99

Please sign in to comment.