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

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

Introduction

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

Prototype

HtmlEmail

Source Link

Usage

From source file:org.camunda.bpm.engine.impl.bpmn.behavior.MailActivityBehavior.java

protected HtmlEmail createHtmlEmail(String text, String html) {
    HtmlEmail email = new HtmlEmail();
    try {//w  w w.  ja va2  s.c  om
        email.setHtmlMsg(html);
        if (text != null) { // for email clients that don't support html
            email.setTextMsg(text);
        }
        return email;
    } catch (EmailException e) {
        throw new ProcessEngineException("Could not create HTML email", e);
    }
}

From source file:org.cerberus.service.email.impl.sendMail.java

public static void sendHtmlMail(String host, int port, String body, String subject, String from, String to,
        String cc) throws Exception {

    HtmlEmail email = new HtmlEmail();
    email.setSmtpPort(port);/*from ww  w  .ja  va 2 s.  c  o  m*/
    email.setDebug(false);
    email.setHostName(host);
    email.setFrom(from);
    email.setSubject(subject);
    email.setHtmlMsg(body);

    String[] destinataire = to.split(";");

    for (int i = 0; i < destinataire.length; i++) {
        String name;
        String emailaddress;
        if (destinataire[i].contains("<")) {
            String[] destinatairedata = destinataire[i].split("<");
            name = destinatairedata[0].trim();
            emailaddress = destinatairedata[1].replace(">", "").trim();
        } else {
            name = "";
            emailaddress = destinataire[i];
        }
        email.addTo(emailaddress, name);
    }

    String[] copy = cc.split(";");

    for (int i = 0; i < copy.length; i++) {
        String namecc;
        String emailaddresscc;
        if (copy[i].contains("<")) {
            String[] copydata = copy[i].split("<");
            namecc = copydata[0].trim();
            emailaddresscc = copydata[1].replace(">", "").trim();
        } else {
            namecc = "";
            emailaddresscc = copy[i];
        }
        email.addCc(emailaddresscc, namecc);
    }

    email.setTLS(true);

    email.send();

}

From source file:org.chenillekit.mail.services.impl.MailServiceImpl.java

/**
 * send a HTML message./*from   ww  w  .  jav  a  2s.co m*/
 *
 * @param headers    the mail headers
 * @param htmlBody   the mail body (HTML based)
 * @param dataSources array of data sources to attach at this mail
 *
 * @return true if mail successfull send
 */
public boolean sendHtmlMail(MailMessageHeaders headers, String htmlBody, DataSource... dataSources) {
    try {
        HtmlEmail email = new HtmlEmail();

        setEmailStandardData(email);

        setMailMessageHeaders(email, headers);

        if (dataSources != null) {
            for (DataSource dataSource : dataSources)
                email.attach(dataSource, dataSource.getName(), dataSource.getName());
        }

        email.setCharset(headers.getCharset());
        email.setHtmlMsg(htmlBody);

        String msgId = email.send();

        return true;
    } catch (EmailException e) {
        // FIXME Handle gracefully
        throw new RuntimeException(e);
    }
}

From source file:org.cobbzilla.mail.sender.SmtpMailSender.java

private Email constructEmail(SimpleEmailMessage message) throws EmailException {
    final Email email;
    if (message instanceof ICalEvent) {
        final MultiPartEmail multiPartEmail = new MultiPartEmail();

        ICalEvent iCalEvent = (ICalEvent) message;

        // Calendar iCalendar = new Calendar();
        Calendar iCalendar = ICalUtil.newCalendarEvent(iCalEvent.getProdId(), iCalEvent);
        byte[] attachmentData = ICalUtil.toBytes(iCalendar);

        String icsName = iCalEvent.getIcsName() + ".ics";
        String contentType = "text/calendar; icsName=\"" + icsName + "\"";
        try {/*from   w  ww.j  a v  a2s. com*/
            multiPartEmail.attach(new ByteArrayDataSource(attachmentData, contentType), icsName, "",
                    EmailAttachment.ATTACHMENT);
        } catch (IOException e) {
            throw new EmailException("constructEmail: couldn't attach: " + e, e);
        }
        email = multiPartEmail;

    } else if (message.getHasHtmlMessage()) {
        final HtmlEmail htmlEmail = new HtmlEmail();
        htmlEmail.setTextMsg(message.getMessage());
        htmlEmail.setHtmlMsg(message.getHtmlMessage());
        email = htmlEmail;

    } else {
        email = new SimpleEmail();
        email.setMsg(message.getMessage());
    }
    return email;
}

