Search in sources :

Example 1 with PropertiesProvider

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());
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) User(org.traccar.model.User) Properties(java.util.Properties) Date(java.util.Date) PropertiesProvider(org.traccar.notification.PropertiesProvider) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Transport(javax.mail.Transport) Session(javax.mail.Session)

Aggregations

Date (java.util.Date)1 Properties (java.util.Properties)1 BodyPart (javax.mail.BodyPart)1 Multipart (javax.mail.Multipart)1 Session (javax.mail.Session)1 Transport (javax.mail.Transport)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeBodyPart (javax.mail.internet.MimeBodyPart)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMultipart (javax.mail.internet.MimeMultipart)1 User (org.traccar.model.User)1 PropertiesProvider (org.traccar.notification.PropertiesProvider)1