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:br.com.atmatech.sac.controller.Email.java

public void emaiMassa(String smtp, String user, String password, Integer porta, Boolean ssl, Boolean tls,
        List<PessoaBeans> emailto, String emailfrom, String conteudo, String assunto)
        throws EmailException, MalformedURLException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(smtp); // o servidor SMTP para envio do e-mail
    Integer indice2 = 0;//from w w w  .ja  v  a  2s. c o  m
    Integer j = 0;
    for (int i = 0; (i < emailto.size()) && (i < 45); i++) {
        email.addTo(emailto.get(i).getEmail());//destinatario            
        indice2 = i;
    }
    while (j <= indice2) {
        emailto.remove(0);
        j++;
    }
    conteudo = conteudo.replaceAll("\n", "<p>");
    email.setFrom(emailfrom); // remetente        
    //email.addCc(emailfrom);
    email.setSubject(assunto);
    // configura a mensagem para o formato HTML        
    email.setHtmlMsg("<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">"
            + conteudo + "</html>");
    email.setAuthentication(user, password);
    email.setSmtpPort(porta);
    email.setSSL(ssl);
    email.setTLS(tls);
    email.send();
    if (emailto != null) {
        if (emailto.size() > 1) {
            emaiMassa(smtp, user, password, porta, ssl, tls, emailto, emailfrom, conteudo, assunto);
        }
    }

}

From source file:dk.dma.ais.abnormal.analyzer.reports.ReportMailer.java

/**
 * Send email with subject and message body.
 * @param subject the email subject.//from  www .  ja va 2  s  .  c  om
 * @param body the email body.
 */
public void send(String subject, String body) {
    try {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_HOST, "localhost"));
        email.setSmtpPort(configuration.getInt(CONFKEY_REPORTS_MAILER_SMTP_PORT, 465));
        email.setAuthenticator(
                new DefaultAuthenticator(configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_USER, "anonymous"),
                        configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_PASS, "guest")));
        email.setStartTLSEnabled(false);
        email.setSSLOnConnect(configuration.getBoolean(CONFKEY_REPORTS_MAILER_SMTP_SSL, true));
        email.setFrom(configuration.getString(CONFKEY_REPORTS_MAILER_SMTP_FROM, ""));
        email.setSubject(subject);
        email.setHtmlMsg(body);
        String[] receivers = configuration.getStringArray(CONFKEY_REPORTS_MAILER_SMTP_TO);
        for (String receiver : receivers) {
            email.addTo(receiver);
        }
        email.send();
        LOG.info("Report sent with email to " + email.getToAddresses().toString() + " (\"" + subject + "\")");
    } catch (EmailException e) {
        LOG.error(e.getMessage(), e);
    }
}

From source file:br.com.ezequieljuliano.argos.util.SendMail.java

public void send() throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(server.getHostName());
    email.setAuthentication(server.getUser(), server.getPassWord());
    email.setSSL(server.isSSL());//  w ww  .j  a  va 2s  . co m
    email.setSmtpPort(server.getPort());

    for (Involved involved : recipients) {
        email.addTo(involved.getEmail(), involved.getName());
    }

    email.setFrom(sender.getEmail(), sender.getName());
    email.setSubject(subject);
    email.setHtmlMsg(message);
    email.setCharset("UTF-8");

    EmailAttachment att;
    for (Attachment annex : attachment) {
        att = new EmailAttachment();
        att.setPath(annex.getPath());
        att.setDisposition(EmailAttachment.ATTACHMENT);
        att.setDescription(annex.getDescription());
        att.setName(annex.getName());
        email.attach(att);
    }

    email.send();
}

From source file:com.vicky.common.utils.sendemail.SendEmailImpl.java

@Override
public void send(Mail mail) throws Exception {
    try {//from w w  w  .  j  a  v  a  2 s  . co m
        HtmlEmail htmlEmail = new HtmlEmail();
        htmlEmail.setHostName(mail.getHost());
        htmlEmail.setCharset(Mail.ENCODEING);
        htmlEmail.addTo(mail.getReceiverEmailAddress());
        htmlEmail.setFrom(mail.getSenderEmailAddress(), mail.getName());
        htmlEmail.setAuthentication(mail.getSenderUsername(), mail.getSenderPassword());
        htmlEmail.setSubject(mail.getSubject());
        htmlEmail.setMsg(mail.getMessage());
        for (MailAttach mailFile : mail.getMailAttachs()) {
            EmailAttachment attachment = new EmailAttachment();//  
            attachment.setPath(mailFile.getFilePath());//?    
            //attachment.setURL(new URL("http://www.baidu.com/moumou"));//?  
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setName(MimeUtility.encodeText(mailFile.getFileName()));//??  
            htmlEmail.attach(attachment);//,?
        }
        htmlEmail.send();
    } catch (Exception exception) {
        exception.printStackTrace();
        this.logger.error("toString" + exception.toString());
        this.logger.error("getMessage" + exception.getMessage());
        this.logger.error("exception", exception);
        throw new StatusMsgException(
                "??,??,?,?V!");
    }
}

From source file:be.thomasmore.controller.EmailController.java

