Example usage for org.apache.commons.mail HtmlEmail setHtmlMsg

List of usage examples for org.apache.commons.mail HtmlEmail setHtmlMsg

Introduction

In this page you can find the example usage for org.apache.commons.mail HtmlEmail setHtmlMsg.

Prototype

public HtmlEmail setHtmlMsg(final String aHtml) throws EmailException 

Source Link

Document

Set the HTML content.

Usage

From source file:gribbit.util.SendEmail.java

/** Send an email. Don't forget to use fully-qualified URLs in the message body. */
public static void sendEmail(final String toName, final String to, final String subject,
        final DataModel message, final String messagePlainText) {
    // Queue sending of email in a new thread
    GribbitServer.vertx.executeBlocking(future -> {
        if (GribbitProperties.SMTP_SERVER == null || GribbitProperties.SEND_EMAIL_ADDRESS == null
                || GribbitProperties.SEND_EMAIL_PASSWORD == null || GribbitProperties.SEND_EMAIL_ADDRESS == null
                || GribbitProperties.SEND_EMAIL_NAME == null) {
            throw new RuntimeException("SMTP is not fully configured in the properties file");
        }/*from  ww  w  .j  a  v a  2s .  c om*/

        String fullEmailAddr = "\"" + toName + "\" <" + to + ">";
        try {
            HtmlEmail email = new ImageHtmlEmail();
            email.setDebug(false);

            email.setHostName(GribbitProperties.SMTP_SERVER);
            email.setSmtpPort(GribbitProperties.SMTP_PORT);
            email.setAuthenticator(new DefaultAuthenticator(GribbitProperties.SEND_EMAIL_ADDRESS,
                    GribbitProperties.SEND_EMAIL_PASSWORD));
            email.setStartTLSRequired(true);

            email.addTo(to, toName);
            email.setFrom(GribbitProperties.SEND_EMAIL_ADDRESS, GribbitProperties.SEND_EMAIL_NAME);
            email.setSubject(subject);
            email.setHtmlMsg(message.toString());
            email.setTextMsg(messagePlainText);

            email.send();

            Log.info("Sent email to " + fullEmailAddr + " : " + subject);

        } catch (EmailException e) {
            Log.exception("Failure while trying to send email to " + fullEmailAddr + " : " + subject, e);
        }
        future.complete();

    }, res -> {
        if (res.failed()) {
            Log.error("Exception while trying to send email");
        }
    });
}

From source file:it.vige.greenarea.test.mail.SendMailTest.java

@Test
public void testSendMailToGoogle() throws Exception {
    HtmlEmail email = new HtmlEmail();
    try {/*  w  w w .j a va 2s.  c o m*/
        email.setSubject("prova");
        email.setHtmlMsg("<div>ciao</div>");
        email.addTo("luca.stancapiano@vige.it");
        email.setSmtpPort(587);
        email.setHostName("smtp.gmail.com");
        email.setFrom("greenareavige@gmail.com");
        email.setAuthentication("greenareavige@gmail.com", "vulitgreenarea");
        email.setTLS(true);
        email.send();
    } catch (EmailException e) {
        fail();
    }

}

From source file:com.ning.billing.util.email.DefaultEmailSender.java

@Override
public void sendHTMLEmail(final List<String> to, final List<String> cc, final String subject,
        final String htmlBody) throws EmailApiException {
    final HtmlEmail email = new HtmlEmail();
    try {/* w  w w  . j a  va 2 s .c  o  m*/
        email.setHtmlMsg(htmlBody);
    } catch (EmailException e) {
        throw new EmailApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
    }

    sendEmail(to, cc, subject, email);
}

From source file:com.qatickets.service.MailService.java

public void send(EmailMessage msg) throws Exception {

    HtmlEmail email = new HtmlEmail();

    email.setSmtpPort(port);//from ww w . j a v a  2  s  .  c  om
    email.setHostName(host);

    email.setHtmlMsg(msg.getHTMLContent());
    email.setTextMsg(msg.getTextContent());
    email.setSubject(msg.getSubject());

    email.addTo(msg.getRecepient().getEmail(), msg.getRecepient().getName());
    //      email.setFrom(systemOwner, "QATickets.com");

    email.send();

}

From source file:com.github.robozonky.notifications.EmailHandler.java

@Override
public void send(final SessionInfo sessionInfo, final String subject, final String message,
        final String fallbackMessage) throws Exception {
    final HtmlEmail email = createNewEmail(sessionInfo);
    email.setSubject(subject);/*from   w ww.  j  a  v a 2 s. c o  m*/
    email.setHtmlMsg(message);
    email.setTextMsg(fallbackMessage);
    LOGGER.debug("Will send '{}' from {} to {} through {}:{} as {}.", email.getSubject(),
            email.getFromAddress(), email.getToAddresses(), email.getHostName(), email.getSmtpPort(),
            getSmtpUsername());
    email.send();
}

From source file:com.jaeksoft.searchlib.config.Mailer.java

public void send() throws EmailException {
    if (email instanceof HtmlEmail) {
        HtmlEmail htmlEmail = (HtmlEmail) email;
        if (htmlStringWriter != null)
            htmlEmail.setHtmlMsg(htmlStringWriter.toString());
        if (textStringWriter != null)
            htmlEmail.setTextMsg(textStringWriter.toString());
        else//w  w w .  j a v  a2s.  c  o  m
            htmlEmail.setTextMsg("This message contains an HTML content");
    } else if (email instanceof SimpleEmail) {
        SimpleEmail simpleEmail = (SimpleEmail) email;
        if (textStringWriter != null)
            simpleEmail.setMsg(textStringWriter.toString());
    }
    if (textStringWriter != null)
        email.send();
}

