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:org.ms123.common.workflow.tasks.TaskMailExecutor.java

protected void setMailServerProperties(Email email) {
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();

    String host = processEngineConfiguration.getMailServerHost();
    if (host == null) {
        throw new RuntimeException("TaskMailExecutor:Could not send email: no SMTP host is configured");
    }/*  w ww . ja  va  2  s  . co  m*/
    email.setHostName(host);

    int port = processEngineConfiguration.getMailServerPort();
    email.setSmtpPort(port);

    email.setSSL(processEngineConfiguration.getMailServerUseSSL());
    email.setTLS(processEngineConfiguration.getMailServerUseTLS());

    String user = processEngineConfiguration.getMailServerUsername();
    String password = processEngineConfiguration.getMailServerPassword();
    if (user != null && password != null) {
        email.setAuthentication(user, password);
    }
}

From source file:org.ms123.common.workflow.TaskSendExecutor.java

protected void setMailServerProperties(Email email) {
    ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();

    String host = processEngineConfiguration.getMailServerHost();
    if (host == null) {
        throw new RuntimeException("TaskSendExecutor:Could not send email: no SMTP host is configured");
    }/*  ww  w .ja v  a2s .c o m*/
    email.setHostName(host);

    int port = processEngineConfiguration.getMailServerPort();
    email.setSmtpPort(port);

    email.setSSL(processEngineConfiguration.getMailServerUseSSL());
    email.setTLS(processEngineConfiguration.getMailServerUseTLS());

    String user = processEngineConfiguration.getMailServerUsername();
    String password = processEngineConfiguration.getMailServerPassword();
    if (user != null && password != null) {
        email.setAuthentication(user, password);
    }
}

From source file:org.opencms.mail.CmsMailUtil.java

/**
 * Configures the mail from the given mail host configuration data.<p>
 *
 * @param host the mail host configuration
 * @param mail the email instance//from w w  w.  j a  v  a2 s  .c o  m
 */
public static void configureMail(CmsMailHost host, Email mail) {

    // set the host to the default mail host
    mail.setHostName(host.getHostname());
    mail.setSmtpPort(host.getPort());

    // check if username and password are provided
    String userName = host.getUsername();
    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(userName)) {
        // authentication needed, set user name and password
        mail.setAuthentication(userName, host.getPassword());
    }
    String security = host.getSecurity() != null ? host.getSecurity().trim() : null;
    if (SECURITY_SSL.equalsIgnoreCase(security)) {
        mail.setSslSmtpPort("" + host.getPort());
        mail.setSSLOnConnect(true);
    } else if (SECURITY_STARTTLS.equalsIgnoreCase(security)) {
        mail.setStartTLSEnabled(true);
    }

    try {
        // set default mail from address
        mail.setFrom(OpenCms.getSystemInfo().getMailSettings().getMailFromDefault());
    } catch (EmailException e) {
        // default email address is not valid, log error
        LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_SENDER_ADDRESS_0), e);
    }
}

From source file:org.ploin.pmf.impl.MailSender.java

/**
 *
 * @param email - the email object/*from w  w w.  ja va 2 s.  c  o  m*/
 * @param serverConfig - the server config object
 */
private void setServerProperties(Email email, ServerConfig serverConfig) throws MailFactoryException {
    try {
        String host = serverConfig.getHost();
        String fromEmail = serverConfig.getFromEmail();
        String fromName = serverConfig.getFromName();
        String authUser = serverConfig.getAuthUser();
        String authPassword = serverConfig.getAuthPassword();
        String replyTo = serverConfig.getReplyTo();

        email.setHostName(host);
        email.setFrom(fromEmail, fromName);
        if (isNotEmpty(authUser) && isNotEmpty(authPassword))
            email.setAuthentication(authUser, authPassword);

        email.addReplyTo((replyTo != null && !"".equals(replyTo)) ? replyTo : fromEmail);
    } catch (Exception e) {
        throw new MailFactoryException(e);
    }
}

From source file:org.sonatype.nexus.internal.email.EmailManagerImpl.java

/**
 * Apply server configuration to email.//from w w  w.  j av  a  2  s . co  m
 */
