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

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

Introduction

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

Prototype

public Email setSubject(final String aSubject) 

Source Link

Document

Set the email subject.

Usage

From source file:easycar.controller.BookingListController.java

public void emailRemind() {
    Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);//from  w ww.j a  v a2s  .  com
    email.setAuthenticator(new DefaultAuthenticator("easycarremind", "easycartest"));
    email.setSSLOnConnect(true);
    try {
        email.setFrom("easycarremind@gmail.com");
        email.setSubject("Reminder to return car");
        email.setMsg("Dear " + selectedBooking.getCustomerName()
                + ",\n\nPlease note that you are supposed to return the car with id "
                + selectedBooking.getCarId() + " to us on " + selectedBooking.getEndDateToDisplay()
                + "\n\nBest regards,\nEasy Car Team.");
        email.addTo(selectedBooking.getCustomerEmail());
        email.send();
    } catch (EmailException e) {
        e.printStackTrace();
    }

    selectedBooking.setLastEmailRemindDate(getZeroTimeDate(new Date()));
    try {
        bookingService.editBooking(selectedBooking);
    } catch (IOException e) {
        e.printStackTrace();
    }

    FacesContext.getCurrentInstance().addMessage("messageDetailDialog", new FacesMessage(
            FacesMessage.SEVERITY_INFO, dictionaryController.getDictionary().get("REMINDER_SENT"), null));
}

From source file:easycar.controller.BookingListController.java