From source file:cl.alma.scrw.bpmn.tasks.MailActivityBehavior.java

protected HtmlEmail createHtmlEmail(String text, String html) {
    HtmlEmail email = new HtmlEmail();
    try {/* w  w w  .  jav a 2 s .com*/
        email.setHtmlMsg(html);
        if (text != null) { // for email clients that don't support html
            email.setTextMsg(text);
        }
        return email;
    } catch (EmailException e) {
        throw new ActivitiException("Could not create HTML email", e);
    }
}

From source file:com.manydesigns.mail.sender.DefaultMailSender.java

protected void send(Email emailBean) throws EmailException {
    logger.debug("Entering send(Email)");
    org.apache.commons.mail.Email email;
    String textBody = emailBean.getTextBody();
    String htmlBody = emailBean.getHtmlBody();
    if (null == htmlBody) {
        if (emailBean.getAttachments().isEmpty()) {
            SimpleEmail simpleEmail = new SimpleEmail();
            simpleEmail.setMsg(textBody);
            email = simpleEmail;/*from w  w  w.j a v  a 2s. com*/
        } else {
            MultiPartEmail multiPartEmail = new MultiPartEmail();
            multiPartEmail.setMsg(textBody);
            for (Attachment attachment : emailBean.getAttachments()) {
                EmailAttachment emailAttachment = new EmailAttachment();
                emailAttachment.setName(attachment.getName());
                emailAttachment.setDisposition(attachment.getDisposition());
                emailAttachment.setDescription(attachment.getDescription());
                emailAttachment.setPath(attachment.getFilePath());
                multiPartEmail.attach(emailAttachment);
            }
            email = multiPartEmail;
        }
    } else {
        HtmlEmail htmlEmail = new HtmlEmail();
        htmlEmail.setHtmlMsg(htmlBody);
        if (textBody != null) {
            htmlEmail.setTextMsg(textBody);
        }
        for (Attachment attachment : emailBean.getAttachments()) {
            if (!attachment.isEmbedded()) {
                EmailAttachment emailAttachment = new EmailAttachment();
                emailAttachment.setName(attachment.getName());
                emailAttachment.setDisposition(attachment.getDisposition());
                emailAttachment.setDescription(attachment.getDescription());
                emailAttachment.setPath(attachment.getFilePath());
                htmlEmail.attach(emailAttachment);
            } else {
                FileDataSource dataSource = new FileDataSource(new File(attachment.getFilePath()));
                htmlEmail.embed(dataSource, attachment.getName(), attachment.getContentId());
            }
        }
        email = htmlEmail;
    }

    if (null != login && null != password) {
        email.setAuthenticator(new DefaultAuthenticator(login, password));
    }
    email.setHostName(server);
    email.setSmtpPort(port);
    email.setSubject(emailBean.getSubject());
    email.setFrom(emailBean.getFrom());
    // hongliangpan add
    if ("smtp.163.com".equals(server)) {
        email.setFrom(login + "@163.com");
        // email.setAuthenticator(new DefaultAuthenticator("test28797575", "1qa2ws3ed"));
    }

    for (Recipient recipient : emailBean.getRecipients()) {
        switch (recipient.getType()) {
        case TO:
            email.addTo(recipient.getAddress());
            break;
        case CC:
            email.addCc(recipient.getAddress());
            break;
        case BCC:
            email.addBcc(recipient.getAddress());
            break;
        }
    }
    email.setSSL(ssl);
    email.setTLS(tls);
    email.setSslSmtpPort(port + "");
    email.setCharset("UTF-8");
    email.send();
    logger.debug("Exiting send(Email)");
}

From source file:actors.ValidationEmailSender.java

@Override
public void onReceive(Object object) {
    if (!(object instanceof Email)) {
        return;/* w  w w .  ja  va2 s  .c o m*/
    }

    Email email = (Email) object;

    final HtmlEmail htmlEmail = new HtmlEmail();

    try {
        htmlEmail.setFrom(Config.getEmailFromSmtp(), utils.Config.getSiteName());
        htmlEmail.addTo(email.email, email.user.name);
        htmlEmail.setSubject(Messages.get("emails.validation.email.title", utils.Config.getSiteName()));
        htmlEmail.setHtmlMsg(getMessage(email.confirmUrl));
        htmlEmail.setCharset("utf-8");
        Mailer.send(htmlEmail);
        String escapedTitle = htmlEmail.getSubject().replace("\"", "\\\"");
        String logEntry = String.format("\"%s\" %s", escapedTitle, htmlEmail.getToAddresses());
        play.Logger.of("mail").info(logEntry);
    } catch (Exception e) {
        Logger.warn("Failed to send a notification: " + email + "\n" + ExceptionUtils.getStackTrace(e));
    }
}

From source file:com.fatecib.projetoemail.servlets.DAO.Enviaremail2.java

private void enviar(Email em, ConfiguracaoSQL conf, String destinatario) throws EmailException {
    try {//w w w  .java  2 s. c o  m
        HtmlEmail email = new HtmlEmail();
        email.setHostName(conf.getEMAILHOST());
        email.setSmtpPort(conf.getPORTASMTP());
        email.setAuthenticator(new DefaultAuthenticator(conf.getUsuario(), conf.getSENHA()));
        email.setSSL(true);
        email.setFrom(conf.getUsuario());
        email.setSubject(em.getTitulo());
        email.setHtmlMsg(em.getConteudo());
        // set the alternative message
        email.setTextMsg("Email enviado com sucesso");
        email.addTo(destinatario);
        email.send();
    } catch (Exception e) {
        throw e;
    }

}