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

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

Introduction

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

Prototype

public Email addCc(final String email) throws EmailException 

Source Link

Document

Add a recipient CC to the email.

Usage

From source file:com.duroty.utils.mail.MessageUtilities.java

/**
 * DOCUMENT ME!//from   w  ww .  ja va2  s  .  c  o m
 *
 * @param args DOCUMENT ME!
 *
 * @throws UnsupportedEncodingException DOCUMENT ME!
 * @throws EmailException DOCUMENT ME!
 * @throws MessagingException DOCUMENT ME!
 */
public static void main(String[] args) throws UnsupportedEncodingException, EmailException, MessagingException {
    InternetAddress[] aux1 = new InternetAddress[5];
    aux1[0] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[1] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[2] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[3] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[4] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");

    System.out.println(MessageUtilities.decodeAddressesEmail(aux1));

    HtmlEmail email = new HtmlEmail();
    email.setHostName("10.0.0.68");
    email.setFrom("duroty@iigov.net");
    email.addReplyTo("duroty@iigov.net");

    email.addTo("cagao@ii.org");

    email.addCc("raul1@iigov.org");
    email.addCc("raul2@iigov.org");
    email.addCc("raul3@iigov.org");
    email.addCc("raul4@iigov.org");
    email.addCc("raul5@iigov.org");

    email.addBcc("caca1@iigov.org");

    email.setHtmlMsg("<html>la merda fa pudor</html>");

    email.buildMimeMessage();

    MimeMessage mime = email.getMimeMessage();

    System.out.println(MessageUtilities.decodeAddressesEmail(mime.getAllRecipients()));
}

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  ww . ja  v a 2 s  . c o  m*/
        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:de.maklerpoint.office.Schnittstellen.Email.MultiEmailSender.java

public void send() throws EmailException {

    if (files != null && urls != null) {
        attachments = new EmailAttachment[files.length + urls.length];

        int cnt = 0;

        for (int i = 0; i < files.length; i++) {
            attachments[cnt] = new EmailAttachment();
            attachments[cnt].setPath(files[i].getPath());
            attachments[cnt].setName(files[i].getName());
            attachments[cnt].setDisposition(EmailAttachment.ATTACHMENT);

            cnt++;/*from www .j  a  va2s  .c  o  m*/
        }

        for (int i = 0; i < urls.length; i++) {
            attachments[cnt] = new EmailAttachment();
            attachments[cnt].setURL(urls[i]);
            attachments[cnt].setName(urls[i].getFile());
            attachments[cnt].setDisposition(EmailAttachment.ATTACHMENT);
            cnt++;
        }

    } else if (files != null) {
        attachments = new EmailAttachment[files.length];

        for (int i = 0; i < files.length; i++) {
            attachments[i] = new EmailAttachment();
            attachments[i].setPath(files[i].getPath());
            attachments[i].setName(files[i].getName());
            attachments[i].setDisposition(EmailAttachment.ATTACHMENT);
        }
    } else if (urls != null) {
        attachments = new EmailAttachment[urls.length];

        for (int i = 0; i < urls.length; i++) {
            attachments[i] = new EmailAttachment();
            attachments[i].setURL(urls[i]);
            attachments[i].setName(urls[i].getFile());
            attachments[i].setDisposition(EmailAttachment.ATTACHMENT);
        }
    }

    HtmlEmail email = new HtmlEmail();
    email.setHostName(Config.get("mailHost", ""));
    email.setSmtpPort(Config.getConfigInt("mailPort", 25));

    email.setTLS(Config.getConfigBoolean("mailTLS", false));
    email.setSSL(Config.getConfigBoolean("mailSSL", false));

    //email.setSslSmtpPort(Config.getConfigInt("emailPort", 25));
    email.setAuthenticator(
            new DefaultAuthenticator(Config.get("mailUsername", ""), Config.get("mailPassword", "")));

    email.setFrom(Config.get("mailSendermail", "info@example.de"), Config.get("mailSender", null));

    email.setSubject(subject);
    email.setHtmlMsg(body);
    email.setTextMsg(nohtmlmsg);

    for (int i = 0; i < adress.length; i++) {
        email.addTo(adress[i]);
    }

    if (cc != null) {
        for (int i = 0; i < cc.length; i++) {
            email.addCc(cc[i]);
        }
    }

    if (attachments != null) {
        for (int i = 0; i < attachments.length; i++) {
            email.attach(attachments[i]);
        }
    }

    email.send();

}

From source file:fr.gouv.culture.thesaurus.util.MailUtil.java

/**
 * /*from w w  w .  j a v a2 s.co  m*/
 * Envoi l'email.
 * 
 * @throws EmailException
 *             Erreur lors de l'envoi de l'email.
 */
public void send() throws EmailException {

    if (hostProperty == null) {
        log.error("Session email non initialise : envoi des emails impossible.");
        return;
    }

    HtmlEmail email = new HtmlEmail();
    email.setHostName(hostProperty);

    // To
    if (to != null) {
        for (String adresse : to) {
            email.addTo(adresse);
        }
    }

    // Cc
    if (cc != null) {
        for (String adresse : cc) {
            email.addCc(adresse);
        }
    }

    // Cci
    if (cci != null) {
        for (String adresse : cci) {
            email.addBcc(adresse);
        }
    }

    // Subject
    email.setSubject(subjectPrefix != null ? subjectPrefix + subject : subject);

    // From
    email.setFrom(StringUtils.isNotEmpty(from) ? from : defaultFrom);

    // Message & Html
    if (message != null) {
        email.setTextMsg(message);
    }
    if (html != null) {
        email.setHtmlMsg(html);
    }

    if (StringUtils.isNotEmpty(this.charset)) {
        email.setCharset(this.charset);
    }

    email.buildMimeMessage();

    // Attachments
    for (AttachmentBean attachement : attachments) {
        email.attach(attachement.getDataSource(), attachement.getName(), attachement.getDescription());
    }

    email.sendMimeMessage();

}

