Example usage for org.springframework.mail MailPreparationException MailPreparationException

List of usage examples for org.springframework.mail MailPreparationException MailPreparationException

Introduction

In this page you can find the example usage for org.springframework.mail MailPreparationException MailPreparationException.

Prototype

public MailPreparationException(String msg, Throwable cause) 

Source Link

Document

Constructor for MailPreparationException.

Usage

From source file:org.brushingbits.jnap.email.EmailFactory.java

/**
 * /*from  ww  w.ja  v a  2 s  .  co m*/
 * @param <E>
 * @param id
 * @param type
 * @return
 */
public <E extends Email> E getEmail(String id, Class<E> type) {
    E email = null;
    final String emailId = getIdPrefix() + id;
    try {
        email = applicationContext.getBean(emailId, type);
    } catch (NoSuchBeanDefinitionException e) {
        throw new MailPreparationException(
                MessageFormat.format("The e-mail with id '{0}' could not be found!", emailId), e);
    }
    return email;
}

From source file:org.brushingbits.jnap.email.FreemarkerEmail.java

/**
 * Freemarker implementation of {@link TemplateEmail#processTemplate()}
 *//*  w ww  . ja  v  a  2 s .  c  om*/
@Override
protected void processTemplate() {
    try {
        // Email content
        StringBuilder htmlMsg = new StringBuilder();

        // Header
        if (StringUtils.isNotBlank(getHeaderTemplateName())) {
            Template header = getTemplate(getHeaderTemplateName());
            htmlMsg.append(FreeMarkerTemplateUtils.processTemplateIntoString(header, values));
        }

        // Body
        Template body = getTemplate(getBodyTemplateName());
        String bodyContent = FreeMarkerTemplateUtils.processTemplateIntoString(body, values);
        htmlMsg.append(bodyContent);
        if (isMixedContent()) {
            setText(extractTextFromHtml(bodyContent));
        }

        // Footer
        if (StringUtils.isNotBlank(getFooterTemplateName())) {
            Template footer = getTemplate(getFooterTemplateName());
            htmlMsg.append(FreeMarkerTemplateUtils.processTemplateIntoString(footer, values));
        }

        // Setting the content
        if (isMixedContent()) {
            setHtmlText(htmlMsg.toString());
        } else {
            setText(htmlMsg.toString());
        }
    } catch (IOException e) {
        throw new MailPreparationException("Error trying to open template file.", e);
    } catch (TemplateException e) {
        throw new MailPreparationException("Error loading/processing template file.", e);
    }
}

From source file:org.jrecruiter.service.notification.impl.NotificationServiceActivator.java