From source file:org.fao.geonet.util.MailUtil.java

/**
 * Send an html mail. Will look on the settings directly to know the
 * remitent/* w w  w .  ja v a  2 s .  co m*/
 *
 * @param toAddress
 * @param subject
 * @param htmlMessage
 * @param settings
 * @throws EmailException
 */
public static Boolean sendHtmlMail(List<String> toAddress, String subject, String htmlMessage,
        SettingManager settings) {
    // Create data information to compose the mail
    HtmlEmail email = new HtmlEmail();
    configureBasics(settings, email);

    email.setSubject(subject);
    try {
        email.setHtmlMsg(htmlMessage);
    } catch (EmailException e1) {
        Log.error("Error setting email HTML content. Subject:" + subject, e1);
        return false;
    }

    // send to all mails extracted from settings
    for (String add : toAddress) {
        try {
            email.addBcc(add);
        } catch (EmailException e) {
            Log.error(LOG_MODULE_NAME, "Error setting email BCC address " + add, e);
            return false;
        }
    }

    return send(email);
}

From source file:org.fao.geonet.util.MailUtil.java

/**
 * Send a plain text mail. Will look on the settings directly to know the
 * remitent//from w w w .  ja va  2  s . co m
 *
 * @param toAddress
 * @param subject
 * @param message
 * @param htmlMessage
 * @param settings
 * @param replyTo
 * @param replyToDesc    @throws EmailException
 */
public static Boolean sendMail(List<String> toAddress, String subject, String message, String htmlMessage,
        SettingManager settings, String replyTo, String replyToDesc) {
    // Create data information to compose the mail
    boolean isHtml = StringUtils.isNotBlank(htmlMessage);
    Email email = isHtml ? new HtmlEmail() : new SimpleEmail();
    configureBasics(settings, email);

    List<InternetAddress> addressColl = new ArrayList<InternetAddress>();
    if (StringUtils.isNotEmpty(replyTo)) {
        try {
            addressColl.add(new InternetAddress(replyTo, replyToDesc));
            email.setReplyTo(addressColl);
        } catch (UnsupportedEncodingException e2) {

            Log.error(LOG_MODULE_NAME,
                    "Error setting email replyTo. Characters not supported in \"" + replyToDesc + "\"", e2);
            return false;
        } catch (EmailException e) {
            Log.error(LOG_MODULE_NAME, "Error setting email replyTo. Invalid email address \"" + replyTo + "\"",
                    e);
            return false;
        }
    }

    email.setSubject(subject);
    try {
        if (StringUtils.isNotBlank(message)) {
            email.setMsg(message);
        }
        if (isHtml) {
            ((HtmlEmail) email).setHtmlMsg(htmlMessage);
        }
    } catch (EmailException e1) {
        Log.error(LOG_MODULE_NAME, "Error setting email message", e1);
        return false;
    }

    // send to all mails extracted from settings
    for (String add : toAddress) {
        try {
            email.addBcc(add);
        } catch (EmailException e) {
            Log.error(LOG_MODULE_NAME, "Error setting email BCC address " + add, e);
        }
    }

    return send(email);
}

From source file:org.fao.geonet.util.MailUtil.java

/**
 * Send an html mail with atachments/*from  w  ww. j  ava 2 s  . c  o  m*/
 *
 * @param toAddress
 * @param from
 * @param subject
 * @param htmlMessage
 * @param attachment
 * @throws EmailException
 */
