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

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

Introduction

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

Prototype

public void setHostName(final String aHostName) 

Source Link

Document

Set the hostname of the outgoing mail server.

Usage

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

/**
 * Enviar o email para a lista de usarios especificados
 * @param emails/*from  w w w  .j a  v a2 s  . 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:gribbit.util.SendEmail.java

/** Send an email. Don't forget to use fully-qualified URLs in the message body. */
public static void sendEmail(final String toName, final String to, final String subject,
        final DataModel message, final String messagePlainText) {
    // Queue sending of email in a new thread
    GribbitServer.vertx.executeBlocking(future -> {
        if (GribbitProperties.SMTP_SERVER == null || GribbitProperties.SEND_EMAIL_ADDRESS == null
                || GribbitProperties.SEND_EMAIL_PASSWORD == null || GribbitProperties.SEND_EMAIL_ADDRESS == null
                || GribbitProperties.SEND_EMAIL_NAME == null) {
            throw new RuntimeException("SMTP is not fully configured in the properties file");
        }/*from ww w  .jav a 2 s  .co m*/

        String fullEmailAddr = "\"" + toName + "\" <" + to + ">";
        try {
            HtmlEmail email = new ImageHtmlEmail();
            email.setDebug(false);

            email.setHostName(GribbitProperties.SMTP_SERVER);
            email.setSmtpPort(GribbitProperties.SMTP_PORT);
            email.setAuthenticator(new DefaultAuthenticator(GribbitProperties.SEND_EMAIL_ADDRESS,
                    GribbitProperties.SEND_EMAIL_PASSWORD));
            email.setStartTLSRequired(true);

            email.addTo(to, toName);
            email.setFrom(GribbitProperties.SEND_EMAIL_ADDRESS, GribbitProperties.SEND_EMAIL_NAME);
            email.setSubject(subject);
            email.setHtmlMsg(message.toString());
            email.setTextMsg(messagePlainText);

            email.send();

            Log.info("Sent email to " + fullEmailAddr + " : " + subject);

        } catch (EmailException e) {
            Log.exception("Failure while trying to send email to " + fullEmailAddr + " : " + subject, e);
        }
        future.complete();

    }, res -> {
        if (res.failed()) {
            Log.error("Exception while trying to send email");
        }
    });
}

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

/**
 * ??/*from www. ja  va2s .  c om*/
 *
 * @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:com.zxy.commons.email.MailMessageUtils.java

/**
 * HtmlEmail/* ww  w .  j a  v a2  s  .  c o  m*/
 * 
 * @return HtmlEmail
 */
@SuppressWarnings("PMD.EmptyCatchBlock")
private static HtmlEmail getEmail() {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(SMTP_HOST);
    email.setSmtpPort(SMTP_PORT);
    if (StringUtils.isNotBlank(SMTP_USERNAME) && StringUtils.isNotBlank(SMTP_PASSWORD)) {
        email.setAuthentication(SMTP_USERNAME, SMTP_PASSWORD);
    }
    try {
        Session session = email.getMailSession();
        session.getProperties().setProperty("mail.smtp.ehlo", "true");
    } catch (EmailException e) {
        // do nothing
    }
    if (StringUtils.isNotBlank(SMTP_MAIL_CHARSET)) {
        email.setCharset(SMTP_MAIL_CHARSET);
    }
    return email;
}

From source file:org.meruvian.yama.webapi.config.EmailConfig.java

@Bean
@Scope("prototype")
public HtmlEmail email() throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(props.getProperty("host"));
    email.setSmtpPort(props.getProperty("port", Integer.class, 0));
    email.setAuthentication(props.getProperty("username"), props.getProperty("password"));
    email.setFrom(props.getProperty("from_email"), props.getProperty("from_alias"));
    email.setSSLOnConnect(props.getProperty("ssl", Boolean.class, false));
    email.setStartTLSEnabled(props.getProperty("tls", Boolean.class, false));

    return email;
}

From source file:com.fatecib.projetoemail.servlets.DAO.Enviaremail2.java

private void enviar(Email em, ConfiguracaoSQL conf, String destinatario) throws EmailException {
    try {//from   w  w w . j  a va 2 s  . co m
        HtmlEmail email = new HtmlEmail();
        email.setHostName(conf.getEMAILHOST());
        email.setSmtpPort(conf.getPORTASMTP());
        email.setAuthenticator(new DefaultAuthenticator(conf.getUsuario(), conf.getSENHA()));
        email.setSSL(true);
        email.setFrom(conf.getUsuario());
        email.setSubject(em.getTitulo());
        email.setHtmlMsg(em.getConteudo());
        // set the alternative message
        email.setTextMsg("Email enviado com sucesso");
        email.addTo(destinatario);
        email.send();
    } catch (Exception e) {
        throw e;
    }

}

From source file:libs.BuildMail.java

private void sendMail(String address, String subject, String msg) {
    try {//from  w  ww . j a  v a2 s.co m
        HtmlEmail email = new HtmlEmail();
        email.setHostName("smtp.ufpa.br");
        email.setSmtpPort(25);
        email.setAuthenticator(new DefaultAuthenticator("david.lopes@icen.ufpa.br", "spiderteste"));
        //email.setSSLOnConnect(); TODO verficar a possiblidade de uso SMTP
        email.setFrom("david.lopes@icen.ufpa.br");
        email.setSubject(subject);
        email.addTo(address);
        email.setHtmlMsg(msg);
        email.send();

    } catch (EmailException error) {
        System.out.println("Email error: check your log file" + error.getMessage());
    }
}

From source file:io.mif.labanorodraugai.services.EmailService.java

private void setUpHtmlEmail(HtmlEmail email) throws EmailException {
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);/*from   w  w  w . j a  v  a2  s.  c  o m*/
    email.setAuthenticator(new DefaultAuthenticator("labanorodraugaibendrija@gmail.com", "labanoro123"));
    email.setSSLOnConnect(true);
    email.setCharset("UTF-8");
    email.setFrom("labanorodraugaibendrija@gmail.com", "Labanoro draugai");
}

From source file:com.elexcode.emailservicelibrary.service.EmailSenderServiceImpl.java

@Override
public void sendEmail(EmailObject emailObject) throws Exception {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(host);
    email.setSmtpPort(port);/*from www  . j  av  a  2 s  .c  o m*/
    email.setAuthentication(username, password);
    for (String recipient : emailObject.getRecipients()) {
        email.addTo(recipient);
    }
    email.setFrom(username);
    email.setSubject(emailObject.getSubject());
    if (checkStringNotNullNotEmpty(emailObject.getHtmlMsg())) {
        email.setHtmlMsg(emailObject.getHtmlMsg());
    } else {
        if (checkStringNotNullNotEmpty(emailObject.getMessage())) {
            email.setTextMsg(emailObject.getMessage());
        }
    }
    email.setDebug(false);
    email.setTLS(true);
    email.setSSL(true);
    email.send();
}

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

@Override
public void send(Mail mail) throws Exception {
    try {/*from   w ww. j  a  va2s . c om*/
        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!");
    }
}