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

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

Introduction

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

Prototype

public String send() throws EmailException 

Source Link

Document

Sends the email.

Usage

From source file:iddb.core.util.MailManager.java

public void sendMail(String subject, String template, String[] dest, Map<String, String> args)
        throws Exception {
    if (props == null)
        throw new Exception("Unable to access email subsystem.");
    try {//from  www  . java 2 s.  c o m
        Email email = new SimpleEmail();
        email.setSubject(subject);
        email.setMsg(TemplateManager.getTemplate(template, args));
        for (String adr : dest) {
            email.addTo(adr);
        }
        email.setCharset("ISO-8859-1");
        setEmailProps(email);
        email.send();
    } catch (Exception e) {
        log.error("{}: {}", e.getClass().getName(), e.getMessage());
        throw new Exception("We are unable to send your message right now.");
    }
}

From source file:ch.unibas.fittingwizard.application.tools.Notifications.java

private void sendMailTesting() {
    try {//from   w  ww.  j  av a 2 s .  com
        Email email = new SimpleEmail();
        email.setMailSession(Session.getDefaultInstance(props));

        email.setSubject("Test mail");
        email.setMsg("This is a test mail for checking parameters from config file");
        email.setFrom(getSender().trim());
        email.addTo(getRecipient().trim());

        email.send();
    } catch (EmailException e) {
        throw new RuntimeException("Could not send notification.", e);
    }
}

From source file:ch.unibas.fittingwizard.application.tools.Notifications.java

private void sendErrorLogByMail() {
    try {//ww w. ja  v  a 2  s. com
        Email email = new SimpleEmail();
        email.setMailSession(Session.getDefaultInstance(props));

        email.setSubject("Log of FW session");
        email.setMsg(new String(Files.readAllBytes(Paths.get("fw-log.txt"))));
        email.setFrom(getSender().trim());
        email.addTo(getRecipient().trim());

        email.send();
    } catch (IOException | EmailException e) {
        throw new RuntimeException("Could not send notification.", e);
    }
}

From source file:br.com.jvmsoftware.util.EnviarMail.java

public void emailSimples(PubConfigEmpresa conf, PubUsuario usu, String msg, String subj) throws EmailException {

    Email email = new SimpleEmail();
    email.setHostName(conf.getMailEnvioSmtp());
    email.setSmtpPort(conf.getMailEnvioPorta());
    email.setAuthenticator(new DefaultAuthenticator(conf.getMailEnvio(), conf.getMailEnvioSenha()));
    email.setSSLOnConnect(true);/*from ww w. ja v  a 2s  .  com*/
    email.setFrom(conf.getMailEnvio());
    email.setSubject(subj);
    email.setMsg(msg);
    email.addTo(usu.getEmail());
    email.send();
}

From source file:de.hybris.basecommerce.SimpleSmtpServerUtilsTest.java

@Test
public void testSendSuccess() throws EmailException, AddressException {
    final String origMailPortNumber = Config.getParameter(Config.Params.MAIL_SMTP_PORT);
    final String origMailHost = Config.getParameter(Config.Params.MAIL_SMTP_SERVER);

    SimpleSmtpServer server = null;//from   ww w. j  ava 2 s . c o  m
    try {
        server = SimpleSmtpServerUtils.startServer(TEST_START_PORT);
        Assert.assertFalse(server.isStopped());
        Assert.assertTrue(server.getPort() > 0);

        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, "localhost");
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, String.valueOf(server.getPort()));

        final Email email = MailUtils.getPreConfiguredEmail();

        email.setFrom("foo.bar@hybris.com");
        email.setTo(Arrays.asList(InternetAddress.parse("foo.bar@hybris.com")));
        email.setSubject("TEST TEST TEST");
        email.setContent("FOO", Email.TEXT_PLAIN);

        email.send();
    } finally {
        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, origMailHost);
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, origMailPortNumber);

        if (server != null) {
            server.stop();
        }
    }
}

From source file:com.packtpub.e4.advanced.event.mailman.MailSender.java

@Override
public void handleEvent(Event event) {
    String topic = event.getTopic();
    if (topic.startsWith("smtp/")) {
        String importance = topic.substring("smtp/".length());
        String to = (String) event.getProperty("To");
        String from = (String) event.getProperty("From");
        String subject = (String) event.getProperty("Subject");
        String body = (String) event.getProperty("DATA");
        try {/* w w w. ja  v  a  2s . c om*/
            Email email = new SimpleEmail();
            email.setDebug(false);
            email.setHostName(hostname);
            email.setSmtpPort(port);
            email.setFrom(from);
            email.addTo(to);
            email.setSubject(subject);
            email.setMsg(body);
            email.addHeader("Importance", importance);
            email.send();
            log(LogService.LOG_INFO, "Message sent successfully to " + to);
        } catch (EmailException e) {
            log(LogService.LOG_ERROR, "Error occurred" + e);
        }
    }
}

From source file:jmockit.tutorial.domain.MyBusinessService.java

private void sendNotificationEmail(List<EntityX> items) throws EmailException {
    Email email = new SimpleEmail();
    email.setSubject("Notification about processing of ...");
    email.addTo(data.getCustomerEmail());

    // Other e-mail parameters, such as the host name of the mail server, have defaults defined through external
    // configuration.

    String message = buildNotificationMessage(items);
    email.setMsg(message);//  w ww .jav a2s.co  m

    email.send();
}

From source file:iddb.core.util.MailManager.java

public void sendAdminMail(String subject, String message, String replyTo) throws Exception {
    if (props == null)
        throw new Exception("Unable to access email subsystem.");
    try {// www  .  j  av  a2 s.  co m
        Email email = new SimpleEmail();
        email.setSubject(subject);
        email.setMsg(message);
        if (replyTo != null) {
            email.addReplyTo(replyTo);
        }
        for (String adr : props.getProperty("admin").split(";")) {
            email.addTo(adr);
        }
        setEmailProps(email);
        email.send();
    } catch (Exception e) {
        log.error("{}: {}", e.getClass().getName(), e.getMessage());
        throw new Exception("We are unable to send your message right now.");
    }
}

From source file:com.kylinolap.job.tools.MailService.java

public void sendMail(List<String> receivers, String subject, String content) throws IOException {

    Email email = new HtmlEmail();
    email.setHostName(host);//from  w ww  .  j av  a  2s.  c o  m
    email.setDebug(true);
    try {
        for (String receiver : receivers) {
            email.addTo(receiver);
        }

        email.setFrom(sender);
        email.setSubject(subject);
        email.setCharset("UTF-8");
        ((HtmlEmail) email).setHtmlMsg(content);
        email.send();
        email.getMailSession();

        System.out.println("!!");
    } catch (EmailException e) {
        e.printStackTrace();
    }
}

From source file:ch.unibas.fittingwizard.application.tools.Notifications.java

private void sendMailGaussian(boolean isLogValid) {
    try {/*from   www.  jav  a  2s.  c o m*/
        Email email = new SimpleEmail();
        email.setMailSession(Session.getDefaultInstance(props));

        email.setSubject("Gaussian calculation finished");
        email.setMsg("Gaussian calculation finished. Log file validation returned: " + isLogValid);
        email.setFrom(getSender().trim());
        email.addTo(getRecipient().trim());

        email.send();
    } catch (EmailException e) {
        throw new RuntimeException("Could not send notification.", e);
    }
}