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

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

Introduction

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

Prototype

public Email addTo(final String... emails) throws EmailException 

Source Link

Document

Add a list of TO recipients to the email.

Usage

From source file:com.ning.billing.util.email.DefaultEmailSender.java

private void sendEmail(final List<String> to, final List<String> cc, final String subject, final Email email)
        throws EmailApiException {
    try {//from  ww  w.  j ava  2  s .com
        email.setSmtpPort(config.getSmtpPort());
        if (config.useSmtpAuth()) {
            email.setAuthentication(config.getSmtpUserName(), config.getSmtpPassword());
        }
        email.setHostName(config.getSmtpServerName());
        email.setFrom(config.getDefaultFrom());

        email.setSubject(subject);

        if (to != null) {
            for (final String recipient : to) {
                email.addTo(recipient);
            }
        }

        if (cc != null) {
            for (final String recipient : cc) {
                email.addCc(recipient);
            }
        }

        email.setSSL(config.useSSL());

        log.info("Sending email to {}, cc {}, subject {}", new Object[] { to, cc, subject });
        email.send();
    } catch (EmailException ee) {
        throw new EmailApiException(ee, ErrorCode.EMAIL_SENDING_FAILED);
    }
}

From source file:com.swissbit.ifttt.IFTTTConfgurationImpl.java

/** {@inheritDoc} */
@Override//from   www. ja v a  2s  .  co m
public void trigger() {
    LOGGER.debug("IFTTT Email is getting sent...");

    final List<String> tags = this.retrieveHashtags(this.m_hashTags);

    if (tags.size() == 0) {
        return;
    }

    if (tags.size() > 0) {
        for (final String tag : tags) {
            try {
                final Email email = new SimpleEmail();
                email.setHostName(this.m_smtpHost);
                email.setSmtpPort(this.m_smtpPort);
                email.setAuthenticator(new DefaultAuthenticator(this.m_smtpUsername, this.m_smtpPassword));
                email.setSSL(true);
                email.setFrom(this.m_smtpUsername);
                email.setSubject(tag);
                email.setMsg("This is a test mail ... :-)");
                email.addTo(TRIGGER_EMAIL);
                email.send();
            } catch (final EmailException e) {
                LOGGER.error(Throwables.getStackTraceAsString(e));
            }
        }
    }
    LOGGER.debug("IFTTT Email is sent...Done");
}

From source file:in.flipbrain.controllers.BaseController.java

protected void sendEmail(String to, String subject, String body) throws EmailException {
    logger.debug("Sending email to " + to + "\nSubject: " + subject + "\nMessage: " + body);
    if ("true".equalsIgnoreCase(getConfigValue(Constants.EM_FAKE_SEND)))
        return;// w  w w .jav  a 2  s.  com

    Email email = new SimpleEmail();
    email.setHostName(getConfigValue("smtp.host"));
    email.setSmtpPort(Integer.parseUnsignedInt(getConfigValue("smtp.port")));
    email.setAuthenticator(
            new DefaultAuthenticator(getConfigValue("smtp.user"), getConfigValue("smtp.password")));
    email.setSSLOnConnect(Boolean.parseBoolean(getConfigValue("smtp.ssl")));
    email.setFrom(getConfigValue("smtp.sender"));
    email.setSubject(subject);
    email.setMsg(body);
    email.addTo(to);
    email.send();
}

From source file:json.JsonUI.java

public void enviandoEmail() {
    Properties login = new Properties();
    String properties = "json/login.properties";
    try {//w  w w. j  av a 2  s  .com
        InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(properties);
        login.load(stream);
    } catch (IOException ex) {
        System.out.println("Erro: " + ex.getMessage());
    }
    String username = login.getProperty("hotmail.username");
    String password = login.getProperty("hotmail.password");

    try {
        Email email = new SimpleEmail();
        email.setHostName("smtp.live.com");
        email.setSmtpPort(587);
        email.setStartTLSRequired(true);
        email.setAuthenticator(new DefaultAuthenticator(username, password));
        email.setFrom(username);
        email.setSubject(assuntoEmail.getText());
        email.setMsg(jTextAreaMsgEmail.getText());
        email.addTo(emailDestino.getText());
        email.setDebug(true);
        email.send();
        aviso.setText("Mensagem enviada.");
    } catch (EmailException ex) {
        System.out.println("Erro: " + ex.getMessage());
    }

}

From source file:com.mirth.connect.server.util.SMTPConnection.java

