diff options
author | Abyss777 <abyss@fox5.ru> | 2016-11-30 14:18:26 +0500 |
---|---|---|
committer | Abyss777 <abyss@fox5.ru> | 2016-11-30 14:18:26 +0500 |
commit | b6d9d992b3b5eaaa5c7fec33fd63e28480f05d97 (patch) | |
tree | 3ab2814893a6da8064955c012b56508bacd89ffa /src/org/traccar/notification | |
parent | 963adbba43d2849c968da85e51112f834b550f8d (diff) | |
download | trackermap-server-b6d9d992b3b5eaaa5c7fec33fd63e28480f05d97.tar.gz trackermap-server-b6d9d992b3b5eaaa5c7fec33fd63e28480f05d97.tar.bz2 trackermap-server-b6d9d992b3b5eaaa5c7fec33fd63e28480f05d97.zip |
Implement API to test mail
Diffstat (limited to 'src/org/traccar/notification')
-rw-r--r-- | src/org/traccar/notification/NotificationMail.java | 63 |
1 files changed, 31 insertions, 32 deletions
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(); } |