@VisibleForTesting
Email apply(final EmailConfiguration configuration, final Email mail) throws EmailException {
    mail.setHostName(configuration.getHost());
    mail.setSmtpPort(configuration.getPort());
    mail.setAuthentication(configuration.getUsername(), configuration.getPassword());

    mail.setStartTLSEnabled(configuration.isStartTlsEnabled());
    mail.setStartTLSRequired(configuration.isStartTlsRequired());
    mail.setSSLOnConnect(configuration.isSslOnConnectEnabled());
    mail.setSSLCheckServerIdentity(configuration.isSslCheckServerIdentityEnabled());
    mail.setSslSmtpPort(Integer.toString(configuration.getPort()));

    // default from address
    if (mail.getFromAddress() == null) {
        mail.setFrom(configuration.getFromAddress());
    }

    // apply subject prefix if configured
    String subjectPrefix = configuration.getSubjectPrefix();
    if (subjectPrefix != null) {
        String subject = mail.getSubject();
        mail.setSubject(String.format("%s %s", subjectPrefix, subject));
    }

    // do this last (mail properties are set up from the email fields when you get the mail session)
    if (configuration.isNexusTrustStoreEnabled()) {
        SSLContext context = trustStore.getSSLContext();
        Session session = mail.getMailSession();
        Properties properties = session.getProperties();
        properties.remove(EmailConstants.MAIL_SMTP_SOCKET_FACTORY_CLASS);
        properties.put(EmailConstants.MAIL_SMTP_SSL_ENABLE, true);
        properties.put("mail.smtp.ssl.socketFactory", context.getSocketFactory());
    }

    return mail;
}

From source file:org.structr.common.MailHelper.java

private static void setup(final Email mail, final String to, final String toName, final String from,
        final String fromName, final String cc, final String bcc, final String bounce, final String subject)
        throws EmailException {

    // FIXME: this might be slow if the config file is read each time
    final Properties config = Services.getInstance().getCurrentConfig();
    final String smtpHost = config.getProperty(Services.SMTP_HOST, "localhost");
    final String smtpPort = config.getProperty(Services.SMTP_PORT, "25");
    final String smtpUser = config.getProperty(Services.SMTP_USER);
    final String smtpPassword = config.getProperty(Services.SMTP_PASSWORD);
    final String smtpUseTLS = config.getProperty(Services.SMTP_USE_TLS, "true");
    final String smtpRequireTLS = config.getProperty(Services.SMTP_REQUIRE_TLS, "false");

    mail.setCharset(charset);/*from w w w .j a v  a2  s  .com*/
    mail.setHostName(smtpHost);
    mail.setSmtpPort(Integer.parseInt(smtpPort));
    mail.setStartTLSEnabled(Boolean.parseBoolean(smtpUseTLS));
    mail.setStartTLSRequired(Boolean.parseBoolean(smtpRequireTLS));
    mail.setCharset(charset);
    mail.setHostName(smtpHost);
    mail.setSmtpPort(Integer.parseInt(smtpPort));

    if (StringUtils.isNotBlank(smtpUser) && StringUtils.isNotBlank(smtpPassword)) {
        mail.setAuthentication(smtpUser, smtpPassword);
    }

    mail.addTo(to, toName);
    mail.setFrom(from, fromName);

    if (StringUtils.isNotBlank(cc)) {
        mail.addCc(cc);
    }

    if (StringUtils.isNotBlank(bcc)) {
        mail.addBcc(bcc);
    }

    if (StringUtils.isNotBlank(bounce)) {
        mail.setBounceAddress(bounce);
    }

    mail.setSubject(subject);

}

From source file:org.thousandturtles.gmail_demo.Main.java

public static void main(String[] args) {
    MailInfoRetriever retriever = new CLIMailInfoRetriever();
    MailInfo mailInfo = retriever.retrieveMailInfo();

    if (mailInfo.isNoAuth()) {
        System.out.println("Using No auth, mail with aspmx.l.google.com");
    } else {/*from   w  ww  . ja  va  2 s.  c  o  m*/
        System.out.printf("Username: %s%n", mailInfo.getUsername());
        System.out.printf("Password: %c***%c%n", mailInfo.getPassword().charAt(0),
                mailInfo.getPassword().charAt(mailInfo.getPassword().length() - 1));
    }
    System.out.printf("Mail target: %s%n", mailInfo.getMailTo());

    try {
        Email mail = new SimpleEmail();
        if (mailInfo.isNoAuth()) {
            mail.setHostName("aspmx.l.google.com");
            mail.setSmtpPort(25);
            mail.setFrom("no-reply@thousandturtles.org");
        } else {
            mail.setHostName("smtp.gmail.com");
            mail.setSmtpPort(465);
            mail.setAuthentication(mailInfo.getUsername(), mailInfo.getPassword());
            mail.setSSLOnConnect(true);
            mail.setFrom(mailInfo.getMailer());
        }
        mail.setCharset("UTF-8");
        mail.addTo(mailInfo.getMailTo(), "White Mouse");
        mail.setSubject("");
        mail.setMsg("?\nThis is a test mail!\n");
        mail.send();

        System.out.println("Mail sent successfully.");
    } catch (EmailException ee) {
        ee.printStackTrace();
    }
}

From source file:org.xerela.server.birt.ReportJob.java

