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.ploin.pmf.impl.MailSender.java

public SendingResult sendMail(String plain, String html, MailConfig mailConfig, ServerConfig serverConfig,
        Map<String, String> map) throws MailFactoryException {
    try {//from www . j  a  v  a  2s . co  m
        if (html == null || html.trim().equals("")) {
            MultiPartEmail email = new MultiPartEmail();
            if (mailConfig.isAttachementsEmpty()) {
                email.setContent(plain, "text/plain; charset=iso-8859-1");
            } else {
                email.setMsg(plain);
            }
            return send(email, mailConfig, serverConfig);
        }

        HtmlEmail email = new HtmlEmail();
        setEmbeds(email, map, html);
        if (plain != null && !"".equals(plain.trim()))
            email.setTextMsg(plain);

        email.setHtmlMsg(html);
        return send(email, mailConfig, serverConfig);
    } catch (Exception e) {
        throw new MailFactoryException(e);
    }
}

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  w w. ja v  a2s  . co m*/
    mail.setTextMsg(textContent);

    return mail.send();
}

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

public void sendEmail(DropbitzEmail dropbitzEmail) throws EmailServiceException {
    sendEmail(dropbitzEmail, new HtmlEmail());
}

From source file:org.vulpe.commons.util.VulpeEmailUtil.java

/**
 * Send Mail to many recipients.//from w ww.j a v  a  2 s .  co m
 *
 * @param recipients
 *            Recipients
 * @param subject
 *            Subject
 * @param body
 *            Body
 * @throws VulpeSystemException
 *             exception
 */
public static boolean sendMail(final String[] recipients, final String subject, final String body) {
    boolean sended = true;
    if (!checkValidEmail(recipients)) {
        LOG.error("Invalid mails: " + recipients);
        sended = false;
    }
    if (isDebugEnabled) {
        LOG.debug("Entering in sendMail...");
        for (int i = 0; i < recipients.length; i++) {
            LOG.debug("recipient: " + recipients[i]);
        }
        LOG.debug("subject: " + subject);
        LOG.debug("body: " + body);
    }
    try {
        final ResourceBundle bundle = ResourceBundle.getBundle("mail");
        if (bundle != null) {
            final HtmlEmail mail = new HtmlEmail();
            String mailFrom = "";
            if (bundle.containsKey("mail.smtp.auth") && Boolean.valueOf(bundle.getString("mail.smtp.auth"))) {
                final String username = bundle.getString("mail.smtp.user");
                final String password = bundle.getString("mail.smtp.password");
                mail.setAuthentication(username, password);
            }
            if (bundle.containsKey("mail.from")) {
                mailFrom = bundle.getString("mail.from");
            }
            mail.setFrom(mailFrom);
            for (final String recipient : recipients) {
                mail.addTo(recipient);
            }
            mail.setHostName(bundle.getString("mail.smtp.host"));
            final String port = bundle.getString("mail.smtp.port");
            mail.setSmtpPort(Integer.valueOf(port));
            if (bundle.containsKey("mail.smtp.starttls.enable")
                    && Boolean.valueOf(bundle.getString("mail.smtp.starttls.enable"))) {
                mail.setTLS(true);
                mail.setSSL(true);
                if (bundle.containsKey("mail.smtp.socketFactory.port")) {
                    String factoryPort = bundle.getString("mail.smtp.socketFactory.port");
                    mail.setSslSmtpPort(factoryPort);
                }
            }
            String encoding = "UTF-8";
            if (bundle.containsKey("mail.encode")) {
                encoding = bundle.getString("mail.encode");
            }
            mail.setCharset(encoding);
            mail.setSubject(subject);
            mail.setHtmlMsg(body);
            mail.send();
        } else {
            LOG.error("Send Mail properties not setted");
            sended = false;
        }
    } catch (Exception e) {
        LOG.error("Error on send mail", e);
        sended = false;
    }
    LOG.debug("Out of sendMail...");
    return sended;
}

From source file:org.xerela.server.birt.ReportJob.java

/**
 * Email the report if the JobData defines the required parameters,
 * otherwise this method just returns without doing anything.
 *
 * @param intermediate the BIRT intermediate format file
 * @param executionContext the Quartz JobExecutionContext of this job
 *///  ww w.j a  va2s  .c om
