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

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

Introduction

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

Prototype

public String send() throws EmailException 

Source Link

Document

Sends the email.

Usage

From source file:com.mycompany.webtestegit.util.TesteMail.java

public static void main(String[] args) {
    SimpleEmail email = new SimpleEmail();
    email.setHostName("smtp.gmail.com"); // o servidor SMTP para envio do e-mail 
    try {/*from   www.j  a  v  a 2s . co m*/
        email.addTo("cfs.bsi@gmail.com", "Christian"); //destinatrio 
        email.setFrom("programacao.micromap@gmail.com", "Micromap"); // remetente 
        email.setSubject("Titulo do e-mail"); // assunto do e-mail 
        email.setMsg("Teste de Email utilizando commons-email"); //conteudo do e-mail 
        email.setAuthentication("ORIGEM", "SENHA");
        email.setSSLCheckServerIdentity(true);
        email.send(); //envia o e-mail
    } catch (EmailException ex) {
        Logger.getLogger(TesteMail.class.getName()).log(Level.SEVERE, null, ex);
    }

    //EMAIL HTML
    //        HtmlEmail email = new HtmlEmail();
    //
    //        try {
    //            email.setHostName("smtp.gmail.com");
    //            email.addTo("cfs.bsi@gmail.com", "Cfs");
    //            email.setFrom("programacao.micromap@gmail.com", "Micromap"); 
    //            email.setSubject("Teste de e-mail em formato HTML");   
    //
    //
    //            // adiciona uma imagem ao corpo da mensagem e retorna seu id 
    //            URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
    //            String cid = email.embed(url, "Apache logo");   
    //
    //            // configura a mensagem para o formato HTML 
    //            email.setHtmlMsg("<html>The apache logo - <img src=\"cid:" + cid + "\"></html>");   
    //
    //            // configure uma mensagem alternativa caso o servidor no suporte HTML 
    //            email.setTextMsg("Seu servidor de e-mail no suporta mensagem HTML");   
    //            email.setAuthentication("ORIGEM", "SENHA");
    //            
    //            // envia o e-mail 
    //            email.send();
    //        } catch (EmailException ex) {
    //            Logger.getLogger(TesteMail.class.getName()).log(Level.SEVERE, null, ex);
    //        } catch (MalformedURLException ex) {
    //            Logger.getLogger(TesteMail.class.getName()).log(Level.SEVERE, null, ex);
    //        }
}

From source file:com.turn.griffin.GriffinModule.java

public static void emailAlert(String subject, String body) {
    String serverId = GriffinConfig.getProperty("serverid", null);
    try {//from   w w w  .j  a v  a  2s.c om
        SimpleEmail email = new SimpleEmail();
        email.setCharset("utf-8");
        email.setFrom(ERROR_EMAIL_SENDER);
        email.addTo(ERROR_EMAIL_RECIPIENTS);
        email.setSubject(subject + "(" + serverId + ")");
        email.setMsg(body);
        email.send();
    } catch (EmailException e) {
        logger.error(String.format("Failed to send alert email To:%s Subject:%s Body:%s",
                ERROR_EMAIL_RECIPIENTS, subject, body), e);
    }
}

From source file:model.Email.java

public static void sendToken(String receiver, String token) throws EmailException {

    SimpleEmail email = new SimpleEmail();
    String message = "Para sua segurana, voc precisa colocar o cdigo abaixo para continar a transao.\n";
    message += "Token: " + token + "\n";
    message += "\nO sistema de token  para sua segurana, sempre ao realizar uma transao, um novo token  gerado ";
    message += " e enviado  voc.";

    try {//from  w w w.  ja v a 2  s . co  m
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("totheworldgroup@gmail.com", "albrcalu"));
        email.setSSLOnConnect(true);
        email.setFrom("totheworldgroup@gmail.com");
        email.setSubject("Token para realizar transao");
        email.setMsg(message);
        email.addTo(receiver);
        email.send();

    } catch (EmailException e) {
        System.out.println(e.getMessage());
    }

}

From source file:com.kamike.misc.MailUtils.java

public static void sendSystemMail(String email, String msg) {
    SimpleEmail mail = new SimpleEmail();
    if (SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL_SMTP) == null
            && SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL) == null
            && SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL_ACCOUNT) == null
            && SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL_PASSWORD) == null) {
        //???/* ww w .j av a 2 s.  c  om*/
        return;
    }
    try {
        mail.setHostName(SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL_SMTP));
        mail.addTo(email, email);
        mail.setFrom(SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL), "?");
        mail.setAuthentication(SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL_ACCOUNT),
                SystemConfig.getInstance().getParameter(SystemParam.SYSTEM_EMAIL_PASSWORD));
        mail.setSubject("??");
        mail.setMsg(msg);
        String a = mail.send();
        System.out.println(a);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:model.Email.java

