use of org.traccar.notification.PropertiesProvider in project traccar by tananaev.
the class MailManager method sendMessage.
public void sendMessage(long userId, String subject, String body, MimeBodyPart attachment) throws MessagingException {
User user = Context.getPermissionsManager().getUser(userId);
Properties properties = null;
if (!Context.getConfig().getBoolean("mail.smtp.ignoreUserConfig")) {
properties = getProperties(new PropertiesProvider(user));
}
if (properties == null || !properties.containsKey("mail.smtp.host")) {
properties = getProperties(new PropertiesProvider(Context.getConfig()));
}
if (!properties.containsKey("mail.smtp.host")) {
LOGGER.warn("No SMTP configuration found");
return;
}
Session session = Session.getInstance(properties);
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()));
message.setSubject(subject);
message.setSentDate(new Date());
if (attachment != null) {
Multipart multipart = new MimeMultipart();
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(body, "text/html; charset=utf-8");
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachment);
message.setContent(multipart);
} else {
message.setContent(body, "text/html; charset=utf-8");
}
try (Transport transport = session.getTransport()) {
Main.getInjector().getInstance(StatisticsManager.class).registerMail();
transport.connect(properties.getProperty("mail.smtp.host"), properties.getProperty("mail.smtp.username"), properties.getProperty("mail.smtp.password"));
transport.sendMessage(message, message.getAllRecipients());
}
}
Aggregations