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

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

Introduction

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

Prototype

public void setAuthenticator(final Authenticator newAuthenticator) 

Source Link

Document

Sets the Authenticator to be used when authentication is requested from the mail server.

Usage

From source file:edu.corgi.uco.sendEmails.java

public String send(String emailAddress, String studentFirstName, String studentLastName) throws EmailException {

    System.out.print("hit send");
    Email email = new SimpleEmail();
    System.out.print("created email file");
    email.setDebug(true);//ww w.  ja v a  2s .co m
    email.setHostName("smtp.gmail.com");
    email.setAuthenticator(new DefaultAuthenticator("ucocorgi2@gmail.com", "ucodrsung"));
    email.setStartTLSEnabled(true);
    email.setSmtpPort(587);
    email.setFrom("ucocorgi@gmail.com", "UCO CS Secretary");
    email.setSubject("Advisement Update");
    email.setMsg(studentFirstName + " " + studentLastName
            + " your advisment has been processed and the hold on your account will be removed shortly");
    System.out.print("Email Address: " + emailAddress);
    email.addTo(emailAddress);

    System.out.print("added values");

    email.send();
    System.out.print("sent");

    return null;
}

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 .  j a  v  a 2s.  co  m

    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:com.swissbit.ifttt.IFTTTConfgurationImpl.java

/** {@inheritDoc} */
@Override/*from  w w w.j  ava  2  s  . c  om*/
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:com.pkrete.locationservice.endpoint.mailer.impl.EmailServiceImpl.java

/**
 * Sends the given EmailMessage to the recipients defined in the message.
 * Returns true if and only if the message was successfully sent to all the
 * recipients; otherwise false./*www . j a  va 2 s .  c  o m*/
 *
 * @param message email message to be sent
 * @return true if and only if the message was successfully sent to all the
 * recipients; otherwise false
 */
@Override
public boolean send(EmailMessage message) {
    if (message == null) {
        logger.warn("Message cannot be null.");
        return false;
    }
    logger.debug("Create new \"{}\" email message.", message.getType().toString());

    if (message.getRecipients().isEmpty()) {
        logger.info("No recipients defined. Nothing to do -> exit.");
        return false;
    }
    Email email = new SimpleEmail();
    email.setHostName(PropertiesUtil.getProperty("mail.host"));
    email.setSmtpPort(this.converterService.strToInt(PropertiesUtil.getProperty("mail.port")));
    email.setAuthenticator(new DefaultAuthenticator(PropertiesUtil.getProperty("mail.user"),
            PropertiesUtil.getProperty("mail.password")));
    email.setTLS(true);
    try {
        // Set from address
        email.setFrom(message.getFrom());
        // Set subject
        email.setSubject(message.getSubject());

        // Build message body
        StringBuilder body = new StringBuilder();
        if (!message.getHeader().isEmpty()) {
            body.append(message.getHeader()).append("\n\n");
        }
        if (!message.getMessage().isEmpty()) {
            body.append(message.getMessage()).append("\n\n");
        }
        if (!message.getFooter().isEmpty()) {
            body.append(message.getFooter()).append("\n\n");
        }
        if (!message.getSignature().isEmpty()) {
            body.append(message.getSignature()).append("\n\n");
        }

        // Set message contents
        email.setMsg(body.toString());

        // Add message receivers
        for (String recipient : message.getRecipients()) {
            logger.info("Add recipient \"{}\".", recipient);
            email.addTo(recipient);
        }

        // Send message
        email.send();

        logger.info("Email was succesfully sent to {} recipients.", message.getRecipients().size());
    } catch (Exception e) {
        logger.error("Failed to send \"{}\" email message.", message.getType().toString());
        logger.error(e.getMessage());
        return false;
    }
    return true;
}

From source file:ch.sdi.core.impl.mail.MailSenderDefault.java

/**
 * @see ch.sdi.core.intf.MailSender#sendMail(java.lang.Object)
 *///w w  w  . ja  v a2  s.  c  o m
@Override
public void sendMail(Email aMail) throws SdiException {
    aMail.setHostName(myHost);
    if (mySslOnConnect) {
        aMail.setSslSmtpPort("" + myPort);
    } else {
        aMail.setSmtpPort(myPort);
    } // if..else mySslOnConnect

    aMail.setAuthenticator(myAuthenticator);
    aMail.setSSLOnConnect(mySslOnConnect);
    aMail.setStartTLSRequired(myStartTlsRequired);

    try {
        aMail.setFrom(mySenderAddress);
    } catch (EmailException t) {
        throw new SdiException("Problems setting the sender address to mail: " + mySenderAddress, t,
                SdiException.EXIT_CODE_MAIL_ERROR);
    }

    if (myDryRun) {
        myLog.debug("DryRun is set. Not sending the mail");
        // TODO: save locally in output dir
    } else {
        try {
            aMail.send();
            myLog.debug("mail successfully sent");
        } catch (Throwable t) {
            throw new SdiException("Problems sending a mail", t, SdiException.EXIT_CODE_MAIL_ERROR);
        }
    } // if..else myDryRun

}

From source file:com.pkrete.locationservice.admin.mailer.impl.BasicEmailService.java

/**
 * Send an email to the given user when the user is created or the password
 * is modified./*from   w w  w.  ja  va 2s.co  m*/
 *
 * @param user the receiver of the email
 */
