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

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

Introduction

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

Prototype

String TEXT_PLAIN

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

Click Source Link

Usage

From source file:de.hybris.basecommerce.SimpleSmtpServerUtilsTest.java

@Test
public void testSendSuccess() throws EmailException, AddressException {
    final String origMailPortNumber = Config.getParameter(Config.Params.MAIL_SMTP_PORT);
    final String origMailHost = Config.getParameter(Config.Params.MAIL_SMTP_SERVER);

    SimpleSmtpServer server = null;/*from  www . j a v a  2s.c  om*/
    try {
        server = SimpleSmtpServerUtils.startServer(TEST_START_PORT);
        Assert.assertFalse(server.isStopped());
        Assert.assertTrue(server.getPort() > 0);

        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, "localhost");
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, String.valueOf(server.getPort()));

        final Email email = MailUtils.getPreConfiguredEmail();

        email.setFrom("foo.bar@hybris.com");
        email.setTo(Arrays.asList(InternetAddress.parse("foo.bar@hybris.com")));
        email.setSubject("TEST TEST TEST");
        email.setContent("FOO", Email.TEXT_PLAIN);

        email.send();
    } finally {
        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, origMailHost);
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, origMailPortNumber);

        if (server != null) {
            server.stop();
        }
    }
}

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

/**
 * @throws EmailException EmailException
 * @throws MessagingException MessagingException
 *//*  w  w  w.  ja  v a2 s . co  m*/
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 ww  .  ja v a 2 s. co  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
 *//*  ww w .j  av  a 2  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 {//from  w  w  w.j av  a2  s.  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);
    }

}