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

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

Introduction

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

Prototype

public String send() throws EmailException 

Source Link

Document

Sends the email.

Usage

From source file:com.baifendian.swordfish.common.mail.MailSendUtil.java

/**
 * ??/* w  ww.j  a v  a 2 s  .  c o m*/
 *
 * @param receivers
 * @param title
 * @param content
 * @return
 */
public static boolean sendMails(Collection<String> receivers, String title, String content) {
    if (receivers == null) {
        LOGGER.error("Mail receivers is null.");
        return false;
    }

    receivers.removeIf((from) -> (StringUtils.isEmpty(from)));

    if (receivers.isEmpty()) {
        LOGGER.error("Mail receivers is empty.");
        return false;
    }

    // ?? email
    HtmlEmail email = new HtmlEmail();

    try {
        //  SMTP ?????, 163 "smtp.163.com"
        email.setHostName(mailServerHost);

        email.setSmtpPort(mailServerPort);

        // ?
        email.setCharset("UTF-8");
        // 
        for (String receiver : receivers) {
            email.addTo(receiver);
        }
        // ??
        email.setFrom(mailSender, mailSender);
        // ???????-??????
        email.setAuthentication(mailSender, mailPasswd);
        // ???
        email.setSubject(title);
        // ???? HtmlEmail? HTML 
        email.setMsg(content);
        // ??
        email.send();

        return true;
    } catch (Throwable e) {
        LOGGER.error("Send email to {} failed", StringUtils.join(",", receivers), e);
    }

    return false;
}

From source file:br.com.smarttaco.util.EnviarEmail.java

/**
 * Enviar o email para a lista de usarios especificados
 * @param emails//  ww  w. j a  v a2s  . c  o  m
 * @param assunto
 * @param conteudo 
 */
public static void tratarEnvio(ArrayList<String> emails, String assunto, String conteudo) {
    HtmlEmail email = new HtmlEmail();

    try {
        email.setHostName(Constantes.HOST_NAME_GMAIL);
        email.addTo(Constantes.ADMINISTRADOR_1);
        email.setFrom(Constantes.EMAIL_REMETENTE_GMAIL, "SmartTaco - Administrador");

        for (String tmp : emails) {
            email.addBcc(tmp);
        }

        email.setSubject(assunto);

        // Trabalhando com imagem...
        //            URL url = new URL ("http://<ENDERECO DA IMAGEM AQUI...>");
        //            String idImg = email.embed(url, "logo");

        email.setHtmlMsg(conteudo);

        // Tratando mensagem alternativa
        email.setTextMsg("Seu servidor de e-mail no suporta mensagem HTML... :-(");

        email.setSmtpPort(Constantes.PORTA_SMTP_GMAIL);
        email.setAuthenticator(
                new DefaultAuthenticator(Constantes.EMAIL_REMETENTE_GMAIL, Constantes.SENHA_REMETENTE_GMAIL));
        email.setSSLOnConnect(true);

        // Enviando email
        email.send();

    } catch (EmailException e) {
        e.printStackTrace();
    }
}

From source file:com.sigaf.bean.MailReset.java

public static void enviaEmail(TUsuario usuario, String URI) throws EmailException, MalformedURLException {

    HtmlEmail email;
    email = conectaEmail();/*from   w w w . ja  v  a  2s  . co  m*/
    email.setSubject("Restablece tu contrasea");

    //        URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
    //        String cid2 = email.embed(url, "logo ??.gif");

    email.setHtmlMsg("<html>\n" + "     <head>\n" + "        <title>Restablece tu contrasea</title>\n"
            + "     </head>\n" + "     <body>\n"
            + "       <p>Hemos recibido una peticin para restablecer la contrasea de tu cuenta.</p>\n"
            + "       <p>Si hiciste esta peticin, haz clic en el siguiente enlace, si no hiciste esta peticin puedes ignorar este correo.</p>\n"
            + "       <p>\n" + "         <strong>Enlace para restablecer tu contrasea</strong><br>\n"
            + "         <a href='" + URI + "'> Restablecer contrasea </a>\n" + "       </p>\n"
            + "     </body>\n" + "\n" + "     </html>");

    email.setTextMsg("Your email client does not support HTML messages");
    email.addTo(usuario.getTEmpleado().getCorreoEmpleado());
    email.send();
}

From source file:com.mycollab.module.mail.DefaultMailer.java

@Override
public void sendHTMLMail(String fromEmail, String fromName, List<MailRecipientField> toEmails,
        List<MailRecipientField> ccEmails, List<MailRecipientField> bccEmails, String subject, String html) {
    try {//from  w w  w  . j a  v  a 2 s .  co m
        HtmlEmail email = getBasicEmail(fromEmail, fromName, toEmails, ccEmails, bccEmails, subject, html);
        email.send();
    } catch (EmailException e) {
        throw new MyCollabException(e);
    }
}

