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.ms123.common.workflow.tasks.TaskMailExecutor.java

protected HtmlEmail createHtmlEmail(DelegateExecution execution, String text, String html,
        String attachmentPath) {//from  ww w .j  ava2  s.  c  om
    HtmlEmail email = new HtmlEmail();
    try {
        if (html != null) {
            email.setHtmlMsg(html);
        }
        if (text != null) { // for email clients that don't support html
            email.setTextMsg(text);
        }
        if (attachmentPath != null) {
            email.attach(
                    createAttachment(new File(getProcessDocBasedir(execution), attachmentPath).toString()));
        }
        return email;
    } catch (EmailException e) {
        throw new RuntimeException("TaskMailExecutor:Could not create HTML email", e);
    }
}

From source file:org.ms123.common.workflow.TaskSendExecutor.java

protected HtmlEmail createHtmlEmail(DelegateExecution execution, String text, String html,
        String attachmentPath) {/*from   w w w  .j a va2 s. c o m*/
    HtmlEmail email = new HtmlEmail();
    try {
        if (html != null) {
            email.setHtmlMsg(html);
        }
        if (text != null) { // for email clients that don't support html
            email.setTextMsg(text);
        }
        if (attachmentPath != null) {
            email.attach(
                    createAttachment(new File(getProcessDocBasedir(execution), attachmentPath).toString()));
        }
        return email;
    } catch (EmailException e) {
        throw new RuntimeException("TaskSendExecutor:Could not create HTML email", e);
    }
}

From source file:org.mycontroller.standalone.email.EmailUtils.java

private static void initializeEmail(EmailSettings emailSettings) throws EmailException {
    _logger.info("{}", emailSettings);
    email = new HtmlEmail();
    email.setHostName(emailSettings.getSmtpHost());
    email.setSmtpPort(emailSettings.getSmtpPort());
    if (emailSettings.getSmtpUsername() != null && emailSettings.getSmtpUsername().length() > 0) {
        email.setAuthenticator(//www  .  j  a v  a 2s  .  com
                new DefaultAuthenticator(emailSettings.getSmtpUsername(), emailSettings.getSmtpPassword()));
    }
    if (emailSettings.getEnableSsl()) {
        if (emailSettings.getUseStartTLS()) {
            email.setStartTLSEnabled(emailSettings.getEnableSsl());
        } else {
            email.setSSLOnConnect(emailSettings.getEnableSsl());
        }
    }
    email.setFrom(emailSettings.getFromAddress());
}

From source file:org.ng200.openolympus.services.EmailService.java

@PreAuthorize(SecurityExpressionConstants.IS_ADMIN)
public void sendEmail(String emailAddress, String subject, String view, String alternativeText,
        Map<String, Object> variables) throws MessagingException, EmailException {
    final Context ctx = new Context();
    ctx.setVariables(variables);/*from   w w  w .j  a  v  a  2s.c om*/

    final HtmlEmail email = new HtmlEmail();
    email.setHostName(this.emailHost);
    email.setSmtpPort(this.emailHostPort);
    email.setAuthenticator(new DefaultAuthenticator(this.emailLogin, this.emailPassword));
    email.setSSL(true);
    email.setFrom(this.emailLogin);
    email.setSubject(subject);

    final String htmlContent = this.templateEngine.process(view, ctx);

    email.setHtmlMsg(htmlContent);

    email.setTextMsg(alternativeText);

    email.addTo(emailAddress);
    email.send();

}

From source file:org.onehippo.forge.resetpassword.services.mail.MailServiceImpl.java

@Override
public void sendMail(final MailMessage mailMessage) throws EmailException {

    final HtmlEmail email = new HtmlEmail();

    final Session session = getSession();
    if (session == null) {
        throw new EmailException("Unable to send mail; no mail session available");
    }/*from w  w  w  .  java  2s.  co  m*/

    email.setMailSession(session);
    email.addTo(mailMessage.getToMail(), mailMessage.getToName());
    email.setFrom(mailMessage.getFromMail(), mailMessage.getFromName());
    email.setSubject(mailMessage.getSubject());

    // set the html message
    email.setHtmlMsg(mailMessage.getHtmlTextBody());

    // set the alternative message
    email.setTextMsg(mailMessage.getPlainTextBody());

    // send the email
    email.send();
}