/** {@inheritDoc} */
public void sendEmail(final EmailRequest request) {

    final MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws MessagingException, IOException {

            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            message.setFrom("no_reply@jrecruiter.org");
            message.setTo(request.getEmail());
            message.setSubject(request.getSubject());

            final Locale locale = LocaleContextHolder.getLocale();

            final Template textTemplate = freemarkerConfiguration
                    .getTemplate(request.getTemplatePrefix() + "-text.ftl", locale);

            final StringWriter textWriter = new StringWriter();
            try {
                textTemplate.process(request.getContext(), textWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate email body.", e);
            }//  w w  w. j a  v a  2s.c o  m

            final Template htmlTemplate = freemarkerConfiguration
                    .getTemplate(request.getTemplatePrefix() + "-html.ftl", locale);

            final StringWriter htmlWriter = new StringWriter();
            try {
                htmlTemplate.process(request.getContext(), htmlWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate email body.", e);
            }

            message.setText(textWriter.toString(), htmlWriter.toString());

        }
    };

    mailSender.send(preparator);
}

From source file:ch.wisv.areafiftylan.utils.mail.MailServiceImpl.java

@Override
public void sendMail(String recipientEmail, String recipientName, String subject, String messageString) {

    // Prepare message using a Spring helper
    final MimeMessage mimeMessage = this.mailSender.createMimeMessage();
    final MimeMessageHelper message;

    try {//from   w  w  w  .ja  v a 2s. c o m
        message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        message.setSubject("[Area FiftyLAN] " + subject);
        message.setFrom(sender);
        message.setTo(recipientEmail);

        // Create the HTML body using Thymeleaf
        String htmlContent = prepareHtmlContent(recipientName, messageString);
        message.setText(htmlContent, true); // true = isHtml

        // Send mail
        this.mailSender.send(mimeMessage);
    } catch (MessagingException e) {
        throw new MailPreparationException("Unable to prepare email", e.getCause());
    } catch (MailException m) {
        throw new MailSendException("Unable to send email", m.getCause());
    }

}

From source file:org.brushingbits.jnap.email.FreemarkerEmail.java

protected Template getTemplate(String name) {
    try {/* w w w .j av  a 2  s .  co  m*/
        return freemarkerConfiguration.getTemplate(name, getLocale());
    } catch (IOException e) {
        throw new MailPreparationException("Error loading email template: " + name, e);
    }
}

From source file:org.jrecruiter.service.notification.impl.DefaultNotificationServiceImpl.java

/** {@inheritDoc} */
@Override/*from  w ww.  jav a2s  .c o m*/
public void sendEmail(final String email, final String subject, final Map context, final String templateName) {

    final MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws MessagingException, IOException {

            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8");
            message.setFrom("no_reply@jrecruiter.org");
            message.setTo(email);
            message.setSubject(subject);

            final Locale locale = LocaleContextHolder.getLocale();

            final Template textTemplate = freemarkerConfiguration.getTemplate(templateName + "-text.ftl",
                    locale);

            final StringWriter textWriter = new StringWriter();
            try {
                textTemplate.process(context, textWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate email body.", e);
            }

            final Template htmlTemplate = freemarkerConfiguration.getTemplate(templateName + "-html.ftl",
                    locale);

            final StringWriter htmlWriter = new StringWriter();
            try {
                htmlTemplate.process(context, htmlWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate email body.", e);
            }

            message.setText(textWriter.toString(), htmlWriter.toString());

        }
    };

    mailSender.send(preparator);
}

From source file:com.autentia.wuija.mail.MailService.java

/**
 * send an e-mail/*from   ww w.j a v  a  2s.  c  o m*/
 * 
 * @param to recipient e-mail
 * @param subject the subject of the e-mail
 * @param text the body of the e-mail
 * @param attachments an array of it
 * @throws EmailException if the e-mail cannot be prepare or send.
 */
public void send(String to, String subject, String text, File... attachments) {
    Assert.hasLength(to, "email 'to' needed");
    Assert.hasLength(subject, "email 'subject' needed");
    Assert.hasLength(text, "email 'text' needed");

    if (log.isDebugEnabled()) {
        final boolean usingPassword = StringUtils.isNotBlank(mailSender.getPassword());
        log.debug("Sending email to: '" + to + "' [through host: '" + mailSender.getHost() + ":"
                + mailSender.getPort() + "', username: '" + mailSender.getUsername() + "' usingPassword:"
                + usingPassword + "].");
        log.debug("isActive: " + active);
    }
    if (!active) {
        return;
    }

    final MimeMessage message = mailSender.createMimeMessage();

    try {
        // use the true flag to indicate you need a multipart message
        final MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setFrom(getFrom());
        helper.setText(text);

        if (attachments != null) {
            for (int i = 0; i < attachments.length; i++) {
                // let's attach each file
                final FileSystemResource file = new FileSystemResource(attachments[i]);
                helper.addAttachment(attachments[i].getName(), file);
                if (log.isDebugEnabled()) {
                    log.debug("File '" + file + "' attached.");
                }
            }
        }

    } catch (MessagingException e) {
        final String msg = "Cannot prepare email message : " + subject + ", to: " + to;
        log.error(msg, e);
        throw new MailPreparationException(msg, e);
    }
    this.mailSender.send(message);
}

From source file:org.topazproject.ambra.email.impl.FreemarkerTemplateMailer.java

private BodyPart createBodyPart(final String mimeType, final String htmlTemplateFilename,
        final Map<String, Object> context) throws IOException, MessagingException {
    final BodyPart htmlPage = new MimeBodyPart();
    final Template htmlTemplate = configuration.getTemplate(htmlTemplateFilename);
    final String encoding = configuration.getDefaultEncoding();
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(100);
    final Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream, encoding));
    htmlTemplate.setOutputEncoding(encoding);
    htmlTemplate.setEncoding(encoding);//from   w w w .  ja va  2  s  . c o  m
    try {
        htmlTemplate.process(context, writer);
    } catch (TemplateException e) {
        throw new MailPreparationException(
                "Can't generate " + mailContentTypes.get(mimeType) + " subscription mail", e);
    }

    htmlPage.setDataHandler(new BodyPartDataHandler(outputStream, mimeType));
    return htmlPage;
}

