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

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

Introduction

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

Prototype

public void setAuthentication(final String userName, final String password) 

Source Link

Document

Sets the userName and password if authentication is needed.

Usage

From source file:fr.gael.dhus.messaging.mail.MailServer.java

public void send(Email email, String to, String cc, String bcc, String subject) throws EmailException {
    email.setHostName(getSmtpServer());//from   w w w  .  j a  va  2s.c  om
    email.setSmtpPort(getPort());
    if (getUsername() != null) {
        email.setAuthentication(getUsername(), getPassword());
    }
    if (getFromMail() != null) {
        if (getFromName() != null)
            email.setFrom(getFromMail(), getFromName());
        else
            email.setFrom(getFromMail());
    }
    if (getReplyto() != null) {
        try {
            email.setReplyTo(ImmutableList.of(new InternetAddress(getReplyto())));
        } catch (AddressException e) {
            logger.error("Cannot configure Reply-to (" + getReplyto() + ") into the mail: " + e.getMessage());
        }
    }

    // Message configuration
    email.setSubject("[" + cfgManager.getNameConfiguration().getShortName() + "] " + subject);
    email.addTo(to);

    // Add CCed
    if (cc != null) {
        email.addCc(cc);
    }
    // Add BCCed
    if (bcc != null) {
        email.addBcc(bcc);
    }

    email.setStartTLSEnabled(isTls());
    try {
        email.send();
    } catch (EmailException e) {
        logger.error("Cannot send email: " + e.getMessage());
        throw e;
    }
}

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);/*from w w w  . j  a v a2  s  .  co  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:Control.CommonsMail.java

/**
 * Classe que envia E-amil/*w  w  w.  j  a  v  a  2 s .  c om*/
 * @throws EmailException
 */
