Example usage for org.apache.commons.mail Email TEXT_HTML

List of usage examples for org.apache.commons.mail Email TEXT_HTML

Introduction

In this page you can find the example usage for org.apache.commons.mail Email TEXT_HTML.

Prototype

String TEXT_HTML

To view the source code for org.apache.commons.mail Email TEXT_HTML.

Click Source Link

Usage

From source file:lucee.runtime.net.mail.HtmlEmailImpl.java

/**
 * @throws EmailException EmailException
 * @throws MessagingException MessagingException
 *///from   www  . j ava2s. c  om
private void buildAttachments() throws MessagingException, EmailException {
    MimeMultipart container = this.getContainer();
    MimeMultipart subContainer = null;
    MimeMultipart subContainerHTML = new MimeMultipart("related");
    BodyPart msgHtml = null;
    BodyPart msgText = null;

    container.setSubType("mixed");
    subContainer = new MimeMultipart("alternative");

    if (!StringUtil.isEmpty(this.text)) {
        msgText = new MimeBodyPart();
        subContainer.addBodyPart(msgText);

        if (!StringUtil.isEmpty(this.charset)) {
            msgText.setContent(this.text, Email.TEXT_PLAIN + "; charset=" + this.charset);
        } else {
            msgText.setContent(this.text, Email.TEXT_PLAIN);
        }
    }

    if (!StringUtil.isEmpty(this.html)) {
        if (this.inlineImages.size() > 0) {
            msgHtml = new MimeBodyPart();
            subContainerHTML.addBodyPart(msgHtml);
        } else {
            msgHtml = new MimeBodyPart();
            subContainer.addBodyPart(msgHtml);
        }

        if (!StringUtil.isEmpty(this.charset)) {
            msgHtml.setContent(this.html, Email.TEXT_HTML + "; charset=" + this.charset);
        } else {
            msgHtml.setContent(this.html, Email.TEXT_HTML);
        }

        Iterator iter = this.inlineImages.iterator();
        while (iter.hasNext()) {
            subContainerHTML.addBodyPart((BodyPart) iter.next());
        }
    }

    // add sub containers to message
    this.addPart(subContainer, 0);

    if (this.inlineImages.size() > 0) {
        // add sub container to message
        this.addPart(subContainerHTML, 1);
    }
}

From source file:lucee.runtime.net.mail.HtmlEmailImpl.java

/**
 * @throws EmailException EmailException
 * @throws MessagingException MessagingException
 *//*from   w w  w  .  j a  v a2 s . c  o  m*/
private void buildNoAttachments() throws MessagingException, EmailException {
    MimeMultipart container = this.getContainer();
    MimeMultipart subContainerHTML = new MimeMultipart("related");

    container.setSubType("alternative");

    BodyPart msgText = null;
    BodyPart msgHtml = null;

    if (!StringUtil.isEmpty(this.text)) {
        msgText = this.getPrimaryBodyPart();
        if (!StringUtil.isEmpty(this.charset)) {
            msgText.setContent(this.text, Email.TEXT_PLAIN + "; charset=" + this.charset);
        } else {
            msgText.setContent(this.text, Email.TEXT_PLAIN);
        }
    }

    if (!StringUtil.isEmpty(this.html)) {
        // if the txt part of the message was null, then the html part
        // will become the primary body part
        if (msgText == null) {
            msgHtml = getPrimaryBodyPart();
        } else {
            if (this.inlineImages.size() > 0) {
                msgHtml = new MimeBodyPart();
                subContainerHTML.addBodyPart(msgHtml);
            } else {
                msgHtml = new MimeBodyPart();
                container.addBodyPart(msgHtml, 1);
            }
        }

        if (!StringUtil.isEmpty(this.charset)) {
            msgHtml.setContent(this.html, Email.TEXT_HTML + "; charset=" + this.charset);
        } else {
            msgHtml.setContent(this.html, Email.TEXT_HTML);
        }

        Iterator iter = this.inlineImages.iterator();
        while (iter.hasNext()) {
            subContainerHTML.addBodyPart((BodyPart) iter.next());
        }

        if (this.inlineImages.size() > 0) {
            // add sub container to message
            this.addPart(subContainerHTML);
        }
    }
}

From source file:immf.MyHtmlEmail.java

/**
 * @throws EmailException EmailException
 * @throws MessagingException MessagingException
 *//*from w w  w. j  a v  a2  s  .  c  om*/