@SuppressWarnings("nls")
private void emailReport(File intermediate, JobExecutionContext executionContext) {
    JobDataMap jobData = executionContext.getMergedJobDataMap();
    boolean emailAttachment = jobData.containsKey(REPORT_EMAIL_ATTACHMENT)
            ? jobData.getBooleanValue(REPORT_EMAIL_ATTACHMENT)
            : false;
    boolean emailLink = jobData.containsKey(REPORT_EMAIL_LINK) ? jobData.getBooleanValue(REPORT_EMAIL_LINK)
            : false;
    String emailTo = jobData.getString(REPORT_EMAIL_TO);
    String emailCc = jobData.getString(REPORT_EMAIL_CC);
    String reportFormat = jobData.getString(REPORT_EMAIL_FORMAT);

    if (!validateEmailProperties(emailAttachment, emailLink, emailTo, reportFormat)) {
        return;
    }

    try {
        Email email = null;
        if (reportFormat.equals("pdf")) {
            email = new MultiPartEmail();
        } else if (reportFormat.equals("html")) {
            email = new HtmlEmail();
        }

        setupEmail(executionContext, emailTo, emailCc, email);

        // Create the attachment
        if (emailAttachment) {
            final File render = RenderElf.render(intermediate, executionData.getId(), reportFormat);

            if (reportFormat.equals("pdf")) {
                EmailAttachment attachment = new EmailAttachment();
                attachment.setPath(render.getAbsolutePath());
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                attachment.setDescription(reportTitle);
                attachment.setName(String.format("%s.%s", reportTitle, reportFormat)); //$NON-NLS-1$
                ((MultiPartEmail) email).attach(attachment);
            } else if (reportFormat.equals("html")) {
                HtmlEmail htmlEmail = (HtmlEmail) email;
                String html = stringFromFile(render);

                final String pathStub = render.getName().replaceFirst("\\.[a-z]+$", ""); //$NON-NLS-1$ //$NON-NLS-2$
                File parentDir = new File(render.getParent());
                File[] listFiles = parentDir.listFiles(new FileFilter() {
                    public boolean accept(File pathname) {
                        return pathname.getName().startsWith(pathStub) && !pathname.getName().endsWith("html"); //$NON-NLS-1$
                    }
                });

                for (File image : listFiles) {
                    String cid = htmlEmail.embed(image);
                    String regex = "src=.+" + image.getName();
                    html = html.replaceAll(regex, "src=\"cid:" + cid);
                }

                htmlEmail.setHtmlMsg(html);
            }
        }

        if (emailLink) {
        }

        email.send();
    } catch (AddressException ae) {
        LOGGER.error(Messages.bind(Messages.ReportJob_badAddresses, reportTitle), ae);
    } catch (EmailException ee) {
        LOGGER.error(Messages.bind(Messages.ReportJob_errorSending, reportTitle), ee);
    } catch (EngineException ee) {
        LOGGER.error(Messages.bind(Messages.ReportJob_errorSending, reportTitle), ee);
    } catch (IOException ie) {
        LOGGER.error(Messages.bind(Messages.ReportJob_errorSending, reportTitle), ie);
    }
}

From source file:org.xmatthew.spy2servers.component.util.EMailUtils.java

/**
 * <p>/*from  w w w  .ja  va2s.c  om*/
 * send html body mail
 * </p>
 *
 * @param mailBody
 * @throws EmailException email about exception
 */
public static void sendHtmlEmail(MailBody mailBody) throws EmailException {

    HtmlEmail email = new HtmlEmail();
    email.setHostName(mailBody.getLoginServer());
    email.setAuthentication(mailBody.getLoginName(), mailBody.getLoginPassword());

    //receivers
    Map<String, String> receivers = mailBody.getReceivers();
    parseReceivers(email, receivers);
    //cc receivers
    receivers = mailBody.getCcReceivers();
    parseCCRecievers(email, receivers);
    //attatchments
    Map<String, EmailAttachment> attatchments = mailBody.getAttachments();
    parseAttatchments(email, attatchments);

    String aHtml = mailBody.getAHtml();
    if (StringUtils.isNotBlank(aHtml)) {
        email.setHtmlMsg(aHtml);
    }

    email.setFrom(mailBody.getSender(), mailBody.getSender_nick());
    email.setSubject(mailBody.getSubject());
    email.setMsg(mailBody.getBody());
    email.send();
}

From source file:org.yestech.notify.deliver.HtmlEmailDelivery.java

protected void sendMessage(INotification notification, IRecipient recipient) throws EmailException {
    // Create the email message
    HtmlEmail email = new HtmlEmail();
    enableAuthenticator(email);// w ww.  jav  a 2 s.co  m
    email.setHostName(getEmailHost());
    ISender sender = notification.getSender();
    email.setFrom(sender.getEmailAddress(), sender.getDisplayName());
    if (StringUtils.isNotBlank(sender.getReplyAddress())) {
        email.addReplyTo(sender.getReplyAddress());
    }
    email.setSubject(notification.getMessage().getSubject());
    email.addTo(recipient.getEmailAddress(), recipient.getDisplayName());
    ITemplateLanguage template = notification.getTemplate();
    String appliedMessage = template.apply(notification.getMessage());
    email.setHtmlMsg(appliedMessage);
    email.send();
}