public void emailRemindAll() {
    FullBookingDto currentFullBookingDto;
    for (int i = 0; i < baseFullBookingList.size(); i++) {
        currentFullBookingDto = baseFullBookingList.get(i);

        if (!currentFullBookingDto.isRemindButtonOn()) {
            continue;
        }//from  ww w . j  a v  a2s.  c  om

        Email email = new SimpleEmail();
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("easycarremind", "easycartest"));
        email.setSSLOnConnect(true);

        try {
            email.setFrom("easycarremind@gmail.com");
            email.setSubject("Reminder to return car");
            email.setMsg("Dear " + currentFullBookingDto.getCustomerName()
                    + ",\n\nPlease note that you are supposed to return the car with id "
                    + currentFullBookingDto.getCarId() + " to us on "
                    + currentFullBookingDto.getEndDateToDisplay() + "\n\nBest regards,\nEasy Car Team.");
            email.addTo(currentFullBookingDto.getCustomerEmail());
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }

        currentFullBookingDto.setLastEmailRemindDate(getZeroTimeDate(new Date()));
        try {
            bookingService.editBooking(currentFullBookingDto);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    FacesContext.getCurrentInstance().addMessage("messageEmailRemindAll", new FacesMessage(
            FacesMessage.SEVERITY_INFO, dictionaryController.getDictionary().get("REMINDER_SENT"), null));
}

From source file:net.wikiadmin.clientnotification.SendMain.java

/** sendMainText. */
void sendMainText(String cMail, String cName, String cCredit) {

    /* i need your data!!! :D */
    XmlProp readData = new XmlProp("mailsettings.xml");
    readData.readData();/*from www .  j  a  v  a 2 s  .c om*/

    userS = readData.getUser();
    passS = readData.getpass();
    hostS = readData.gethost();
    portS = readData.getport();
    sslS = readData.getssl();
    fromS = readData.getfrom();
    toS = cMail;
    intPortS = Integer.parseInt(portS);
    sslBoolean = Boolean.parseBoolean(sslS);
    themeText = readData.getTheme();
    bodyText = readData.getBody() + cName + ".\n" + readData.getBodyP2() + cCredit + readData.getBodyP3() + "\n"
            + readData.getBodyContacts();

    try {
        Email email = new SimpleEmail();
        email.setHostName(hostS);
        email.setAuthenticator(new DefaultAuthenticator(userS, passS));
        email.setSSLOnConnect(sslBoolean);
        email.setSmtpPort(intPortS);
        email.setFrom(fromS);
        email.setSubject(themeText);
        email.setMsg(bodyText);
        email.addTo(toS);
        email.send();
    } catch (EmailException ex) {
        ex.printStackTrace();
        System.out.println("");
    }
}

From source file:org.apache.airavata.credential.store.notifier.impl.EmailNotifier.java

public void notifyMessage(NotificationMessage message) throws CredentialStoreException {
    try {//  www  .j a  v  a2  s .  co  m
        Email email = new SimpleEmail();
        email.setHostName(this.emailNotifierConfiguration.getEmailServer());
        email.setSmtpPort(this.emailNotifierConfiguration.getEmailServerPort());
        email.setAuthenticator(new DefaultAuthenticator(this.emailNotifierConfiguration.getEmailUserName(),
                this.emailNotifierConfiguration.getEmailPassword()));
        email.setSSLOnConnect(this.emailNotifierConfiguration.isSslConnect());
        email.setFrom(this.emailNotifierConfiguration.getFromAddress());

        EmailNotificationMessage emailMessage = (EmailNotificationMessage) message;

        email.setSubject(emailMessage.getSubject());
        email.setMsg(emailMessage.getMessage());
        email.addTo(emailMessage.getSenderEmail());
        email.send();

    } catch (EmailException e) {
        log.error("[CredentialStore]Error sending email notification message.");
        throw new CredentialStoreException("Error sending email notification message", e);
    }

}

From source file:org.apache.fineract.infrastructure.core.service.GmailBackedPlatformEmailService.java

@Override
public void sendToUserAccount(final EmailDetail emailDetail, final String unencodedPassword) {
    final Email email = new SimpleEmail();
    final SMTPCredentialsData smtpCredentialsData = this.externalServicesReadPlatformService
            .getSMTPCredentials();/*from   w w w .  j  a v  a 2 s . co m*/
    final String authuserName = smtpCredentialsData.getUsername();

    final String authuser = smtpCredentialsData.getUsername();
    final String authpwd = smtpCredentialsData.getPassword();

    // Very Important, Don't use email.setAuthentication()
    email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
    email.setDebug(false); // true if you want to debug
    email.setHostName(smtpCredentialsData.getHost());
    try {
        if (smtpCredentialsData.isUseTLS()) {
            email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
        }
        email.setFrom(authuser, authuserName);

        final StringBuilder subjectBuilder = new StringBuilder().append("Fineract Prototype Demo: ")
                .append(emailDetail.getContactName()).append(" user account creation.");

        email.setSubject(subjectBuilder.toString());

        final String sendToEmail = emailDetail.getAddress();

        final StringBuilder messageBuilder = new StringBuilder()
                .append("You are receiving this email as your email account: ").append(sendToEmail)
                .append(" has being used to create a user account for an organisation named [")
                .append(emailDetail.getOrganisationName()).append("] on Fineract Prototype Demo.")
                .append("You can login using the following credentials: username: ")
                .append(emailDetail.getUsername()).append(" password: ").append(unencodedPassword);

        email.setMsg(messageBuilder.toString());

        email.addTo(sendToEmail, emailDetail.getContactName());
        email.send();
    } catch (final EmailException e) {
        throw new PlatformEmailSendException(e);
    }
}

From source file:org.apache.kylin.common.util.MailService.java

/**
 * @param receivers//from   ww w. ja v  a2s  .  c  om
 * @param subject
 * @param content
 * @return true or false indicating whether the email was delivered successfully
 * @throws IOException
 */
public boolean sendMail(List<String> receivers, String subject, String content, boolean isHtmlMsg) {

    if (!enabled) {
        logger.info("Email service is disabled; this mail will not be delivered: " + subject);
        logger.info("To enable mail service, set 'kylin.job.notification-enabled=true' in kylin.properties");
        return false;
    }

    Email email = new HtmlEmail();
    email.setHostName(host);
    email.setStartTLSEnabled(starttlsEnabled);
    if (starttlsEnabled) {
        email.setSslSmtpPort(port);
    } else {
        email.setSmtpPort(Integer.valueOf(port));
    }

    if (username != null && username.trim().length() > 0) {
        email.setAuthentication(username, password);
    }

    //email.setDebug(true);
    try {
        for (String receiver : receivers) {
            email.addTo(receiver);
        }

        email.setFrom(sender);
        email.setSubject(subject);
        email.setCharset("UTF-8");
        if (isHtmlMsg) {
            ((HtmlEmail) email).setHtmlMsg(content);
        } else {
            ((HtmlEmail) email).setTextMsg(content);
        }
        email.send();
        email.getMailSession();

    } catch (EmailException e) {
        logger.error(e.getLocalizedMessage(), e);
        return false;
    }

    return true;
}

From source file:org.apache.nifi.processors.email.GenerateAttachment.java

public byte[] SimpleEmail() {
    Email email = new SimpleEmail();
    try {//from w  ww  .j  ava2s  .com
        email.setFrom(from);
        email.addTo(to);
        email.setSubject(subject);
        email.setMsg(message);
        email.setHostName(hostName);
        email.buildMimeMessage();
    } catch (EmailException e) {
        e.printStackTrace();
    }

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    MimeMessage mimeMessage = email.getMimeMessage();
    try {
        mimeMessage.writeTo(output);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }

    return output.toByteArray();
}

From source file:org.apache.nifi.processors.email.TestListenSMTP.java

@Test
public void validateSuccessfulInteraction() throws Exception, EmailException {
    int port = NetworkUtils.availablePort();

    TestRunner runner = TestRunners.newTestRunner(ListenSMTP.class);
    runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");
    runner.setProperty(ListenSMTP.SMTP_TIMEOUT, "10 seconds");

    runner.assertValid();//from  w w  w.  j  a va2 s. c om
    runner.run(5, false);
    final int numMessages = 5;
    CountDownLatch latch = new CountDownLatch(numMessages);

    this.executor.schedule(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < numMessages; i++) {
                try {
                    Email email = new SimpleEmail();
                    email.setHostName("localhost");
                    email.setSmtpPort(port);
                    email.setFrom("alice@nifi.apache.org");
                    email.setSubject("This is a test");
                    email.setMsg("MSG-" + i);
                    email.addTo("bob@nifi.apache.org");
                    email.send();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }
    }, 1500, TimeUnit.MILLISECONDS);

    boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
    runner.shutdown();
    assertTrue(complete);
    runner.assertAllFlowFilesTransferred(ListenSMTP.REL_SUCCESS, numMessages);
}

