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

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

Introduction

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

Prototype

public HtmlEmail setTextMsg(final String aText) throws EmailException 

Source Link

Document

Set the text content.

Usage

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  ava2  s .  c  o m

    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 ww  w . j  a  v a 2s.  c  om*/

    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.openhab.binding.mail.internal.MailBuilder.java

/**
 * Build the Mail/*from   www. j  ava  2s  .  co  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  ww w. ja v  a 2  s  .co m*/
    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 is a convenience method for sending and email to 1 recipient using the configuration file settings.
 * @throws EmailException /*from   w  w w .j a va  2  s . co  m*/
 */
public static void sendEmail(String toEmailAddress, String toName, String fromEmailAddress, String fromName,
        String subject, String textContents, String htmlContents) throws EmailException {
    HtmlEmail htmlEmail = getHtmlEmail();

    htmlEmail.addTo(toEmailAddress, toName);
    htmlEmail.setFrom(fromEmailAddress, fromName);

    htmlEmail.setSubject(subject);
    if (textContents != null)
        htmlEmail.setTextMsg(textContents);
    if (htmlContents != null)
        htmlEmail.setHtmlMsg(htmlContents);

    htmlEmail.send();
}

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

/**
 * This main method is useful when debugging smtp configuration problems.
 *//* w  w w  . j a v  a2  s  .  co  m*/
public static void main(String... argv) throws EmailException {
    // gmail : smtp.gmail.com:465      

    String fromEmailAddress = argv[0];
    String toEmailAddress = argv[1];
    String smtpServer = argv[2];

    String smtpPort = (argv.length > 3 ? argv[3] : null);
    String smtpUser = (argv.length > 4 ? argv[4] : null);
    String smtpPassword = (argv.length > 5 ? argv[4] : null);
    String connectionSecurity = (argv.length > 6 ? argv[5] : null);
    HtmlEmail htmlEmail = EmailUtils.getHtmlEmail(smtpServer, smtpPort, smtpUser, smtpPassword,
            connectionSecurity);

    htmlEmail.addTo(toEmailAddress, toEmailAddress);
    htmlEmail.setFrom(fromEmailAddress, fromEmailAddress);

    htmlEmail.setSubject("test subject");
    htmlEmail.setTextMsg("test contents " + (new java.util.Date()));

    htmlEmail.send();
}

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

/**
 * This main method is useful when debugging smtp configuration problems.
 *//*w  w w  .  jav a 2s.  c o m*/
public static void main(String... argv) throws EmailException {
    // gmail : smtp.gmail.com:465      

    String fromEmailAddress = argv[0];
    String toEmailAddress = argv[1];
    String smtpServer = argv[2];

    String smtpPort = (argv.length > 3 ? argv[3] : null);
    String smtpUser = (argv.length > 4 ? argv[4] : null);
    String smtpPassword = (argv.length > 5 ? argv[4] : null);
    String connectionSecurity = (argv.length > 6 ? argv[5] : null);
    HtmlEmail htmlEmail = EmailUtilsOld.getHtmlEmail(smtpServer, smtpPort, smtpUser, smtpPassword,
            connectionSecurity);

    htmlEmail.addTo(toEmailAddress, toEmailAddress);
    htmlEmail.setFrom(fromEmailAddress, fromEmailAddress);

    htmlEmail.setSubject("test subject");
    htmlEmail.setTextMsg("test contents " + (new java.util.Date()));

    htmlEmail.send();
}

From source file:org.structr.common.MailHelper.java

public static String sendHtmlMail(final String from, final String fromName, final String to,
        final String toName, final String cc, final String bcc, final String bounce, final String subject,
        final String htmlContent, final String textContent) throws EmailException {

    HtmlEmail mail = new HtmlEmail();

    setup(mail, to, toName, from, fromName, cc, bcc, bounce, subject);
    mail.setHtmlMsg(htmlContent);//from  w ww. j  ava 2 s  .  c o m
    mail.setTextMsg(textContent);

    return mail.send();
}

From source file:org.teknux.dropbitz.service.email.EmailSender.java

public void sendEmail(DropbitzEmail dropbitzEmail, HtmlEmail email) throws EmailServiceException {
    logger.debug("Send email...");

    if (dropbitzEmail == null) {
        throw new EmailServiceException("DropbitzEmail can not be null");
    }//from   w  w  w  .ja v  a  2s .co  m
    if (email == null) {
        throw new EmailServiceException("HtmlEmail can not be null");
    }

    //Global Configuration
    email.setHostName(Objects.requireNonNull(config.getEmailHost(), "Email Host is required"));
    email.setSmtpPort(config.getEmailPort());
    if ((config.getEmailUsername() != null && !config.getEmailUsername().isEmpty())
            || (config.getEmailPassword() != null && !config.getEmailPassword().isEmpty())) {
        email.setAuthentication(config.getEmailUsername(), config.getEmailPassword());
    }
    email.setSSLOnConnect(config.isEmailSsl());

    email.setSubject(dropbitzEmail.getSubject());
    try {
        email.setFrom(Objects.requireNonNull(dropbitzEmail.getEmailFrom(), "Email From is required"));
        if (dropbitzEmail.getEmailTo() == null || dropbitzEmail.getEmailTo().size() == 0) {
            throw new EmailServiceException("Email To is required");
        }
        email.addTo(dropbitzEmail.getEmailTo().toArray(new String[dropbitzEmail.getEmailTo().size()]));
        email.setHtmlMsg(Objects.requireNonNull(dropbitzEmail.getHtmlMsg(), "HtmlMsg is required"));
        if (dropbitzEmail.getTextMsg() != null) {
            email.setTextMsg(dropbitzEmail.getTextMsg());
        }
        email.send();

        logger.trace(MessageFormat.format("Email sent from [{0}] to [{1}]", dropbitzEmail.getEmailFrom(),
                String.join(",", dropbitzEmail.getEmailTo())));
    } catch (EmailException e) {
        throw new EmailServiceException("Email not sent", e);
    }
}

From source file:uap.workflow.engine.bpmn.behavior.MailActivityBehavior.java

protected HtmlEmail createHtmlEmail(String text, String html) {
    HtmlEmail email = new HtmlEmail();
    try {/*from  w w w  .jav  a 2  s  .  co  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 WorkflowException("Could not create HTML email", e);
    }
}