public static Boolean sendHtmlMailWithAttachment(List<String> toAddress, String from, String subject,
        String htmlMessage, List<EmailAttachment> attachment, SettingManager settings) {
    // Create data information to compose the mail
    HtmlEmail email = new HtmlEmail();
    String username = settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_USERNAME);
    String password = settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_PASSWORD);
    Boolean ssl = settings.getValueAsBool(Settings.SYSTEM_FEEDBACK_MAILSERVER_SSL, false);
    Boolean tls = settings.getValueAsBool(Settings.SYSTEM_FEEDBACK_MAILSERVER_TLS, false);

    String hostName = settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_HOST);
    Integer smtpPort = Integer.valueOf(settings.getValue(Settings.SYSTEM_FEEDBACK_MAILSERVER_PORT));
    Boolean ignoreSslCertificateErrors = settings
            .getValueAsBool(Settings.SYSTEM_FEEDBACK_MAILSERVER_IGNORE_SSL_CERTIFICATE_ERRORS, false);

    configureBasics(hostName, smtpPort, from, username, password, email, ssl, tls, ignoreSslCertificateErrors);

    for (EmailAttachment attach : attachment) {
        try {
            email.attach(attach);
        } catch (EmailException e) {
            Log.error(LOG_MODULE_NAME, "Error attaching attachment " + attach.getName(), e);
        }
    }

    email.setSubject(subject);
    try {
        email.setHtmlMsg(htmlMessage);
    } catch (EmailException e1) {
        Log.error(LOG_MODULE_NAME, "Error setting email HTML message", e1);
        return false;
    }

    // send to all mails extracted from settings
    for (String add : toAddress) {
        try {
            email.addBcc(add);
        } catch (EmailException e) {
            Log.error(LOG_MODULE_NAME, "Error setting email BCC address " + add, e);
            return false;
        }
    }

    return send(email);
}

From source file:org.fao.geonet.util.MailUtil.java

public static void testSendMail(List<String> toAddress, String subject, String message, String htmlMessage,
        SettingManager settings, String replyTo, String replyToDesc) throws Exception {
    // Create data information to compose the mail
    boolean isHtml = StringUtils.isNotBlank(htmlMessage);
    Email email = isHtml ? new HtmlEmail() : new SimpleEmail();
    configureBasics(settings, email);//from  w  ww.j a va2s.co  m

    List<InternetAddress> addressColl = new ArrayList<InternetAddress>();

    addressColl.add(new InternetAddress(replyTo, replyToDesc));
    email.setReplyTo(addressColl);
    email.setSubject(subject);

    if (StringUtils.isNotBlank(message)) {
        email.setMsg(message);
    }
    if (isHtml) {
        ((HtmlEmail) email).setHtmlMsg(htmlMessage);
    }

    // send to all mails extracted from settings
    for (String add : toAddress) {
        email.addBcc(add);
    }

    email.send();
}

From source file:org.flowable.engine.impl.bpmn.behavior.MailActivityBehavior.java

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

From source file:org.gravidence.gravifon.email.ApacheCommonsEmailSender.java

@Override
public boolean send(String toAddress, String toName, String subject, String htmlMessage, String textMessage) {
    HtmlEmail email = new HtmlEmail();

    email.setHostName(host);//from   w  w w .  j a  va2 s .c o  m
    email.setSslSmtpPort(port);
    email.setAuthenticator(new DefaultAuthenticator(username, password));
    email.setSSLOnConnect(true);

    try {
        if (StringUtils.isBlank(toName)) {
            email.addTo(toAddress);
        } else {
            email.addTo(toAddress, toName);
        }
        email.setFrom(fromAddress, fromName);

        email.setSubject(subject);

        if (htmlMessage != null) {
            email.setHtmlMsg(htmlMessage);
        }
        if (textMessage != null) {
            email.setTextMsg(textMessage);
        }

        email.send();
    } catch (EmailException ex) {
        // TODO think about throwing GravifonException
        LOGGER.warn(String.format("Failed to send an email to %s", toAddress), ex);

        return false;
    }

    return true;
}