From source file:com.smi.travel.util.Mail.java

public String sendmailwithAttchfile(String sendTo, String subject, String content, String attachfile,
        String sendCc) throws EmailException {
    String result = "";
    boolean send = false;
    EmailAttachment attachment = new EmailAttachment();
    HtmlEmail email = new HtmlEmail();
    try {// www. ja v  a2  s .  c  o m
        if ((attachfile != null) && (!attachfile.equalsIgnoreCase(""))) {
            //attachment.setPath("C:\\Users\\Surachai\\Documents\\NetBeansProjects\\SMITravel\\test.txt");
            attachment.setPath(attachfile);
            attachment.setDisposition(EmailAttachment.ATTACHMENT);
            attachment.setDescription("file attachment");
            attachment.setName("text.txt");
            email.attach(attachment);
        }
        send = true;
    } catch (EmailException ex) {
        System.out.println("Email Exception");
        ex.printStackTrace();
        result = "fail";
    }
    if (send) {
        System.out.println(mail.getUsername() + mail.getPassword());
        email.setHostName(mail.getHostname());
        email.setSmtpPort(mail.getPort());
        email.setAuthentication(mail.getUsername(), mail.getPassword());
        email.setSSLOnConnect(true);
        email.setFrom(mail.getUsername());
        email.setSubject(subject);
        email.setHtmlMsg(content);
        String[] toSplit = sendTo.split("\\,");
        for (int i = 0; i < toSplit.length; i++) {
            System.out.println("Print toSplit" + toSplit[i]);
            email.addTo(toSplit[i]);
        }
        if (!sendCc.isEmpty()) {
            String[] ccSplit = sendCc.split("\\,");
            for (int i = 0; i < ccSplit.length; i++) {
                System.out.println("Print ccSplit" + ccSplit[i]);
                email.addCc(ccSplit[i]);
            }
        }
        email.send();

        result = "success";

    }
    return result;
}

From source file:nl.b3p.kaartenbalie.struts.Mailer.java

public ActionMessages send(ActionMessages errors)
        throws AddressException, MessagingException, IOException, Exception {

    HtmlEmail email = new HtmlEmail();

    String[] ds = null;/*from  w w  w  .  ja v a2  s  . c o  m*/
    if (mailTo != null && mailTo.trim().length() != 0) {
        ds = mailTo.split(",");
        for (int i = 0; i < ds.length; i++) {
            email.addTo(ds[i]);
        }
    }
    if (mailCc != null && mailCc.trim().length() != 0) {
        ds = mailCc.split(",");
        for (int i = 0; i < ds.length; i++) {
            email.addCc(ds[i]);
        }
    }
    if (mailBcc != null && mailBcc.trim().length() != 0) {
        ds = mailBcc.split(",");
        for (int i = 0; i < ds.length; i++) {
            email.addBcc(ds[i]);
        }
    }

    email.setFrom(mailFrom);
    email.setSubject(subject);
    email.setHostName(mailHost);

    if (isReturnReceipt()) {
        email.addHeader("Disposition-Notification-To", mailFrom);
    }

    if (attachmentName == null) {
        attachmentName = "attachment";
    }
    if (attachment != null) {
        URL attachUrl = null;
        try {
            attachUrl = new URL(attachment);
            email.attach(attachUrl, attachmentName, attachmentName);
        } catch (MalformedURLException mfue) {
        }
    }
    if (attachmentDataSource != null) {
        email.attach(attachmentDataSource, attachmentName, attachmentName);
    }

    email.setMsg(createHTML());

    // send the email
    email.send();

    return errors;
}

From source file:org.apache.unomi.plugins.mail.actions.SendMailAction.java

public int execute(Action action, Event event) {
    String from = (String) action.getParameterValues().get("from");
    String to = (String) action.getParameterValues().get("to");
    String cc = (String) action.getParameterValues().get("cc");
    String bcc = (String) action.getParameterValues().get("bcc");
    String subject = (String) action.getParameterValues().get("subject");
    String template = (String) action.getParameterValues().get("template");

    ST stringTemplate = new ST(template);
    stringTemplate.add("profile", event.getProfile());
    stringTemplate.add("event", event);
    // load your HTML email template
    String htmlEmailTemplate = stringTemplate.render();

    // define you base URL to resolve relative resource locations
    try {/*  www.j a v a  2  s  .c  om*/
        new URL("http://www.apache.org");
    } catch (MalformedURLException e) {
        //
    }

    // create the email message
    HtmlEmail email = new ImageHtmlEmail();
    // email.setDataSourceResolver(new DataSourceResolverImpl(url));
    email.setHostName(mailServerHostName);
    email.setSmtpPort(mailServerPort);
    email.setAuthenticator(new DefaultAuthenticator(mailServerUsername, mailServerPassword));
    email.setSSLOnConnect(mailServerSSLOnConnect);
    try {
        email.addTo(to);
        email.setFrom(from);
        if (cc != null && cc.length() > 0) {
            email.addCc(cc);
        }
        if (bcc != null && bcc.length() > 0) {
            email.addBcc(bcc);
        }
        email.setSubject(subject);

        // set the html message
        email.setHtmlMsg(htmlEmailTemplate);

        // set the alternative message
        email.setTextMsg("Your email client does not support HTML messages");

        // send the email
        email.send();
    } catch (EmailException e) {
        logger.error("Cannot send mail", e);
    }

    return EventService.NO_CHANGE;
}

From source file:tilda.utils.MailUtil.java

/**
 * /*w  ww  . j  a va2 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;
    }
}