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

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

Introduction

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

Prototype

public Email addTo(final String email, final String name) throws EmailException 

Source Link

Document

Add a recipient TO to the email using the specified address and the specified personal name.

Usage

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");
        }/*ww w. j  a  va2  s .c  o  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:de.maklerpoint.office.Marketing.Tools.SendNewsletter.java

/**
 * //  www. j a va2  s.c om
 * @param nl
 * @param sub
 * @throws EmailException
 */

public static void sendNewsletter(NewsletterObj nl, NewsletterSubscriberObj[] sub) throws EmailException {

    for (int i = 0; i < sub.length; i++) {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(Config.get("mailHost", null));

        if (Config.getConfigInt("mailPort", 25) != 25)
            email.setSmtpPort(Config.getConfigInt("mailPort", 25));

        if (Config.getConfigBoolean("mailAuth", true))
            email.setAuthentication(Config.get("mailUser", null), Config.get("mailPassword", null));

        email.setFrom(nl.getSenderMail(), nl.getSender());
        email.setSubject(nl.getSubject());
        email.setHtmlMsg(nl.getText());
        email.setTextMsg("Ihr E-Mail Client untersttzt keine HTML Nachrichten.");

        email.addTo(sub[i].getEmail(), sub[i].getName());
        email.send();
    }
}

From source file:mailbox.EmailHandler.java

private static void reply(IMAPMessage origin, String username, String emailAddress, String msg) {
    final HtmlEmail email = new HtmlEmail();

    try {//from w w w  . j  a  v  a  2  s .co m
        email.setFrom(Config.getEmailFromSmtp(), Config.getSiteName());
        email.addTo(emailAddress, username);
        String subject;
        if (!origin.getSubject().toLowerCase().startsWith("re:")) {
            subject = "Re: " + origin.getSubject();
        } else {
            subject = origin.getSubject();
        }
        email.setSubject(subject);
        email.setTextMsg(msg);
        email.setCharset("utf-8");
        email.setSentDate(new Date());
        email.addHeader("In-Reply-To", origin.getMessageID());
        email.addHeader("References", origin.getMessageID());
        Mailer.send(email);
        String escapedTitle = email.getSubject().replace("\"", "\\\"");
        String logEntry = String.format("\"%s\" %s", escapedTitle, email.getToAddresses());
        play.Logger.of("mail").info(logEntry);
    } catch (Exception e) {
        Logger.warn("Failed to send an email: " + email + "\n" + ExceptionUtils.getStackTrace(e));
    }
}

From source file:com.zxy.commons.email.MailMessageUtils.java

/**
 * smtp??/* ww  w .  j  av a 2s .c  om*/
 * 
 * @param subject subject
 * @param htmlBody htmlBody
 * @param properties properties
 * @param from from
 * @param toList toList
 * @param ccList ccList
 * @param bccList bccList
 * @param embedUrls 
 * @throws EmailException EmailException
 */
@SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseStringBufferForStringAppends" })
public static void sendMail(String subject, String htmlBody, Map<String, String> properties, String from,
        List<String> toList, List<String> ccList, List<String> bccList, Map<String, URL> embedUrls)
        throws EmailException {
    HtmlEmail htmlEmail = getEmail();
    // from?
    if (!Strings.isNullOrEmpty(from)) {
        Address fromMailbox = parseMailbox(from);
        if (fromMailbox != null && StringUtils.isNotBlank(from)) {
            htmlEmail.setFrom(fromMailbox.getAddress(), fromMailbox.getName());
        }
    }
    // to?
    if (toList != null && !toList.isEmpty()) {
        for (String to : toList) {
            if (StringUtils.isNotBlank(to)) {
                Address toMailbox = parseMailbox(to);
                htmlEmail.addTo(toMailbox.getAddress(), toMailbox.getName());
            }
        }
    }
    // cc?
    if (ccList != null && !ccList.isEmpty()) {
        for (String cc : ccList) {
            if (StringUtils.isNotBlank(cc)) {
                Address ccMailbox = parseMailbox(cc);
                htmlEmail.addCc(ccMailbox.getAddress(), ccMailbox.getName());
            }
        }
    }
    // bcc?
    if (bccList != null && !bccList.isEmpty()) {
        for (String bcc : bccList) {
            if (StringUtils.isNotBlank(bcc)) {
                Address bccMailbox = parseMailbox(bcc);
                htmlEmail.addBcc(bccMailbox.getAddress(), bccMailbox.getName());
            }
        }
    }
    // 
    htmlEmail.setSubject(subject);
    htmlEmail.setHtmlMsg(htmlBody);
    htmlEmail.setSentDate(new Date());
    // 
    if (properties != null) {
        htmlEmail.setHeaders(properties);
    }
    // 
    if (embedUrls != null && !embedUrls.isEmpty()) {
        for (Map.Entry<String, URL> entry : embedUrls.entrySet()) {
            String cid = entry.getKey();
            URL url = entry.getValue();
            String fileName = StringUtils.substringAfterLast(url.getPath(), "/");
            if (StringUtils.isBlank(fileName)) {
                fileName = cid;
            } else {
                fileName += IdUtils.genStringId();
            }
            htmlEmail.embed(new URLDataSource(url), fileName, cid);
        }
    }
    htmlEmail.send();
}