public String sendEmail() throws EmailException {
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestParameterMap();//  w  w w  .j a va2s . c  o  m
    String id = params.get("studentId");
    int studentId = Integer.parseInt(id);
    Student student = service.getStudent(studentId);
    HtmlEmail email = new HtmlEmail();

    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator("pointernulltest@gmail.com", "r0449914"));
    email.setSSLOnConnect(true);
    email.addTo(student.getEmail(), student.getNaam() + " " + student.getVoornaam());
    email.setFrom("me@apache.org", "Thomas More Geel");
    email.setSubject("Rapport");
    StringBuffer msg = new StringBuffer();
    msg.append("<html><body>");
    msg.append("<h2>Resultaten</h2>");
    List<Score> scores = student.getScoreList();
    msg.append("<p>Beste " + student.getVoornaam() + " " + student.getNaam()
            + " hieronder vind je je punten voor afgelopen semester.");
    for (Score score : scores) {
        msg.append("<p>");
        msg.append(score.getTestId().getVakId().getNaam() + " " + score.getTestId().getBeschrijving() + " : "
                + score.getScore());
        msg.append("</p>");
        msg.append("</body></html>");
    }
    email.setHtmlMsg(msg.toString());

    email.send();
    return null;
}

From source file:br.com.hslife.imobiliaria.service.EmailService.java

/**
 * Envia email no formato HTML/*from   w  ww . j a va 2s .c o m*/
 *
 * @param nomeRemetente
 * @param nomeDestinatario
 * @param emailRemetente
 * @param emailDestinatario
 * @param assunto
 * @param mensagem
 * @param anexo
 *
 * @throws EmailException
 * @throws MalformedURLException
 */
public void enviaEmailFormatoHtml(String nomeRementente, String emailRemetente, String nomeDestinatario,
        String emailDestinatario, String assunto, StringBuilder mensagem, String anexo)
        throws EmailException, MalformedURLException {

    HtmlEmail email = new HtmlEmail();

    // adiciona uma imagem ao corpo da mensagem e retorna seu id
    URL url = new URL(anexo); // URL do arquivo a ser anexado
    String cid = email.embed(url, "Anexos");

    // configura a mensagem para o formato HTML
    email.setHtmlMsg("<html>Anexos</html>");

    // configure uma mensagem alternativa caso o servidor no suporte HTML
    email.setTextMsg("Seu servidor de e-mail no suporta mensagem HTML");

    email.setHostName("smtp.hslife.com.br"); // o servidor SMTP para envio do e-mail
    email.addTo(emailDestinatario, nomeDestinatario); //destinatrio
    email.setFrom(emailRemetente, nomeRementente); // remetente
    email.setSubject(assunto); // assunto do e-mail
    email.setMsg(mensagem.toString()); //conteudo do e-mail
    email.setAuthentication("realimoveis@hslife.com.br", "real123");
    //email.setSmtpPort(465);
    //email.setSSL(true);
    //email.setTLS(true);

    // envia email
    email.send();
}

From source file:br.com.hslife.catu.service.EmailService.java

/**
 * Envia email no formato HTML//w w  w .j a  v a 2 s.c  o  m
 *
 * @param nomeRemetente
 * @param nomeDestinatario
 * @param emailRemetente
 * @param emailDestinatario
 * @param assunto
 * @param mensagem
 * @param anexo
 *
 * @throws EmailException
 * @throws MalformedURLException
 */
public void enviaEmailFormatoHtml(String nomeRementente, String emailRemetente, String nomeDestinatario,
        String emailDestinatario, String assunto, StringBuilder mensagem, String anexo)
        throws EmailException, MalformedURLException {

    HtmlEmail email = new HtmlEmail();

    // adiciona uma imagem ao corpo da mensagem e retorna seu id
    URL url = new URL(anexo); // URL do arquivo a ser anexado
    String cid = email.embed(url, "Anexos");

    // configura a mensagem para o formato HTML
    email.setHtmlMsg("<html>Anexos</html>");

    // configure uma mensagem alternativa caso o servidor no suporte HTML
    email.setTextMsg("Seu servidor de e-mail no suporta mensagem HTML");

    email.setHostName("smtp.hslife.com.br"); // o servidor SMTP para envio do e-mail
    email.addTo(emailDestinatario, nomeDestinatario); //destinatrio
    email.setFrom(emailRemetente, nomeRementente); // remetente
    email.setSubject(assunto); // assunto do e-mail
    email.setMsg(mensagem.toString()); //conteudo do e-mail
    email.setAuthentication("realimoveis@hslife.com.br", "real123");
    email.setCharset("UTF8");
    //email.setSmtpPort(465);
    //email.setSSL(true);
    //email.setTLS(true);

    // envia email
    email.send();
}

From source file:com.flagleader.builder.dialogs.s.java

