Search in sources :

Example 36 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project thingsboard by thingsboard.

the class MailPlugin method sendMail.

private void sendMail(SendMailActionMsg msg) throws MessagingException {
    log.debug("Sending mail {}", msg);
    MimeMessage mailMsg = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mailMsg, "UTF-8");
    helper.setFrom(msg.getFrom());
    helper.setTo(msg.getTo().split("\\s*,\\s*"));
    if (!StringUtils.isEmpty(msg.getCc())) {
        helper.setCc(msg.getCc().split("\\s*,\\s*"));
    }
    if (!StringUtils.isEmpty(msg.getBcc())) {
        helper.setBcc(msg.getBcc().split("\\s*,\\s*"));
    }
    helper.setSubject(msg.getSubject());
    helper.setText(msg.getBody());
    mailSender.send(helper.getMimeMessage());
    log.debug("Mail sent {}", msg);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 37 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project netxms by netxms.

the class SmtpSenderImpl method mail.

@Override
public void mail(String to, String subject, String body, String fileName, File file) {
    try {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        if (fileName != null && file != null) {
            helper.addAttachment(fileName, file);
        }
        helper.setText(body);
        mailSender.send(message);
    } catch (MessagingException e) {
        log.error("Unable to send notification mail", e);
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 38 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project commons by craftercms.

the class EmailFactoryImpl method createMessage.

protected MimeMessage createMessage(String from, String[] to, String[] cc, String[] bcc, String replyTo, String subject, String body, boolean html, File... attachments) throws EmailException {
    boolean addAttachments = ArrayUtils.isNotEmpty(attachments);
    MimeMessageHelper messageHelper;
    try {
        if (addAttachments) {
            messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true);
        } else {
            messageHelper = new MimeMessageHelper(mailSender.createMimeMessage());
        }
        messageHelper.setFrom(from);
        if (to != null) {
            messageHelper.setTo(to);
        }
        if (cc != null) {
            messageHelper.setCc(cc);
        }
        if (bcc != null) {
            messageHelper.setBcc(bcc);
        }
        if (replyTo != null) {
            messageHelper.setReplyTo(replyTo);
        }
        messageHelper.setSubject(subject);
        messageHelper.setText(body, html);
        if (addAttachments) {
            for (File attachment : attachments) {
                messageHelper.addAttachment(attachment.getName(), attachment);
            }
        }
    } catch (AddressException e) {
        throw new EmailAddressException(e);
    } catch (MessagingException e) {
        throw new EmailPreparationException(e);
    }
    logger.debug(LOG_KEY_MIME_MSG_CREATED, from, StringUtils.join(to, ','), StringUtils.join(cc, ','), StringUtils.join(bcc, ','), subject, body);
    return messageHelper.getMimeMessage();
}
Also used : EmailPreparationException(org.craftercms.commons.mail.EmailPreparationException) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) EmailAddressException(org.craftercms.commons.mail.EmailAddressException) EmailAddressException(org.craftercms.commons.mail.EmailAddressException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) File(java.io.File)

Example 39 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project spring-boot-admin by codecentric.

the class MailNotifier method doNotify.

@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
    return Mono.fromRunnable(() -> {
        Context ctx = new Context();
        ctx.setVariables(additionalProperties);
        ctx.setVariable("baseUrl", this.baseUrl);
        ctx.setVariable("event", event);
        ctx.setVariable("instance", instance);
        ctx.setVariable("lastStatus", getLastStatus(event.getInstance()));
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage, StandardCharsets.UTF_8.name());
            message.setText(getBody(ctx).replaceAll("\\s+\\n", "\n"), true);
            message.setSubject(getSubject(ctx));
            message.setTo(this.to);
            message.setCc(this.cc);
            message.setFrom(this.from);
            mailSender.send(mimeMessage);
        } catch (MessagingException ex) {
            throw new RuntimeException("Error sending mail notification", ex);
        }
    });
}
Also used : Context(org.thymeleaf.context.Context) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 40 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project Settler by EmhyrVarEmreis.

the class EmailService method sendEmail.

@Async
public void sendEmail(List<String> to, EmailTemplate emailTemplate, Map<String, String> options) {
    EmailTemplateTmp emailTemplateTmp = EmailTemplateTmp.from(emailTemplate);
    for (Map.Entry<String, String> optionsEntry : options.entrySet()) {
        emailTemplateTmp.subject = emailTemplateTmp.subject.replaceAll("\\$" + optionsEntry.getKey(), optionsEntry.getValue());
        emailTemplateTmp.content = emailTemplateTmp.content.replaceAll("\\$" + optionsEntry.getKey(), optionsEntry.getValue());
    }
    MimeMessagePreparator preparator = mimeMessage -> {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setTo(to.toArray(new String[to.size()]));
        message.setFrom(settlerProperties.getMail().getFromAddress());
        message.setSubject(emailTemplateTmp.subject);
        message.setText(emailTemplateTmp.content, true);
    };
    try {
        mailSender.send(preparator);
    } catch (Exception e) {
        log.error("Error occurred during sendEmail", e);
    }
}
Also used : Async(org.springframework.scheduling.annotation.Async) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) EmailTemplateRepository(pl.morecraft.dev.settler.dao.repository.EmailTemplateRepository) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) JavaMailSender(org.springframework.mail.javamail.JavaMailSender) HashMap(java.util.HashMap) List(java.util.List) Language(pl.morecraft.dev.settler.domain.dictionaries.Language) Stream(java.util.stream.Stream) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) Transaction(pl.morecraft.dev.settler.domain.Transaction) Service(org.springframework.stereotype.Service) EmailTemplate(pl.morecraft.dev.settler.domain.EmailTemplate) Map(java.util.Map) EmailTemplateType(pl.morecraft.dev.settler.domain.dictionaries.EmailTemplateType) Collections(java.util.Collections) SettlerProperties(pl.morecraft.dev.settler.configuration.SettlerProperties) MimeMessagePreparator(org.springframework.mail.javamail.MimeMessagePreparator) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) Async(org.springframework.scheduling.annotation.Async)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)125 MimeMessage (javax.mail.internet.MimeMessage)112 MessagingException (javax.mail.MessagingException)51 Async (org.springframework.scheduling.annotation.Async)20 MailException (org.springframework.mail.MailException)19 File (java.io.File)17 InternetAddress (javax.mail.internet.InternetAddress)12 FileSystemResource (org.springframework.core.io.FileSystemResource)12 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)12 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)12 Date (java.util.Date)11 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)11 HashMap (java.util.HashMap)8 IOException (java.io.IOException)7 Map (java.util.Map)7 MailSendException (org.springframework.mail.MailSendException)7 Template (freemarker.template.Template)6 List (java.util.List)6 Locale (java.util.Locale)6 Properties (java.util.Properties)6