From source file:com.esofthead.mycollab.module.mail.DefaultMailer.java

@Override
public void sendHTMLMail(String fromEmail, String fromName, List<MailRecipientField> toEmail,
        List<MailRecipientField> ccEmail, List<MailRecipientField> bccEmail, String subject, String html) {
    try {/*  w  ww  .j av  a  2 s  .  c om*/
        HtmlEmail email = getBasicEmail(fromEmail, fromName, toEmail, ccEmail, bccEmail, subject, html);

        email.send();
    } catch (EmailException e) {
        throw new MyCollabException(e);
    }
}

From source file:com.ning.metrics.meteo.publishers.AlertListener.java

private void createAndSendAlertEmail(String body) {
    try {//from   ww w.j a v  a 2s  .  com
        log.info(String.format("Sending alert email to [%s]: %s", config.getRecipients(), body));

        HtmlEmail email = new HtmlEmail();

        email.setTextMsg(body);
        email.setFrom("esper-is-awesome@example.com");
        email.setTo(Arrays.asList(new InternetAddress(config.getRecipients())));
        email.setHostName(config.getHost());
        email.setSmtpPort(config.getPort());
        email.send();
    } catch (Exception ex) {
        log.warn("Could not create or send email", ex);
    }
}

From source file:com.mycollab.module.mail.DefaultMailer.java

@Override
public void sendHTMLMail(String fromEmail, String fromName, List<MailRecipientField> toEmails,
        List<MailRecipientField> ccEmails, List<MailRecipientField> bccEmails, String subject, String html,
        List<? extends AttachmentSource> attachments) {
    try {/*from   w  ww. j  a v a 2  s . c  o m*/
        if (CollectionUtils.isEmpty(attachments)) {
            sendHTMLMail(fromEmail, fromName, toEmails, ccEmails, bccEmails, subject, html);
        } else {
            HtmlEmail email = getBasicEmail(fromEmail, fromName, toEmails, ccEmails, bccEmails, subject, html);

            for (AttachmentSource attachment : attachments) {
                email.attach(attachment.getAttachmentObj());
            }

            email.send();
        }
    } catch (EmailException e) {
        throw new MyCollabException(e);
    }
}

From source file:com.esofthead.mycollab.module.mail.DefaultMailer.java

@Override
public void sendHTMLMail(String fromEmail, String fromName, List<MailRecipientField> toEmail,
        List<MailRecipientField> ccEmail, List<MailRecipientField> bccEmail, String subject, String html,
        List<EmailAttachementSource> attachments) {
    try {/*from   w  w w. j a  v a  2  s. c  om*/
        if (CollectionUtils.isEmpty(attachments)) {
            sendHTMLMail(fromEmail, fromName, toEmail, ccEmail, bccEmail, subject, html);
        } else {
            HtmlEmail email = getBasicEmail(fromEmail, fromName, toEmail, ccEmail, bccEmail, subject, html);

            for (EmailAttachementSource attachment : attachments) {
                email.attach(attachment.getAttachmentObj());
            }

            email.send();
        }
    } catch (EmailException e) {
        throw new MyCollabException(e);
    }
}

From source file:ca.ualberta.physics.cssdp.auth.service.EmailServiceImpl.java

public void sendEmail(String from, String to, String subject, String body) {

    try {/*  w ww .j  a v a  2  s .c om*/

        String host = AuthServer.properties().getString("smtpHost");
        int port = AuthServer.properties().getInt("smtpPort");
        final String user = AuthServer.properties().getString("smtpUsername");
        final String password = AuthServer.properties().getString("smtpPassword");
        String systemEmail = AuthServer.properties().getString("systemEmailAddress");
        boolean useSSL = AuthServer.properties().getBoolean("smtpUseSSL");
        boolean debug = AuthServer.properties().getBoolean("smtpDebug");

        HtmlEmail msg = new HtmlEmail();
        msg.setHostName(host);
        msg.setAuthentication(user, password);
        msg.setSmtpPort(port);
        msg.setSSL(useSSL);
        msg.setDebug(debug);
        msg.setSubject("Password Reset Request");
        msg.addTo(to);
        msg.setFrom(systemEmail);
        msg.setHtmlMsg(body);
        msg.send();

    } catch (Exception e) {
        e.printStackTrace();
        throw Throwables.propagate(Throwables.getRootCause(e));
    } finally {
    }

}

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

public void send(EmailMessage msg) throws Exception {

    HtmlEmail email = new HtmlEmail();

    email.setSmtpPort(port);/*from w  w  w  .ja  v  a2  s  .c o m*/
    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();

}