From source file:cz.zcu.kiv.eegdatabase.data.service.SpringJavaMailService.java

protected MimeMessage createMimeMessage(String from, String to, String subject, String emailBody)
        throws MailException {
    try {//from  w w w . j  a v a 2s  .co m
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
        message.setFrom(from);
        // message.setContent("text/html");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(emailBody, true);
        log.debug("Message " + message);
        return mimeMessage;
    } catch (MessagingException e) {// rethrow as MailException
        throw new MailPreparationException(e.getMessage(), e);
    }
}

From source file:net.naijatek.myalumni.util.mail.FreeMarkerTemplateMailerImpl.java

public void mail(final String email, final Map map, final String bodyTemplatePrefix,
        final String subjectTemplatePrefix) {
    MimeMessagePreparator preparator = new MimeMessagePreparator() {
        public void prepare(MimeMessage mimeMessage) throws MessagingException, IOException {
            mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));

            ///*  w w w .  j a v  a 2 s  .  c o m*/
            // Get the subject
            //
            //BodyPart subjectPart = new MimeBodyPart();
            Template subjectTextTemplate = configuration.getTemplate(subjectTemplatePrefix);
            final StringWriter subjectTextWriter = new StringWriter();

            try {
                subjectTextTemplate.process(map, subjectTextWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate Subject Text", e);
            }
            mimeMessage.setSubject(subjectTextWriter.toString());

            //
            // Create a "text" Multipart message
            //

            Template bodyTextTemplate = configuration.getTemplate(bodyTemplatePrefix);
            final StringWriter bodyTextWriter = new StringWriter();

            try {
                bodyTextTemplate.process(map, bodyTextWriter);
            } catch (TemplateException e) {
                throw new MailPreparationException("Can't generate Body Text", e);
            }
            mimeMessage.setText(bodyTextWriter.toString());

            /*                // @TODO - This part handles sending an attachement
                            textPart.setDataHandler(new DataHandler(new DataSource() {
            public InputStream getInputStream() throws IOException {
                return new StringBufferInputStream(bodyTextWriter.toString());
            }
            public OutputStream getOutputStream() throws IOException {
                throw new IOException("Read-only data");
            }
            public String getContentType() {
                return "text/plain";
            }
            public String getName() {
                return "main";
            }
                            }));
                            mp.addBodyPart(textPart);*/

            /*                // Create a "HTML" Multipart message
                            Multipart htmlContent = new MimeMultipart("related");
                            BodyPart htmlPage = new MimeBodyPart();
                            Template htmlTemplate = configuration.getTemplate(templatePrefix + "-html.ftl");
                            final StringWriter htmlWriter = new StringWriter();
                            try {
            htmlTemplate.process(map, htmlWriter);
                            } catch (TemplateException e) {
            throw new MailPreparationException("Can't generate HTML subscription mail", e);
                            }
                            htmlPage.setDataHandler(new DataHandler(new DataSource() {
            public InputStream getInputStream() throws IOException {
                return new StringBufferInputStream(htmlWriter.toString());
            }
            public OutputStream getOutputStream() throws IOException {
                throw new IOException("Read-only data");
            }
            public String getContentType() {
                return "text/html";
            }
            public String getName() {
                return "main";
            }
                            }));
                            htmlContent.addBodyPart(htmlPage);
                            BodyPart htmlPart = new MimeBodyPart();
                            htmlPart.setContent(htmlContent);
                            mp.addBodyPart(htmlPart);
                    
                            mimeMessage.setContent(mp);*/
        }
    };
    mailSender.send(preparator);
}