public static void sendConfirmation(String receiver, String token, PersonalAccount account)
        throws EmailException {

    SimpleEmail email = new SimpleEmail();
    String message = "Sua conta foi criada com sucesso, porm ainda est inativa.\n";
    message += "Nmero da conta: " + account.getNumber() + "\n";
    message += "Agncia: " + account.getAgency().getNumber() + "\n";
    message += "Token de autentificao: " + token + "\n";
    message += "Com este token voc  capaz de definir uma senha para sua conta para, ento, utiliz-la.\n";
    message += "\nCaso voc perca este token, entre novamente com as informaes para abrir conta,";
    message += " e um novo token ser gerado e reenviado  voc.";

    try {//from  ww  w.j av  a2s  . com
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("totheworldgroup@gmail.com", "albrcalu"));
        email.setSSLOnConnect(true);
        email.setFrom("totheworldgroup@gmail.com");
        email.setSubject("Token de autentificao");
        email.setMsg(message);
        email.addTo(receiver);
        email.send();

    } catch (EmailException e) {
        System.out.println(e.getMessage());
    }

}

From source file:colossal.util.EmailAlerter.java

@Override
public void pipeCompletion(String pipeName, String summary) {
    System.out.println("Pipe completion: " + pipeName);
    System.out.println(summary);//from w  w  w.j  a  v a 2  s.  c  om
    if (!emailEnabled)
        return;

    try {
        SimpleEmail email = makeEmail("Pipe completion: " + pipeName);
        email.setMsg(summary);
        email.send();
    } catch (EmailException e) {
        System.err.println("Can't send email!");
        e.printStackTrace();
    }
}

From source file:io.werval.modules.smtp.SmtpPluginTest.java

@Test
public void testSimpleEmail() throws EmailException, MessagingException {
    Smtp smtp = WERVAL.application().plugin(Smtp.class);
    assertThat(smtp, notNullValue());//from  w w  w .j av a 2s . c o  m

    SimpleEmail email = smtp.newSimpleEmail();
    email.setFrom("from@werval.io");
    email.addTo("to@werval.io");
    email.setSubject("Simple Email");
    email.send();

    List<WiserMessage> messages = wiser.getMessages();
    assertThat(messages.size(), is(1));
    WiserMessage message = messages.get(0);
    assertThat(message.getMimeMessage().getHeader("From")[0], equalTo("from@werval.io"));
    assertThat(message.getMimeMessage().getHeader("To")[0], equalTo("to@werval.io"));
    assertThat(message.getMimeMessage().getHeader("Subject")[0], equalTo("Simple Email"));

    MetricRegistry metrics = WERVAL.application().plugin(Metrics.class).metrics();
    assertThat(metrics.meter("io.werval.modules.smtp.sent").getCount(), is(1L));
    assertThat(metrics.meter("io.werval.modules.smtp.errors").getCount(), is(0L));

    HealthCheckRegistry healthChecks = WERVAL.application().plugin(Metrics.class).healthChecks();
    assertTrue(healthChecks.runHealthCheck("smtp").isHealthy());
}

From source file:com.moss.error_reporting.server.Notifier.java

public void sendPlainEmail(ReportId id, ErrorReport report) {
    try {/*from w w w .j  a  va  2  s .  co  m*/
        SimpleEmail email = new SimpleEmail();
        prepEmail(email, id);
        email.setMsg("hello world");
        email.send();
    } catch (EmailException e) {
        e.printStackTrace();
    }

}

From source file:com.qatickets.service.MailService.java

public void sendToAdmin(String subj, String text) throws Exception {
    SimpleEmail email = new SimpleEmail();
    email.setSmtpPort(port);// w w  w .  j  a  v  a  2s  .c o m
    email.setHostName(host);
    email.setSubject(subj);
    email.setMsg(text);
    email.addTo(adminToEmail, "Admin");
    //      email.setFrom(systemOwner, "QATickets.com");
    email.send();
}

From source file:colossal.util.EmailAlerter.java

@Override
public void alert(List<PhaseError> errors) {
    System.err.format("%sAlert: can't run pipeline\n", emailEnabled ? "Emailing " : "");
    for (PhaseError e : errors) {
        System.err.println(e.getMessage());
    }//from   w w w  .  ja v  a 2s.com
    if (!emailEnabled) {
        System.err.println("Can't email - not configured");
        return;
    }

    try {
        SimpleEmail email = makeFailureEmail();
        StringBuilder msg = new StringBuilder();
        for (PhaseError e : errors) {
            msg.append(e.getMessage()).append('\n');
        }
        email.setMsg(msg.toString());
        email.send();
    } catch (EmailException e) {
        System.err.println("Can't send email!");
        e.printStackTrace();
    }
}