public void enviaEmailSimples(String Msg) throws EmailException {

    Email email = new SimpleEmail();
    email.setDebug(true);
    email.setHostName("smtp.gmail.com"); // o servidor SMTP para envio do e-mail
    //email.setHostName("smtp.pharmapele.com.br"); // o servidor SMTP para envio do e-mail
    email.setSmtpPort(587);
    email.setSSLOnConnect(true);
    email.setStartTLSEnabled(true);
    email.setAuthentication("softwaredeveloperantony@gmail.com", "tony#020567");
    //email.setAuthentication("antony@pharmapele.com.br", "tony#020567");

    //email.setFrom("softwaredeveloperantony@gmail.com"); // remetente
    email.setFrom("antony@pharmapele.com.br"); // remetente
    email.setSubject("Exporta Estoque lojas"); // assunto do e-mail
    email.setMsg(Msg); //conteudo do e-mail
    email.addTo("antony@pharmapele.com.br", "Antony"); //destinatrio

    //email.sets(true);
    //email.setTLS(true);
    try {

        email.send();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * Adds a new user to the jLiki database.<br/>
 *///from www  .  ja  v  a2 s  .  c  o m
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.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 {/*w  w  w  .  j a  va  2  s .com*/
            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:com.music.service.EmailService.java

private Email createEmail(boolean html) {
    Email e = null;
    if (html) {/*from   w ww . j  a v  a  2s  . c  o  m*/
        e = new HtmlEmail();
    } else {
        e = new SimpleEmail();
    }
    e.setHostName(smtpHost);
    if (!StringUtils.isEmpty(smtpUser)) {
        e.setAuthentication(smtpUser, smtpPassword);
    }

    if (!StringUtils.isEmpty(smtpBounceEmail)) {
        e.setBounceAddress(smtpBounceEmail);
    }

    e.setTLS(true);
    e.setSmtpPort(587); //tls port
    e.setCharset("UTF8");
    //e.setDebug(true);

    return e;
}

From source file:com.qwazr.connectors.EmailConnector.java

public void sendEmail(Email email) throws EmailException {
    email.setHostName(hostname);/*from   ww  w.j av a 2 s.  co  m*/
    if (ssl != null)
        email.setSSLOnConnect(ssl);
    if (start_tls_enabled != null)
        email.setStartTLSEnabled(start_tls_enabled);
    if (start_tls_required != null)
        email.setStartTLSRequired(start_tls_required);
    if (port != null)
        email.setSmtpPort(port);
    if (username != null)
        email.setAuthentication(username, password);
    if (connection_timeout != null)
        email.setSocketConnectionTimeout(connection_timeout);
    if (timeout != null)
        email.setSocketTimeout(timeout);
    email.send();
}

From source file:com.qwazr.library.email.EmailConnector.java

public void sendEmail(final Email email) throws EmailException {
    email.setHostName(hostname);/*from ww  w  . j  av a2 s.c  o m*/
    if (ssl != null)
        email.setSSLOnConnect(ssl);
    if (start_tls_enabled != null)
        email.setStartTLSEnabled(start_tls_enabled);
    if (start_tls_required != null)
        email.setStartTLSRequired(start_tls_required);
    if (port != null)
        email.setSmtpPort(port);
    if (username != null)
        email.setAuthentication(username, password);
    if (connection_timeout != null)
        email.setSocketConnectionTimeout(connection_timeout);
    if (timeout != null)
        email.setSocketTimeout(timeout);
    email.send();
}

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

public void send(String toList, String ccList, String from, String subject, String body, String charset)
        throws EmailException {
    Email email = new SimpleEmail();

    // Set the charset if it was specified. Otherwise use the system's default.
    if (StringUtils.isNotBlank(charset)) {
        email.setCharset(charset);//from   w ww.  jav a 2s .  com
    }

    email.setHostName(host);
    email.setSmtpPort(Integer.parseInt(port));
    email.setSocketConnectionTimeout(socketTimeout);
    email.setDebug(true);

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

    if (StringUtils.equalsIgnoreCase(secure, "TLS")) {
        email.setStartTLSEnabled(true);
    } else if (StringUtils.equalsIgnoreCase(secure, "SSL")) {
        email.setSSLOnConnect(true);
        email.setSslSmtpPort(port);
    }

    // These have to be set after the authenticator, so that a new mail session isn't created
    ConfigurationController configurationController = ControllerFactory.getFactory()
            .createConfigurationController();
    email.getMailSession().getProperties().setProperty("mail.smtp.ssl.protocols", StringUtils.join(
            MirthSSLUtil.getEnabledHttpsProtocols(configurationController.getHttpsClientProtocols()), ' '));
    email.getMailSession().getProperties().setProperty("mail.smtp.ssl.ciphersuites", StringUtils.join(
            MirthSSLUtil.getEnabledHttpsCipherSuites(configurationController.getHttpsCipherSuites()), ' '));

    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:com.googlecode.fascinator.messaging.EmailNotificationConsumer.java

private void sendEmails(List<String> toList, List<String> ccList, String subject, String body,
        String fromAddress, String fromName, boolean isHtml) throws EmailException {
    Email email = null;
    if (isHtml) {
        email = new HtmlEmail();
        ((HtmlEmail) email).setHtmlMsg(body);
    } else {//from   w  w w . j ava2 s  .  co  m
        email = new SimpleEmail();
        email.setMsg(body);
    }
    email.setDebug(debug);
    email.setHostName(smtpHost);
    if (smtpUsername != null || smtpPassword != null) {
        email.setAuthentication(smtpUsername, smtpPassword);
    }
    email.setSmtpPort(smtpPort);
    email.setSslSmtpPort(smtpSslPort);
    email.setSSL(smtpSsl);
    email.setTLS(smtpTls);
    email.setSubject(subject);

    for (String to : toList) {
        email.addTo(to);
    }
    if (ccList != null) {
        for (String cc : ccList) {
            email.addCc(cc);
        }
    }
    email.setFrom(fromAddress, fromName);
    email.send();
}