private void setupEmail(JobExecutionContext executionContext, String emailTo, String emailCc, Email email)
        throws AddressException, EmailException {
    InternetAddress[] toAddrs = InternetAddress.parse(emailTo);
    email.setTo(Arrays.asList(toAddrs));

    if (emailCc != null && emailCc.trim().length() > 0) {
        InternetAddress[] ccAddrs = InternetAddress.parse(emailCc);
        email.setCc(Arrays.asList(ccAddrs));
    }/*from   ww  w.j av  a 2s  . co  m*/

    email.setCharset("utf-8"); //$NON-NLS-1$
    email.setHostName(System.getProperty(MAIL_HOST_PROP, "mail")); //$NON-NLS-1$
    String authUser = System.getProperty(MAIL_AUTH_USER_PROP);
    if (authUser != null) {
        email.setAuthentication(authUser, System.getProperty(MAIL_AUTH_PASSWORD_PROP));
    }
    email.setFrom(System.getProperty(MAIL_FROM_PROP), System.getProperty(MAIL_FROM_NAME_PROP));
    email.setSubject(
            Messages.bind(Messages.ReportJob_emailSubject, executionContext.getJobDetail().getFullName()));
    email.addHeader("X-Mailer", "Xerela Mailer"); //$NON-NLS-1$ //$NON-NLS-2$
    email.setDebug(Boolean.getBoolean("org.xerela.mail.debug")); //$NON-NLS-1$
}

From source file:org.yestech.notify.deliver.EmailDelivery.java

protected void enableAuthenticator(Email email) {
    if (isSsl()) {
        email.setSSL(true);/*from  w w  w .  j av  a  2s.c  o  m*/
    }
    if (isTls()) {
        email.setTLS(true);
    }
    if (StringUtils.isNotBlank(getAuthenticatorUserName())
            && StringUtils.isNotBlank(getAuthenticatorPassword())) {
        email.setAuthentication(getAuthenticatorUserName(), getAuthenticatorPassword());
    }
}

From source file:ru.codeinside.adm.CheckExecutionDates.java

@TransactionAttribute(REQUIRES_NEW)
public Email checkDates(ProcessEngine processEngine) throws EmailException {
    String emailTo = get(API.EMAIL_TO);
    String receiverName = get(API.RECEIVER_NAME);
    String hostName = get(API.HOST);
    String port = get(API.PORT);//from www  .  j av  a2  s . c  o m
    String senderLogin = get(API.SENDER_LOGIN);
    String password = get(API.PASSWORD);
    String emailFrom = get(API.EMAIL_FROM);
    String senderName = get(API.SENDER_NAME);

    if (emailTo.isEmpty() || receiverName.isEmpty() || hostName.isEmpty() || port.isEmpty()
            || senderLogin.isEmpty() || password.isEmpty() || emailFrom.isEmpty() || senderName.isEmpty()) {
        return null;
    }

    List<TaskDates> overdueTasks = new ArrayList<TaskDates>();
    List<Bid> overdueBids = new ArrayList<Bid>();
    List<TaskDates> endingTasks = new ArrayList<TaskDates>();
    List<Bid> endingBids = new ArrayList<Bid>();
    List<TaskDates> inactionTasks = new ArrayList<TaskDates>();

    Date currentDate = check(overdueTasks, overdueBids, endingTasks, endingBids, inactionTasks);

    Email email = new SimpleEmail();
    email.setSubject("[siu.oep-penza.ru]  ?? ?  ?  "
            + new SimpleDateFormat("yyyy.MM.dd HH:mm").format(currentDate));

    StringBuilder msg = new StringBuilder();
    msg.append(" !\n"
            + "  ?? ?  ? ?? ? [siu.oep-penza.ru]  ")
            .append(new SimpleDateFormat("yyyy.MM.dd").format(currentDate))
            .append(", ??  :\n\n");

    int n = 1;
    n = addTaskList(
            ",    ? ? ??",
            n, processEngine, overdueTasks, msg);
    n = addBidList(
            "?,    ? ? ??",
            n, overdueBids, msg);
    n = addTaskList(
            ", ? ??  ??  ?",
            n, processEngine, endingTasks, msg);
    n = addBidList(
            "?, ? ??  ??  ?",
            n, endingBids, msg);
    n = addTaskList(",  ???  ?:", n,
            processEngine, inactionTasks, msg);

    msg.append(
            "  ,  ? ? ?    !\n"
                    + " ?  ?  ? "
                    + "      . 8(8412)23-11-25 (. 45, 46, 47)");

    if (n == 1) {
        return null;
    }

    email.setMsg(msg.toString());
    email.addTo(emailTo, receiverName);
    email.setHostName(hostName);
    email.setSmtpPort(Integer.parseInt(port));
    email.setTLS(AdminServiceProvider.getBoolProperty(API.TLS));
    email.setAuthentication(senderLogin, password);
    email.setFrom(emailFrom, senderName);
    email.setCharset("utf-8");

    return email;
}