private void build() throws MessagingException, EmailException {
    MimeMultipart rootContainer = this.getContainer();
    MimeMultipart bodyEmbedsContainer = rootContainer;
    MimeMultipart bodyContainer = rootContainer;
    BodyPart msgHtml = null;
    BodyPart msgText = null;

    rootContainer.setSubType("mixed");

    // determine how to form multiparts of email

    if (StringUtils.isNotEmpty(this.html) && this.inlineEmbeds.size() > 0) {
        //If HTML body and embeds are used, create a related container and add it to the root container
        bodyEmbedsContainer = new MimeMultipart("related");
        bodyContainer = bodyEmbedsContainer;
        this.addPart(bodyEmbedsContainer, 0);

        //If TEXT body was specified, create a alternative container and add it to the embeds container
        if (StringUtils.isNotEmpty(this.text)) {
            bodyContainer = new MimeMultipart("alternative");
            BodyPart bodyPart = createBodyPart();
            try {
                bodyPart.setContent(bodyContainer);
                bodyEmbedsContainer.addBodyPart(bodyPart, 0);
            } catch (MessagingException me) {
                throw new EmailException(me);
            }
        }
    } else if (StringUtils.isNotEmpty(this.text) && StringUtils.isNotEmpty(this.html)) {
        //If both HTML and TEXT bodies are provided, create a alternative container and add it to the root container
        bodyContainer = new MimeMultipart("alternative");
        this.addPart(bodyContainer, 0);
    }

    if (StringUtils.isNotEmpty(this.html)) {
        msgHtml = new MimeBodyPart();
        bodyContainer.addBodyPart(msgHtml, 0);

        // apply default charset if one has been set
        if (StringUtils.isNotEmpty(this.charset)) {
            msgHtml.setContent(this.html, Email.TEXT_HTML + "; charset=" + this.charset);
        } else {
            msgHtml.setContent(this.html, Email.TEXT_HTML);
        }
        if (contentTransferEncoding != null) {
            msgHtml.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
        }

        Iterator<InlineImage> iter = this.inlineEmbeds.values().iterator();
        while (iter.hasNext()) {
            InlineImage ii = (InlineImage) iter.next();
            bodyEmbedsContainer.addBodyPart(ii.getMbp());
        }
    }

    if (StringUtils.isNotEmpty(this.text)) {
        msgText = new MimeBodyPart();
        bodyContainer.addBodyPart(msgText, 0);

        // apply default charset if one has been set
        if (StringUtils.isNotEmpty(this.charset)) {
            msgText.setContent(this.text, Email.TEXT_PLAIN + "; charset=" + this.charset);
        } else {
            msgText.setContent(this.text, Email.TEXT_PLAIN);
        }
        if (contentTransferEncoding != null) {
            msgText.setHeader("Content-Transfer-Encoding", contentTransferEncoding);
        }
    }
}

From source file:org.viafirma.util.SendMailUtil.java

public MultiPartEmail buildMessage(String subject, String mailTo, String texto, String htmlTexto, String alias,
        String password) throws ExcepcionErrorInterno, ExcepcionCertificadoNoEncontrado {

    try {// w ww  .  j  a  va 2s  .co  m
        // 1.- Preparamos el certificado
        // Recuperamos la clave privada asociada al alias
        PrivateKey privateKey = KeyStoreLoader.getPrivateKey(alias, password);
        if (privateKey == null) {
            throw new ExcepcionCertificadoNoEncontrado(
                    "No existe una clave privada para el alias  '" + alias + "'");
        }
        if (log.isDebugEnabled())
            log.info("Firmando el documento con el certificado " + alias);

        // Recuperamos el camino de confianza asociado al certificado
        List<Certificate> chain = KeyStoreLoader.getCertificateChain(alias);

        // Obtenemos los datos del certificado utilizado.
        X509Certificate certificadoX509 = (X509Certificate) chain.get(0);
        CertificadoGenerico datosCertificado = CertificadoGenericoFactory.getInstance()
                .generar(certificadoX509);
        String emailFrom = datosCertificado.getEmail();
        String emailFromDesc = datosCertificado.getCn();
        if (StringUtils.isEmpty(emailFrom)) {
            log.warn("El certificado indicado no tiene un email asociado, No es vlido para firmar emails"
                    + datosCertificado);
            throw new ExcepcionCertificadoNoEncontrado(
                    "El certificado indicado no tiene un email asociado, No es vlido para firmar emails.");
        }

        CertStore certificadosYcrls = CertStore.getInstance("Collection",
                new CollectionCertStoreParameters(chain), BouncyCastleProvider.PROVIDER_NAME);

        // 2.- Preparamos el mail
        MimeBodyPart bodyPart = new MimeBodyPart();
        MimeMultipart dataMultiPart = new MimeMultipart();

        MimeBodyPart msgHtml = new MimeBodyPart();
        if (StringUtils.isNotEmpty(htmlTexto)) {
            msgHtml.setContent(htmlTexto, Email.TEXT_HTML + "; charset=UTF-8");
        } else {
            msgHtml.setContent("<p>" + htmlTexto + "</p>", Email.TEXT_PLAIN + "; charset=UTF-8");
        }

        // create the message we want signed
        MimeBodyPart mensajeTexto = new MimeBodyPart();
        if (StringUtils.isNotEmpty(texto)) {
            mensajeTexto.setText(texto, "UTF-8");
        } else if (StringUtils.isEmpty(texto)) {
            mensajeTexto.setText(CadenaUtilities.cleanHtml(htmlTexto), "UTF-8");
        }
        dataMultiPart.addBodyPart(mensajeTexto);
        dataMultiPart.addBodyPart(msgHtml);

        bodyPart.setContent(dataMultiPart);

        // Crea el nuevo mensaje firmado
        MimeMultipart multiPart = createMultipartWithSignature(privateKey, certificadoX509, certificadosYcrls,
                bodyPart);

        // Creamos el mensaje que finalmente sera enviadio.
        MultiPartEmail mail = createMultiPartEmail(subject, mailTo, emailFrom, emailFromDesc, multiPart,
                multiPart.getContentType());

        return mail;
    } catch (InvalidAlgorithmParameterException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    } catch (NoSuchAlgorithmException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    } catch (NoSuchProviderException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    } catch (MessagingException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    } catch (CertificateParsingException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    } catch (CertStoreException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    } catch (SMIMEException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    } catch (EmailException e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    }

}