From source file:org.opencron.server.service.NoticeService.java

public void sendMessage(List<User> users, Long workId, String emailAddress, String mobiles, String content) {
    Log log = new Log();
    log.setIsread(false);//from  w  w w .  j av  a 2s.  c o m
    log.setAgentId(workId);
    log.setMessage(content);
    //???
    if (CommonUtils.isEmpty(emailAddress, mobiles)) {
        log.setType(Opencron.MsgType.WEBSITE.getValue());
        log.setSendTime(new Date());
        homeService.saveLog(log);
        return;
    }

    /**
     * ????
     */
    boolean emailSuccess = false;
    boolean mobileSuccess = false;

    Config config = configService.getSysConfig();
    try {
        log.setType(Opencron.MsgType.EMAIL.getValue());
        HtmlEmail email = new HtmlEmail();
        email.setCharset("UTF-8");
        email.setHostName(config.getSmtpHost());
        email.setSslSmtpPort(config.getSmtpPort().toString());
        email.setAuthentication(config.getSenderEmail(), config.getPassword());
        email.setFrom(config.getSenderEmail());
        email.setSubject("opencron");
        email.setHtmlMsg(msgToHtml(content));
        email.addTo(emailAddress.split(","));
        email.send();
        emailSuccess = true;
        /**
         * ??
         */
        log.setReceiver(emailAddress);
        log.setSendTime(new Date());
        homeService.saveLog(log);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    /**
     * ????
     */
    try {
        for (String mobile : mobiles.split(",")) {
            //??POST
            String sendUrl = String.format(config.getSendUrl(), mobile,
                    String.format(config.getTemplate(), content));
            String url = sendUrl.substring(0, sendUrl.indexOf("?"));
            String postData = sendUrl.substring(sendUrl.indexOf("?") + 1);
            String message = HttpUtils.doPost(url, postData, "UTF-8");
            log.setResult(message);
            logger.info(message);
            mobileSuccess = true;
        }
        log.setReceiver(mobiles);
        log.setType(Opencron.MsgType.SMS.getValue());
        log.setSendTime(new Date());
        homeService.saveLog(log);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    /**
     * ??,??
     */
    if (!mobileSuccess && !emailSuccess) {
        log.setType(Opencron.MsgType.WEBSITE.getValue());
        log.setSendTime(new Date());
        for (User user : users) {
            //??
            log.setUserId(user.getUserId());
            log.setReceiver(user.getUserName());
            homeService.saveLog(log);
        }
    }

}

From source file:org.openhab.binding.mail.internal.MailBuilder.java

/**
 * Build the Mail/*from  w  ww .j a  va  2s .  c o  m*/
 *
 * @return instance of Email
 * @throws EmailException if something goes wrong
 */
public Email build() throws EmailException {
    Email mail;

    if (attachmentURLs.isEmpty() && attachmentFiles.isEmpty() && html.isEmpty()) {
        // text mail without attachments
        mail = new SimpleEmail();
        if (!text.isEmpty()) {
            mail.setMsg(text);
        }
    } else if (html.isEmpty()) {
        // text mail with attachments
        MultiPartEmail multipartMail = new MultiPartEmail();
        if (!text.isEmpty()) {
            multipartMail.setMsg(text);
        }
        for (File file : attachmentFiles) {
            multipartMail.attach(file);
        }
        for (URL url : attachmentURLs) {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setURL(url);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            multipartMail.attach(attachment);
        }
        mail = multipartMail;
    } else {
        // html email
        HtmlEmail htmlMail = new HtmlEmail();
        if (!text.isEmpty()) {
            // alternate text supplied
            htmlMail.setTextMsg(text);
            htmlMail.setHtmlMsg(html);
        } else {
            htmlMail.setMsg(html);
        }
        for (File file : attachmentFiles) {
            htmlMail.attach(new FileDataSource(file), "", "");
        }
        for (URL url : attachmentURLs) {
            EmailAttachment attachment = new EmailAttachment();
            attachment.setURL(url);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            htmlMail.attach(attachment);
        }
        mail = htmlMail;
    }

    mail.setTo(recipients);
    mail.setSubject(subject);

    if (!sender.isEmpty()) {
        mail.setFrom(sender);
    }

    return mail;
}

From source file:org.oscarehr.oscar_apps.util.Log4JGmailExecutorTask.java

private void sendEmail() throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(smtpServer);/*from w ww  .  j  a  v  a  2 s  . com*/
    if (smtpUser != null && smtpPassword != null)
        email.setAuthentication(smtpUser, smtpPassword);

    if (smtpSslPort != null) {
        email.setSSL(true);
        email.setSslSmtpPort(smtpSslPort);
    }

    Session session = email.getMailSession();
    Properties properties = session.getProperties();
    properties.setProperty("mail.smtp.connectiontimeout", "20000");
    properties.setProperty("mail.smtp.timeout", "20000");

    email.addTo(recipientEmailAddress, recipientEmailAddress);
    email.setFrom(smtpUser, smtpUser);

    email.setSubject(subject);
    email.setHtmlMsg(contents);
    email.setTextMsg(contents);
    email.send();
}

From source file:org.oscarehr.util.EmailUtils.java

/**
 * This method will return an HtmlEmail object populated with 
 * the values passed in, ignoring the parameters in the configuration file.
 *//*www. java2  s  .  c  o m*/
public static HtmlEmail getHtmlEmail(String smtpServer, String smtpPort, String smtpUser, String smtpPassword,
        String connectionSecurity) throws EmailException {
    logger.debug("smtpServer=" + smtpServer + ", smtpSslPort=" + smtpPort + ", smtpUser=" + smtpUser
            + ", smtpPassword=" + smtpPassword + ",connectionSecurity=" + connectionSecurity);

    HtmlEmail email = null;

    if (RECIPIENT_OVERRIDE_KEY != null || printInsteadOfSend)
        email = new HtmlEmailWrapper();
    else
        email = new HtmlEmail();

    email.setHostName(smtpServer);

    if (smtpUser != null && smtpPassword != null)
        email.setAuthentication(smtpUser, smtpPassword);

    Session session = email.getMailSession();

    if (connectionSecurity != null) {
        if (connectionSecurity.equals(CONNECTION_SECURITY_STARTTLS)) {
            session.getProperties().setProperty(Email.MAIL_TRANSPORT_TLS, "true");
            email.setTLS(true);
        } else if (connectionSecurity.equals(CONNECTION_SECURITY_SSL)) {
            email.setSSL(true);
        }
    }

    if (smtpPort != null) {
        email.setSslSmtpPort(smtpPort);
    }

    Properties properties = session.getProperties();
    properties.setProperty("mail.smtp.connectiontimeout", "20000");
    properties.setProperty("mail.smtp.timeout", "20000");

    return (email);
}

From source file:org.pepstock.jem.notify.engine.EmailNotifier.java

/**
 * This method makes the Email notification. <br>
 * It checks if the email to send is not <code>null</code> and if the email
 * has destination addresses. <br>
 * Depending on the email format creates or a {@link HtmlEmail} (for
 * {@link EmailFormat#TEXT_HTML}) or a {@link SimpleEmail} (for
 * {@link EmailFormat#TEXT_PLAIN}). <br>
 * It calls the method {@link #sendEmail(JemEmail, Email)}.
 * /* w  w  w  . j  a va 2  s .  co m*/
 * @param email the <code>JemEmail</code> to be sent.
 * @see JemEmail
 * @throws SendMailException if an error occurs.
 */
private void doMailNotify(JemEmail email) throws SendMailException {
    if (null == email) {
        // log email null
        LogAppl.getInstance().emit(NotifyMessage.JEMN013E, "Email to send");
        throw new SendMailException(NotifyMessage.JEMN013E.toMessage().getFormattedMessage("Email to send"));
    }
    if (!email.hasToEmailAddresses()) {
        LogAppl.getInstance().emit(NotifyMessage.JEMN003E);
        throw new SendMailException(NotifyMessage.JEMN003E.toMessage().getMessage());
    }
    if (!email.hasFromUserEmailAddress()) {
        LogAppl.getInstance().emit(NotifyMessage.JEMN012E, "From User Email Address");
        throw new SendMailException(
                NotifyMessage.JEMN012E.toMessage().getFormattedMessage("From User Email Address"));
    }
    Email sendingEmail = null;
    if (email.getFormat() == EmailFormat.TEXT_HTML) {
        sendingEmail = new HtmlEmail();
    } else {
        sendingEmail = new SimpleEmail();
    }
    sendEmail(email, sendingEmail);
}