protected void okPressed() {
    try {// w  w w  .jav a2  s. c  o  m
        HtmlEmail localHtmlEmail = new HtmlEmail();
        localHtmlEmail.setHostName(BuilderConfig.getInstance().getEmailServer());
        localHtmlEmail.addTo("tech@flagleader.com", "VRS");
        localHtmlEmail.setAuthentication(BuilderConfig.getInstance().getEmailUser(),
                BuilderConfig.getInstance().getEmailPasswd());
        localHtmlEmail.setFrom(this.f.getText(), this.f.getText());
        localHtmlEmail.setSubject(this.c.getText() + "request rule builder license.");
        localHtmlEmail.setCharset("UTF-8");
        StringBuffer localStringBuffer = new StringBuffer();
        localStringBuffer.append("user:" + this.d.getText()).append(FileUtil.newline);
        localStringBuffer.append("mobile:" + this.e.getText()).append(FileUtil.newline);
        localStringBuffer.append("company:" + this.c.getText()).append(FileUtil.newline);
        localStringBuffer.append("checkCode:" + this.b).append(FileUtil.newline);
        localStringBuffer.append(this.a.getText());
        localHtmlEmail.setHtmlMsg(localStringBuffer.toString());
        localHtmlEmail.setMsg(localStringBuffer.toString());
        localHtmlEmail.send();
        super.okPressed();
    } catch (Exception localException) {
        MessageDialog.openError(null, "", localException.getMessage());
    }
}

From source file:com.itcs.commons.email.impl.RunnableSendHTMLEmail.java

public void run() {
    Logger.getLogger(RunnableSendHTMLEmail.class.getName()).log(Level.INFO,
            "executing Asynchronous task RunnableSendHTMLEmail");
    try {/*from w  w  w  . ja  v  a  2  s  .  c om*/
        HtmlEmail email = new HtmlEmail();
        email.setCharset("utf-8");
        email.setMailSession(getSession());
        for (String dir : to) {
            email.addTo(dir);
        }
        if (cc != null) {
            for (String ccEmail : cc) {
                email.addCc(ccEmail);
            }
        }
        if (cco != null) {
            for (String ccoEmail : cco) {
                email.addBcc(ccoEmail);
            }
        }
        email.setSubject(subject);
        // set the html message
        email.setHtmlMsg(body);
        email.setFrom(getSession().getProperties().getProperty(Email.MAIL_SMTP_FROM, Email.MAIL_SMTP_USER),
                getSession().getProperties().getProperty(Email.MAIL_SMTP_FROMNAME, Email.MAIL_SMTP_USER));
        // set the alternative message
        email.setTextMsg("Si ve este mensaje, significa que su cliente de correo no permite mensajes HTML.");
        // send the email
        if (attachments != null) {
            addAttachments(email, attachments);
        }
        email.send();
        Logger.getLogger(RunnableSendHTMLEmail.class.getName()).log(Level.INFO,
                "Email sent successfully to:{0} cc:{1} bcc:{2}",
                new Object[] { Arrays.toString(to), Arrays.toString(cc), Arrays.toString(cco) });
    } catch (EmailException e) {
        Logger.getLogger(RunnableSendHTMLEmail.class.getName()).log(Level.SEVERE,
                "EmailException Error sending email... with properties:\n" + session.getProperties(), e);
    }
}

From source file:br.com.atmatech.sac.controller.Email.java

public void emailAtendimento(String smtp, String user, String password, Integer porta, Boolean ssl, Boolean tls,
        String emailto, String emailfrom, String solicitante, String nchamado, String razao, String data,
        String solicitacao, String realizacao, String tecnico, String imagem)
        throws EmailException, MalformedURLException {
    System.err.println(smtp + ":\n" + user + ":\n" + password + ":\n" + porta + ":\n" + ssl + ":\n" + tls
            + ":\n" + emailto + ":\n" + emailfrom + ":\n" + solicitante + ":\n" + nchamado + ":\n" + razao
            + ":\n" + data + ":\n" + solicitacao + ":\n" + realizacao + ":\n" + tecnico + ":\n" + imagem);
    HtmlEmail email = new HtmlEmail();
    // SimpleEmail email = new SimpleEmail();
    email.setHostName(smtp); // o servidor SMTP para envio do e-mail
    email.addTo(emailto); //destinatrio
    email.setFrom(emailfrom); // remetente        
    email.setSubject("Aviso de Atendimento - Suporte");
    // configura a mensagem para o formato HTML        
    email.setHtmlMsg(/*from ww w .  ja  va  2s  . c om*/
            "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">Prezado(a)Senhor(a).<br>"
                    + "<b>" + solicitante + "</b><p>" + "Informamos que o protocolo nmero&#013<b> " + nchamado
                    + "</b> foi finalizado por nossa Central de Suporte.<p>" + "Cliente<br>" + "" + razao
                    + "<p>" + "Data<br>" + "" + data + "<p>" + "Descrio do Problema<br>" + "" + solicitacao
                    + "<p>" + "Soluo<br>" + "" + realizacao + "<p>" + "Atendente<br>" + "" + tecnico + "<p>"
                    + "<b>Atenciosamente</b> Suporte Atmatech<p><p>" + imagem + " </html>");
    //email.setMsg("Teste");
    email.setAuthentication(user, password);
    email.setSmtpPort(porta);
    email.setSSL(ssl);
    email.setTLS(tls);
    email.send();
}