public void send(String toList, String ccList, String from, String subject, String body) throws EmailException {
    Email email = new SimpleEmail();
    email.setHostName(host);/*ww  w.j av a2 s.c o  m*/
    email.setSmtpPort(Integer.parseInt(port));
    email.setSocketConnectionTimeout(socketTimeout);
    email.setDebug(true);

    if (useAuthentication) {
        email.setAuthentication(username, password);
    }

    if (StringUtils.equalsIgnoreCase(secure, "TLS")) {
        email.setTLS(true);
    } else if (StringUtils.equalsIgnoreCase(secure, "SSL")) {
        email.setSSL(true);
    }

    for (String to : StringUtils.split(toList, ",")) {
        email.addTo(to);
    }

    if (StringUtils.isNotEmpty(ccList)) {
        for (String cc : StringUtils.split(ccList, ",")) {
            email.addCc(cc);
        }
    }

    email.setFrom(from);
    email.setSubject(subject);
    email.setMsg(body);
    email.send();
}

From source file:at.treedb.util.Mail.java

/**
 * //from w ww  .  j a  v a  2  s  . com
 * @param sendTo
 * @param subject
 * @param message
 * @throws EmailException
 */
public void sendMail(String mailTo, String mailFrom, String subject, String message) throws Exception {
    Objects.requireNonNull(mailTo, "Mail.sendMail(): mailTo can not be null!");
    Objects.requireNonNull(mailFrom, "Mail.sendMail(): mailFrom can not be null!");
    Objects.requireNonNull(subject, "Mail.sendMail(): subject can not be null!");
    Objects.requireNonNull(message, "Mail.sendMail(): message can not be null!");
    Email email = new SimpleEmail();
    email.setHostName(smtpHost);

    email.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword));
    if (transportSecurity == TransportSecurity.SSL) {
        email.setSSLOnConnect(true);
        email.setSSLCheckServerIdentity(false);
    } else if (transportSecurity == TransportSecurity.STARTTLS) {
        email.setStartTLSRequired(true);
    }
    email.setSmtpPort(smtpPort);
    email.setFrom(mailFrom);
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(mailTo);
    email.send();
}

From source file:com.mirth.connect.connectors.smtp.SmtpSenderService.java

@Override
public Object invoke(String channelId, String method, Object object, String sessionId) throws Exception {
    if (method.equals("sendTestEmail")) {
        SmtpDispatcherProperties props = (SmtpDispatcherProperties) object;

        String host = replacer.replaceValues(props.getSmtpHost(), channelId);
        String portString = replacer.replaceValues(props.getSmtpPort(), channelId);

        int port = -1;
        try {/*from  www .  j a  v a 2  s  .  c om*/
            port = Integer.parseInt(portString);
        } catch (NumberFormatException e) {
            return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE,
                    "Invalid port: \"" + portString + "\"");
        }

        String secure = props.getEncryption();

        boolean authentication = props.isAuthentication();

        String username = replacer.replaceValues(props.getUsername(), channelId);
        String password = replacer.replaceValues(props.getPassword(), channelId);
        String to = replacer.replaceValues(props.getTo(), channelId);
        String from = replacer.replaceValues(props.getFrom(), channelId);

        Email email = new SimpleEmail();
        email.setDebug(true);
        email.setHostName(host);
        email.setSmtpPort(port);

        if ("SSL".equalsIgnoreCase(secure)) {
            email.setSSL(true);
        } else if ("TLS".equalsIgnoreCase(secure)) {
            email.setTLS(true);
        }

        if (authentication) {
            email.setAuthentication(username, password);
        }

        email.setSubject("Mirth Connect Test Email");

        try {
            for (String toAddress : StringUtils.split(to, ",")) {
                email.addTo(toAddress);
            }

            email.setFrom(from);
            email.setMsg(
                    "Receipt of this email confirms that mail originating from this Mirth Connect Server is capable of reaching its intended destination.\n\nSMTP Configuration:\n- Host: "
                            + host + "\n- Port: " + port);

            email.send();
            return new ConnectionTestResponse(ConnectionTestResponse.Type.SUCCESS,
                    "Sucessfully sent test email to: " + to);
        } catch (EmailException e) {
            return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, e.getMessage());
        }
    }

    return null;
}

From source file:de.eod.jliki.users.jsfbeans.UserRegisterBean.java

/**
 * Adds a new user to the jLiki database.<br/>
 *///from  w w  w  . j  a  v a  2s . com