From source file:shnakkydoodle.notifying.endpoints.EmailEndpoint.java

/**
 * Send the notification//ww w  .j av a2  s .  com
 * 
 * @param recipient
 * @param subject
 * @param message
 * @throws Exception
 */
@Override
public void send(String recipient, String subject, String message) throws Exception {

    Email email = null;

    if (!this.properties.getProperties().containsKey("notification.email.server")
            || !this.properties.getProperties().containsKey("notification.email.port")
            || !this.properties.getProperties().containsKey("notification.email.username")
            || !this.properties.getProperties().containsKey("notification.email.password")
            || !this.properties.getProperties().containsKey("notification.email.fromemail")) {
        throw new Exception("Notification email settings incomplete");
    }

    // Determine if we need to send an html or simple email
    if (message.trim().startsWith("<")) {
        email = new HtmlEmail();
    } else {
        email = new SimpleEmail();
    }

    email.setHostName(this.properties.getProperties().get("notification.email.server").toString());
    email.setSmtpPort(
            Integer.parseInt(this.properties.getProperties().get("notification.email.port").toString()));
    email.setAuthentication(this.properties.getProperties().get("notification.email.username").toString(),
            this.properties.getProperties().get("notification.email.password").toString());
    email.setFrom(this.properties.getProperties().get("notification.email.fromemail").toString());
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(recipient);
    email.send();
}

From source file:sk.baka.webvm.analyzer.utils.NotificationDelivery.java

/**
 * Sends a mail with given report.//from   w w w.j  a  va 2s.  co m
 * @param config the mail server configuration.
 * @param testing if true then a testing mail is sent
 * @param reports the current reports
 * @throws org.apache.commons.mail.EmailException if sending mail fails.
 */
public static void sendEmail(final Config config, final boolean testing, final List<ProblemReport> reports)
        throws EmailException {
    if (!isEmailEnabled(config)) {
        return;
    }
    final HtmlEmail mail = new HtmlEmail();
    configure(mail, config);
    mail.setSubject("WebMon: Problems notification" + (testing ? " (testing mail)" : ""));
    mail.setMsg(ProblemReport.toString(reports, "\n"));
    mail.setHtmlMsg("<html><body>\n" + ProblemReport.toHtml(reports) + "\n</body></html>");
    mail.send();
}

From source file:tilda.utils.MailUtil.java

/**
 * /*w  w w.j a  v  a2 s . c o  m*/
 * @param SmtpInfo A string such as smtp.mydomain.com:422:ssl to connect to an SMTP server
 * @param From the user ID used to send emails from
 * @param Password The password for the account we send emails from
 * @param To Destination email(s)
 * @param Cc CC email(s)
 * @param Bcc BCC emails(s)
 * @param Subject The Subject
 * @param Message The message (HTML allowed)
 * @param Urgent Whether to send the message as urgent or not.
 * @return
 */
public static boolean send(String SmtpInfo, String From, String Password, String[] To, String[] Cc,
        String[] Bcc, String Subject, String Message, boolean Urgent) {
    String LastAddress = null;
    try {
        HtmlEmail email = new HtmlEmail();

        String[] parts = SmtpInfo.split(":");
        email.setHostName(parts[0]);
        if (parts.length > 1) {
            if (parts.length > 2 && parts[2].equalsIgnoreCase("ssl") == true) {
                email.setSslSmtpPort(parts[1]);
                email.setSSLOnConnect(true);
            } else {
                email.setSmtpPort(Integer.parseInt(parts[1]));
            }
        }

        email.setAuthentication(From, Password);
        email.setSubject(Subject);

        LOG.debug("Sending an email '" + email.getSubject() + "'.");
        if (To != null)
            for (String s : To) {
                if (TextUtil.isNullOrEmpty(s) == true)
                    continue;
                LastAddress = s;
                email.addTo(s);
            }
        if (Cc != null)
            for (String s : Cc) {
                if (TextUtil.isNullOrEmpty(s) == true)
                    continue;
                LastAddress = s;
                email.addCc(s);
            }
        if (Bcc != null)
            for (String s : Bcc) {
                if (TextUtil.isNullOrEmpty(s) == true)
                    continue;
                LastAddress = s;
                email.addBcc(s);
            }
        if (LastAddress == null) {
            LOG.debug("No recipient. Not sending anything.");
            return true;
        }
        email.setFrom(From);
        LastAddress = From;
        email.setHtmlMsg(Message);
        if (Urgent == true)
            email.addHeader("X-Priority", "1");
        LastAddress = null;
        email.send();
        return true;
    } catch (EmailException E) {
        if (LastAddress != null)
            LOG.debug("Email address '" + LastAddress + "' seems to be invalid.");
        LOG.error(E);
        return false;
    }
}