From source file:br.vn.Model.Filtros.Email.java

public void enviarHatml(String emailcliente) throws MalformedURLException {

    try {/*  ww w .  j ava2 s  .  co m*/
        // Criar a mensagem de e-mail
        HtmlEmail email = new HtmlEmail();
        email.setHostName("org.apache.commons");

        email.addTo("jdoe@somewhere.org", "John Doe");
        email.setFrom("me@apache.org", "Me");
        email.setSubject("Test email with inline image");

        //incorporar a imagem e obter o ID de contedo
        URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
        String cid = email.embed(url, "Apache logo");

        // definir a mensagem HTML
        email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>");

        // definir a mensagem alternativa
        email.setTextMsg("Your email client does not support HTML messages");

        // enviar o e-mail
        email.send();
    } catch (EmailException ex) {
        Logger.getLogger(Email.class.getName()).log(Level.SEVERE, null, ex);
    }
}

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

protected void okPressed() {
    try {//w w  w  .j a va 2s  .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:actors.ValidationEmailSender.java

@Override
public void onReceive(Object object) {
    if (!(object instanceof Email)) {
        return;//w w  w .  j  a  v  a  2  s.  c o m
    }

    Email email = (Email) object;

    final HtmlEmail htmlEmail = new HtmlEmail();

    try {
        htmlEmail.setFrom(Config.getEmailFromSmtp(), utils.Config.getSiteName());
        htmlEmail.addTo(email.email, email.user.name);
        htmlEmail.setSubject(Messages.get("emails.validation.email.title", utils.Config.getSiteName()));
        htmlEmail.setHtmlMsg(getMessage(email.confirmUrl));
        htmlEmail.setCharset("utf-8");
        Mailer.send(htmlEmail);
        String escapedTitle = htmlEmail.getSubject().replace("\"", "\\\"");
        String logEntry = String.format("\"%s\" %s", escapedTitle, htmlEmail.getToAddresses());
        play.Logger.of("mail").info(logEntry);
    } catch (Exception e) {
        Logger.warn("Failed to send a notification: " + email + "\n" + ExceptionUtils.getStackTrace(e));
    }
}

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  .  j av  a2  s.  co  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();

}

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

public String sendEmail() throws EmailException {
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext()
            .getRequestParameterMap();/*from  w ww  .j  a va  2 s .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:com.duroty.application.open.manager.OpenManager.java

/**
 * DOCUMENT ME!//from   ww w. ja v a 2s. com
 *
 * @param session DOCUMENT ME!
 * @param repositoryName DOCUMENT ME!
 * @param from DOCUMENT ME!
 * @param to DOCUMENT ME!
 * @param subject DOCUMENT ME!
 * @param body DOCUMENT ME!
 *
 * @throws MailException DOCUMENT ME!
 */
private void sendData(Session msession, InternetAddress from, InternetAddress to, String username,
        String password, String signature) throws Exception {
    try {
        HtmlEmail email = new HtmlEmail();
        email.setMailSession(msession);

        email.setFrom(from.getAddress(), from.getPersonal());
        email.addTo(to.getAddress(), to.getPersonal());

        email.setSubject("Duroty System");
        email.setHtmlMsg("<p>Username: <b>" + username + "</b></p><p>Password: " + password + "<b></b></p><p>"
                + signature + "</p>");

        email.setCharset(MimeUtility.javaCharset(Charset.defaultCharset().displayName()));

        email.send();
    } finally {
    }
}