public final void addNewUser() {
    final User newUser = new User(this.username, this.password, this.email, this.firstname, this.lastname);
    final String userHash = UserDBHelper.addUserToDB(newUser);

    if (userHash == null) {
        Messages.addFacesMessage(null, FacesMessage.SEVERITY_ERROR, "message.user.register.failed",
                this.username);
        return;
    }

    UserRegisterBean.LOGGER.debug("Adding user: " + newUser.toString());

    final FacesContext fc = FacesContext.getCurrentInstance();
    final HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
    final ResourceBundle mails = ResourceBundle.getBundle("de.eod.jliki.EMailMessages",
            fc.getViewRoot().getLocale());
    final String activateEMailTemplate = mails.getString("user.registration.email");
    final StringBuffer url = request.getRequestURL();
    final String serverUrl = url.substring(0, url.lastIndexOf("/"));

    UserRegisterBean.LOGGER.debug("Generated key for user: \"" + userHash + "\"");

    final String emsLink = serverUrl + "/activate.xhtml?user=" + newUser.getName() + "&key=" + userHash;
    final String emsLikiName = ConfigManager.getInstance().getConfig().getPageConfig().getPageName();
    final String emsEMailText = MessageFormat.format(activateEMailTemplate, emsLikiName, this.firstname,
            this.lastname, this.username, emsLink);

    final String emsHost = ConfigManager.getInstance().getConfig().getEmailConfig().getHostname();
    final int emsPort = ConfigManager.getInstance().getConfig().getEmailConfig().getPort();
    final String emsUser = ConfigManager.getInstance().getConfig().getEmailConfig().getUsername();
    final String emsPass = ConfigManager.getInstance().getConfig().getEmailConfig().getPassword();
    final boolean emsTSL = ConfigManager.getInstance().getConfig().getEmailConfig().isUseTLS();
    final String emsSender = ConfigManager.getInstance().getConfig().getEmailConfig().getSenderAddress();

    final Email activateEmail = new SimpleEmail();
    activateEmail.setHostName(emsHost);
    activateEmail.setSmtpPort(emsPort);
    activateEmail.setAuthentication(emsUser, emsPass);
    activateEmail.setTLS(emsTSL);
    try {
        activateEmail.setFrom(emsSender);
        activateEmail.setSubject("Activate jLiki Account");
        activateEmail.setMsg(emsEMailText);
        activateEmail.addTo(this.email);
        activateEmail.send();
    } catch (final EmailException e) {
        UserRegisterBean.LOGGER.error("Sending activation eMail failed!", e);
        return;
    }

    this.username = "";
    this.password = "";
    this.confirm = "";
    this.email = "";
    this.firstname = "";
    this.lastname = "";
    this.captcha = "";
    this.termsOfUse = false;
    this.success = true;

    Messages.addFacesMessage(null, FacesMessage.SEVERITY_INFO, "message.user.registered", this.username);
}

From source file:com.music.service.EmailService.java

@Async
public void send(EmailDetails details) {
    if (details.getSubject() == null || !BooleanUtils
            .xor(ArrayUtils.toArray(details.getMessage() != null, details.getMessageTemplate() != null))) {
        throw new IllegalStateException(
                "Either subject or subjectKey / either template/message/messageKey should be specified");
    }/*from   w  ww.  ja  v  a 2 s  .c o m*/
    Validate.notBlank(details.getFrom());

    Email email = createEmail(details.isHtml());
    String subject = constructSubject(details);
    email.setSubject(subject);

    String emailMessage = constructEmailMessages(details);

    try {
        if (details.isHtml()) {
            ((HtmlEmail) email).setHtmlMsg(emailMessage);
        } else {
            email.setMsg(emailMessage);
        }

        for (String to : details.getTo()) {
            email.addTo(to);
        }
        email.setFrom(details.getFrom());

        email.send();
    } catch (EmailException ex) {
        logger.error("Exception occurred when sending email to " + details.getTo(), ex);
    }
}

From source file:com.northernwall.hadrian.workItem.email.EmailWorkItemSender.java

private void emailWorkItem(String subject, String body) {
    try {/*  w ww . j  a va2s.c o m*/
        if (emailTos.isEmpty()) {
            return;
        }
        Email email = new SimpleEmail();
        if (smtpHostname != null) {
            email.setHostName(smtpHostname);
        }
        email.setSmtpPort(smtpPort);
        if (smtpUsername != null && smtpPassword != null) {
            email.setAuthenticator(new DefaultAuthenticator(smtpUsername, smtpPassword));
        }
        email.setSSLOnConnect(smtpSsl);
        email.setFrom(emailFrom);
        email.setSubject(subject);
        email.setMsg(body);
        for (String emailTo : emailTos) {
            email.addTo(emailTo);
        }
        email.send();

        if (emailTos.size() == 1) {
            logger.info("Emailing work item to {} with subject {}", emailTos.get(0), subject);
        } else {
            logger.info("Emailing work item to {} and {} other email addresses with subject {}",
                    emailTos.get(0), (emailTos.size() - 1), subject);
        }
    } catch (EmailException ex) {
        throw new RuntimeException("Failure emailing work item, {}", ex);
    }
}