From source file:org.apache.nifi.processors.email.TestListenSMTP.java

@Test
public void validateSuccessfulInteractionWithTls() throws Exception, EmailException {
    System.setProperty("mail.smtp.ssl.trust", "*");
    System.setProperty("javax.net.ssl.keyStore", "src/test/resources/localhost-ks.jks");
    System.setProperty("javax.net.ssl.keyStorePassword", "localtest");
    int port = NetworkUtils.availablePort();

    TestRunner runner = TestRunners.newTestRunner(ListenSMTP.class);
    runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");
    runner.setProperty(ListenSMTP.SMTP_TIMEOUT, "10 seconds");

    // Setup the SSL Context
    SSLContextService sslContextService = new StandardSSLContextService();
    runner.addControllerService("ssl-context", sslContextService);
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE,
            "src/test/resources/localhost-ts.jks");
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_PASSWORD, "localtest");
    runner.setProperty(sslContextService, StandardSSLContextService.TRUSTSTORE_TYPE, "JKS");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE,
            "src/test/resources/localhost-ks.jks");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_PASSWORD, "localtest");
    runner.setProperty(sslContextService, StandardSSLContextService.KEYSTORE_TYPE, "JKS");
    runner.enableControllerService(sslContextService);

    // and add the SSL context to the runner
    runner.setProperty(ListenSMTP.SSL_CONTEXT_SERVICE, "ssl-context");
    runner.setProperty(ListenSMTP.CLIENT_AUTH, SSLContextService.ClientAuth.NONE.name());
    runner.assertValid();/*from   w ww . j  a  va2 s .c o  m*/

    int messageCount = 5;
    CountDownLatch latch = new CountDownLatch(messageCount);
    runner.run(messageCount, false);

    this.executor.schedule(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < messageCount; i++) {
                try {
                    Email email = new SimpleEmail();
                    email.setHostName("localhost");
                    email.setSmtpPort(port);
                    email.setFrom("alice@nifi.apache.org");
                    email.setSubject("This is a test");
                    email.setMsg("MSG-" + i);
                    email.addTo("bob@nifi.apache.org");

                    // Enable STARTTLS but ignore the cert
                    email.setStartTLSEnabled(true);
                    email.setStartTLSRequired(true);
                    email.setSSLCheckServerIdentity(false);
                    email.send();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }
    }, 1500, TimeUnit.MILLISECONDS);

    boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
    runner.shutdown();
    assertTrue(complete);
    runner.assertAllFlowFilesTransferred("success", messageCount);
}

From source file:org.apache.nifi.processors.email.TestListenSMTP.java

@Test
public void validateTooLargeMessage() throws Exception, EmailException {
    int port = NetworkUtils.availablePort();

    TestRunner runner = TestRunners.newTestRunner(ListenSMTP.class);
    runner.setProperty(ListenSMTP.SMTP_PORT, String.valueOf(port));
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_CONNECTIONS, "3");
    runner.setProperty(ListenSMTP.SMTP_TIMEOUT, "10 seconds");
    runner.setProperty(ListenSMTP.SMTP_MAXIMUM_MSG_SIZE, "10 B");

    runner.assertValid();/*from  w  w  w .j a  v  a2 s.  c  o m*/

    int messageCount = 1;
    CountDownLatch latch = new CountDownLatch(messageCount);

    runner.run(messageCount, false);

    this.executor.schedule(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < messageCount; i++) {
                try {
                    Email email = new SimpleEmail();
                    email.setHostName("localhost");
                    email.setSmtpPort(port);
                    email.setFrom("alice@nifi.apache.org");
                    email.setSubject("This is a test");
                    email.setMsg("MSG-" + i);
                    email.addTo("bob@nifi.apache.org");
                    email.send();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                } finally {
                    latch.countDown();
                }
            }
        }
    }, 1000, TimeUnit.MILLISECONDS);

    boolean complete = latch.await(5000, TimeUnit.MILLISECONDS);
    runner.shutdown();
    assertTrue(complete);
    runner.assertAllFlowFilesTransferred("success", 0);
}