Search in sources :

Example 1 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)

Example 2 with MimeMessageHelper

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

the class QueryServiceImpl method sendEmail.

private void sendEmail(String to, String subject, String body, Boolean htmlEmail) throws Exception {
    try {
        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) appContext.getBean("mailSender");
        Properties javaMailProperties = mailSender.getJavaMailProperties();
        if (null != javaMailProperties) {
            if (javaMailProperties.get("mail.smtp.localhost") == null || ((String) javaMailProperties.get("mail.smtp.localhost")).equalsIgnoreCase("")) {
                javaMailProperties.put("mail.smtp.localhost", "localhost");
            }
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlEmail);
        helper.setFrom(EmailEngine.getAdminEmail());
        helper.setTo(processMultipleImailAddresses(to.trim()));
        helper.setSubject(subject);
        helper.setText(body, true);
        mailSender.send(mimeMessage);
        logger.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        logger.error("Email could not be sent on {} due to: {}", new Date(), me.getMessage());
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date)

Example 3 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project cas by apereo.

the class CommunicationsManager method email.

/**
 * Email.
 *
 * @param text    the text
 * @param from    the from
 * @param subject the subject
 * @param to      the to
 * @param cc      the cc
 * @param bcc     the bcc
 * @return the boolean
 */
public boolean email(final String text, final String from, final String subject, final String to, final String cc, final String bcc) {
    try {
        if (!isMailSenderDefined() || StringUtils.isBlank(text) || StringUtils.isBlank(from) || StringUtils.isBlank(subject) || StringUtils.isBlank(to)) {
            LOGGER.warn("Could not send email to [{}] because either no address/subject/text is found or email settings are not configured.", to);
            return false;
        }
        final MimeMessage message = this.mailSender.createMimeMessage();
        final MimeMessageHelper helper = new MimeMessageHelper(message);
        helper.setTo(to);
        helper.setText(text);
        helper.setSubject(subject);
        helper.setFrom(from);
        helper.setPriority(1);
        if (StringUtils.isNotBlank(cc)) {
            helper.setCc(cc);
        }
        if (StringUtils.isNotBlank(bcc)) {
            helper.setBcc(bcc);
        }
        this.mailSender.send(message);
        return true;
    } catch (final Exception ex) {
        LOGGER.error(ex.getMessage(), ex);
    }
    return false;
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 4 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project topcom-cloud by 545314690.

the class EmailUtil method send.

public static void send(EmailVO emailVO) throws MessagingException, UnsupportedEncodingException {
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, emailVO.isMultipart(), "utf-8");
    mimeMessageHelper.setTo(emailVO.getTo());
    mimeMessageHelper.setFrom(EMAIL_FROM, PERSONAL);
    mimeMessageHelper.setSubject(emailVO.getSubject());
    if (StringUtils.isNotBlank(emailVO.getContent())) {
        mimeMessageHelper.setText(emailVO.getContent(), emailVO.isHtml());
    }
    String[] cc = emailVO.getCc();
    if (cc != null && cc.length > 0) {
        mimeMessageHelper.setCc(cc);
    }
    // 发送邮件
    mailSender.send(mimeMessage);
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper)

Example 5 with MimeMessageHelper

use of org.springframework.mail.javamail.MimeMessageHelper in project webofneeds by researchstudio-sat.

the class WonMailSender method sendHtmlMessage.

public void sendHtmlMessage(String toEmail, String subject, String htmlBody) {
    MimeMessage msg = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(msg, true);
        helper.setFrom(this.templateMessage.getFrom());
        helper.setSubject(subject);
        helper.setTo(toEmail);
        helper.setText(htmlBody, true);
        mailSender.send(msg);
    } catch (Exception ex) {
        logger.warn(ex.getMessage());
    }
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) MailException(org.springframework.mail.MailException)

Aggregations

MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)111 MimeMessage (javax.mail.internet.MimeMessage)103 MessagingException (javax.mail.MessagingException)47 Async (org.springframework.scheduling.annotation.Async)20 File (java.io.File)16 MailException (org.springframework.mail.MailException)16 FileSystemResource (org.springframework.core.io.FileSystemResource)12 Date (java.util.Date)11 JavaMailSenderImpl (org.springframework.mail.javamail.JavaMailSenderImpl)11 InternetAddress (javax.mail.internet.InternetAddress)10 JavaMailSender (org.springframework.mail.javamail.JavaMailSender)9 MimeMessagePreparator (org.springframework.mail.javamail.MimeMessagePreparator)9 MailSendException (org.springframework.mail.MailSendException)7 IOException (java.io.IOException)6 Template (freemarker.template.Template)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Properties (java.util.Properties)5 DataSource (javax.activation.DataSource)5 Test (org.junit.Test)5