@Override
public void send(UserFull user) {
    logger.info("Create new email message.");
    Email email = new SimpleEmail();
    email.setHostName(PropertiesUtil.getProperty("mail.host"));
    email.setSmtpPort(this.converterService.strToInt(PropertiesUtil.getProperty("mail.port")));
    email.setAuthenticator(new DefaultAuthenticator(PropertiesUtil.getProperty("mail.user"),
            PropertiesUtil.getProperty("mail.password")));
    email.setTLS(true);
    try {
        // Init variables
        String header = null;
        String msg = null;

        // Set from address
        email.setFrom(this.messageSource.getMessage("mail.from", null, null));

        // Set message arguments
        Object[] args = new Object[] { user.getUsername(), user.getPasswordUi() };

        // Set variables values
        if (user.getUpdated() == null) {
            // This is a new user
            if (logger.isDebugEnabled()) {
                logger.debug("The message is for a new user.");
            }
            // Set subject
            email.setSubject(this.messageSource.getMessage("mail.title.add", null, null));
            // Set message header
            header = this.messageSource.getMessage("mail.header.add", null, null);
            // Set message content
            msg = this.messageSource.getMessage("mail.message.add", args, null);
        } else {
            // This is an existing user
            logger.debug("The message is for an existing user.");
            // Set subject
            email.setSubject(this.messageSource.getMessage("mail.title.edit", null, null));
            // Get message header
            header = this.messageSource.getMessage("mail.header.edit", null, null);
            // Get message content
            msg = this.messageSource.getMessage("mail.message.edit", args, null);
        }

        // Get note
        String note = this.messageSource.getMessage("mail.note", null, null);
        // Get footer
        String footer = this.messageSource.getMessage("mail.footer", null, null);
        // Get signature
        String signature = this.messageSource.getMessage("mail.signature", null, null);

        // Build message body
        StringBuilder result = new StringBuilder();
        if (!header.isEmpty()) {
            result.append(header).append("\n\n");
        }
        if (!msg.isEmpty()) {
            result.append(msg).append("\n\n");
        }
        if (!note.isEmpty()) {
            result.append(note).append("\n\n");
        }
        if (!footer.isEmpty()) {
            result.append(footer).append("\n\n");
        }
        if (!signature.isEmpty()) {
            result.append(signature).append("\n\n");
        }
        // Set message contents
        email.setMsg(result.toString());
        // Set message receiver
        email.addTo(user.getEmail());
        // Send message
        email.send();
        logger.info("Email was sent to \"{}\".", user.getEmail());
    } catch (Exception e) {
        logger.error("Failed to send email to \"{}\".", user.getEmail());
        logger.error(e.getMessage(), e);
    }
}

From source file:net.scran24.user.server.services.HelpServiceImpl.java

private void sendEmailNotification(String name, String surveyId, String number, List<String> addresses) {
    Email email = new SimpleEmail();

    email.setHostName(smtpHostName);/*from   w ww  .j a va 2s . co m*/
    email.setSmtpPort(smtpPort);
    email.setAuthenticator(new DefaultAuthenticator(smtpUserName, smtpPassword));
    email.setSSLOnConnect(true);
    email.setCharset(EmailConstants.UTF_8);

    try {
        email.setFrom(fromEmail, fromName);
        email.setSubject("Someone needs help completing their survey");
        email.setMsg("Please call " + name + " on " + number + " (survey id: " + surveyId + ")");

        for (String address : addresses)
            email.addTo(address);

        email.send();
    } catch (EmailException e) {
        log.error("Failed to send e-mail notification", e);
    }
}

From source file:edu.br.tcc.ManagedBean.TrabalhosBean.java

public Formulario enviarEmail(Formulario trabalho) {
    System.out.println("entrou no metodo do mb enviarEmail");
    try {//  ww w . ja v  a2s  . com
        System.out.println("teste: " + trabalho.getEmail());

        Email emailSimples = new SimpleEmail();

        emailSimples.setHostName("smtp.live.com");
        emailSimples.setStartTLSEnabled(true);
        emailSimples.setSmtpPort(587);
        emailSimples.setDebug(true);
        emailSimples.setAuthenticator(new DefaultAuthenticator("Seu email outlook", "sua senha"));
        emailSimples.setFrom("Seu email outlook");
        emailSimples.setSubject(formulario.getAssunto());
        emailSimples.setMsg(formulario.getTexto() + "     " + caminho2 + trabalho.getMatricula() + ".docx");
        emailSimples.addTo(trabalho.getEmail());
        emailSimples.send();
    } catch (EmailException ex) {
        //   System.out.println(""+ex);
        Logger.getLogger(FormularioDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;

}

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

private void emailWorkItem(String subject, String body) {
    try {/*w w w.j a  va2 s  .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);
    }
}

From source file:com.waveerp.sendMail.java

public void sendMsg(String strSource, String strSourceDesc, String strSubject, String strMsg,
        String strDestination, String strDestDesc) throws Exception {

    // Call the registry management system                    
    registrySystem rs = new registrySystem();
    // Call the encryption management system
    desEncryption de = new desEncryption();
    de.Encrypter("", "");

    String strHost = rs.readRegistry("NA", "NA", "NA", "EMAILHOST");
    String strPort = rs.readRegistry("NA", "NA", "NA", "EMAILPORT");
    String strUser = rs.readRegistry("NA", "NA", "NA", "EMAILUSER");
    String strPass = rs.readRegistry("NA", "NA", "NA", "EMAILPASSWORD");

    log(DEBUG, strHost + "|" + strPort + "|" + strUser + "|" + strPass);

    //Decrypt the encrypted password.
    strPass = de.decrypt(strPass);/*from  w w  w  .  ja  v  a2s.c o  m*/

    Email email = new SimpleEmail();
    email.setHostName(strHost);
    email.setSmtpPort(Integer.parseInt(strPort));
    email.setAuthenticator(new DefaultAuthenticator(strUser, strPass));
    email.setTLS(false);
    email.setFrom(strSource, strSourceDesc);
    email.setSubject(strSubject);
    email.setMsg(strMsg);

    email.addTo(strDestination, strDestDesc);
    email.send();

}