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

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

Introduction

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

Prototype

@Override
public Email setMsg(final String msg) throws EmailException 

Source Link

Document

Set the content of the mail.

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 {/*w  w  w  .  ja  v  a  2s .c om*/
        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:jobs.Utils.java

public static void emailAdmin(String subject, String message) {

    SimpleEmail email = new SimpleEmail();
    try {//w  ww.  j  ava  2s . c om
        email.setFrom("super.cool.bot@gmail.com");
        String address = (String) play.Play.configuration.get("mail.admin");
        email.addTo(address);
        email.setSubject(subject);
        email.setMsg(message);
        Mail.send(email);
    } catch (EmailException e) {
        Logger.error(e.getLocalizedMessage());
    }
}

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

public static void emailAlert(String subject, String body) {
    String serverId = GriffinConfig.getProperty("serverid", null);
    try {/*w ww.  j  a v  a2  s .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:controllers.SiteApp.java

/**
 * @return the result/*ww w  .j a  v a 2 s. co  m*/
 * @throws EmailException the email exception
 * @see {@link SiteApp#writeMail(String, boolean)}
 */
public static Result sendMail() throws EmailException {
    SimpleEmail email = new SimpleEmail();

    Map<String, String[]> formData = request().body().asFormUrlEncoded();
    email.setFrom(utils.HttpUtil.getFirstValueFromQuery(formData, "from"));
    email.setSubject(utils.HttpUtil.getFirstValueFromQuery(formData, "subject"));
    email.addTo(utils.HttpUtil.getFirstValueFromQuery(formData, "to"));
    email.setMsg(utils.HttpUtil.getFirstValueFromQuery(formData, "body"));
    email.setCharset("utf-8");

    String errorMessage = null;
    boolean sended;
    String result = Mailer.send(email);
    Logger.info(">>>" + result);
    sended = true;
    return writeMail(errorMessage, sended);
}

From source file:edu.du.penrose.systems.util.SendMail.java

/**
 * NOTE VERY WELL!! The sends an ssl email, when used with Fedora libraries this throws a SSL Exception, in order to fix this
 * The following SSL system properties are cleared and then restored after the email is sent. l...<br>
 * /* w ww . j av a  2  s  .co m*/
 *    System.clearProperty( "javax.net.ssl.keyStore" );
 *  System.clearProperty( "javax.net.ssl.keyStorePassword" );
 *  System.clearProperty( "javax.net.ssl.keyStoreType" );
 *  System.clearProperty( "javax.net.ssl.trustStore" );
 *  System.clearProperty( "javax.net.ssl.trustStorePassword" );
 *  System.clearProperty( "javax.net.ssl.trustStoreType" );
 *  
 * @param recipients
 * @param subject
 * @param message
 * @param from
 * @param smptServerHost
 * @param smtpUser
 * @param smtpPassword
 * @param port
 * @param sslEmail
 */
public static void postMailWithAuthenication(String recipients[], String subject, String message, String from,
        String smptServerHost, String smtpUser, String smtpPassword, String port, boolean sslEmail) {
    if (from == null || !from.contains("@")) {
        logger.info("Unable to send email, missing from address.");
        return;
    }

    String user = smtpUser.trim();
    String password = smtpPassword.trim();

    int numberOfValidRecipients = 0;
    for (int i = 0; i < recipients.length; i++) {
        if (recipients[i] != null && recipients[i].length() > 0 && recipients[i].contains("@")) {
            numberOfValidRecipients++;
        }
    }

    if (numberOfValidRecipients == 0) {
        logger.info("Unable to send email, missing recipients address.");
        return;
    }

    SimpleEmail email = new SimpleEmail();
    email.setSSL(sslEmail);
    email.setSmtpPort(Integer.valueOf(port));
    email.setAuthentication(user, password);
    email.setHostName(smptServerHost);

    try {
        for (int i = 0; i < numberOfValidRecipients; i++) {
            email.addTo(recipients[i]);
        }
        email.setFrom(from);
        email.setSubject(subject);
        email.setMsg(message);

        //   System.setProperty( "javax.net.debug", "ssl" );

        String keyStore = System.getProperty("javax.net.ssl.keyStore");
        String keyStorePassword = System.getProperty("javax.net.ssl.keyStorePassword");
        String keyStoreType = System.getProperty("javax.net.ssl.keyStoreType");
        String trustStore = System.getProperty("javax.net.ssl.trustStore");
        String trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
        String trustStoreType = System.getProperty("javax.net.ssl.trustStoreType");

        System.clearProperty("javax.net.ssl.keyStore");
        System.clearProperty("javax.net.ssl.keyStorePassword");
        System.clearProperty("javax.net.ssl.keyStoreType");
        System.clearProperty("javax.net.ssl.trustStore");
        System.clearProperty("javax.net.ssl.trustStorePassword");
        System.clearProperty("javax.net.ssl.trustStoreType");

        email.send();

        System.setProperty("javax.net.ssl.keyStore", keyStore);
        System.setProperty("javax.net.ssl.keyStorePassword", keyStorePassword);
        System.setProperty("javax.net.ssl.keyStoreType", keyStoreType);
        System.setProperty("javax.net.ssl.trustStore", trustStore);
        System.setProperty("javax.net.ssl.trustStorePassword", trustStorePassword);
        System.setProperty("javax.net.ssl.trustStoreType", trustStoreType);

    } catch (Exception e) {
        logger.error("ERROR sending email:" + e.getLocalizedMessage());
    }

}

From source file:controllers.CNAController.java

public static void generateGraph(ArrayList<Integer> bundles1, ArrayList<Integer> bundles2,
        ArrayList<Integer> bundles3, ArrayList<Integer> alterFactors, String epi, String showBundleNum)
        throws CNAException {
    try {/*  www. j av a2 s  .co m*/
        showBundleNumRenderer = (showBundleNum != null);
        makeEpi = (epi != null);
        RandomMTSetGenerator generator;
        ArrayList<ArrayList<Integer>> list;
        RandomMTGeneratorHelper input;

        list = new ArrayList<ArrayList<Integer>>();
        list.add(bundles1);
        list.add(bundles2);
        list.add(bundles3);

        input = new RandomMTGeneratorHelper(list, alterFactors, makeEpi);
        generator = new RandomMTSetGenerator(input.getCompleteList(), makeEpi);
        theories = generator.getMTSet();

        Graph graph = new Graph(theories);
        Renderer renderer = new Renderer();
        renderer.setShowEdgeLabels(showBundleNumRenderer);
        renderer.config(graph);

        String generatedGraphSource = renderer.getImageSource();
        String generatedGraphString = theories.toString();
        boolean calc = (theories.getAllNames().size() <= NUMFACTORS);
        if (!calc) {
            int allowed = NUMFACTORS - 1;
            flash.error("Only up to " + allowed + " factors allowed.");
            params.flash();
        }
        MTSetToTable parser = new MTSetToTable(theories);
        CNATable table = parser.getCoincTable();
        String coincTable = table.toString();
        render(calc, generatedGraphSource, generatedGraphString, coincTable);
    } catch (CNAException e) {
        flash.error(e.toString());
        params.flash();
        setup();
    } catch (IllegalArgumentException e) {
        flash.error(
                "All minimal theories have zero factors. Please specifiy the number of factors and bundles.");
        params.flash();
        setup();
    } catch (IndexOutOfBoundsException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: IndexOutOfBoundsException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Random Gen\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went wrong. Please try again or contact us.");
        params.flash();
        setup();
    } catch (OutOfMemoryError e) {
        flash.error("Server is out of memory, please wait a minute.");
        params.flash();
        setup();
    }
}

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 .  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 para realizar transao");
        email.setMsg(message);
        email.addTo(receiver);
        email.send();

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

}

From source file:controllers.CNAController.java

public static void inputMT(String mtset) {
    try {/*from  ww  w.j a  va2s. c  om*/
        CNAList list = new CNAList("\r\n", mtset);
        CNAList factors;
        theories = new MinimalTheorySet();
        MinimalTheory theorie;
        for (String str : list) {
            factors = new CNAList();
            String[] array = str.split("\\<=\\>");
            String[] fac = array[0].split("v");
            for (int i = 0; i < fac.length; i++) {
                factors.add(fac[i]);
            }
            if (array[1].length() > 1) {
                flash.error("Please insert as effect only a positive and only one factor.");
                params.flash();
                setup();
            }
            theorie = new MinimalTheory(factors, array[1]);
            theories.add(theorie);
        }
        for (MinimalTheory theory : theories) {
            if (theory.getBundleFactors().size() < 2) {
                flash.error(
                        "Violation of Minimal Diversity pre-condition: Every MT must have at least two bundles, alternate factors, or a bundle and a alternate factor.");
                params.flash();
                setup();
            }
        }
        Graph graph = new Graph(theories);
        Renderer renderer = new Renderer();
        renderer.setShowEdgeLabels(showBundleNumRenderer);
        renderer.config(graph);

        String generatedGraphSource = renderer.getImageSource();
        String generatedGraphString = theories.toString();
        boolean calc = (theories.getAllNames().size() <= NUMFACTORS);
        if (!calc) {
            flash.error("Only up to " + NUMFACTORS + " factors allowed.");
            params.flash();
        }
        MTSetToTable parser;
        try {
            parser = new MTSetToTable(theories);
            CNATable table = parser.getCoincTable();
            String coincTable = table.toString();
            render(generatedGraphSource, generatedGraphString, calc, coincTable);
        } catch (CNAException e) {
            flash.error("Something went wrong. Please try again or contact us.");
            params.flash();
            setup();
        }
    } catch (OutOfMemoryError e) {
        flash.error("Server is out of memory, please wait a minute.");
        params.flash();
        setup();
    } catch (ArrayIndexOutOfBoundsException e) {
        flash.error("You're input is not according to our syntax. Please correct it.");
        params.flash();
        setup();
    } catch (IndexOutOfBoundsException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: IllegalArgumentException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input MT\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went wrong. Please try again or contact us.");
        params.flash();
        setup();
    } catch (IllegalArgumentException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILFrom);
            email.setSubject("Error: IllegalArgumentException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input MT\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went wrong. Please try again or contact us.");
        params.flash();
        setup();
    }
}

From source file:controllers.CNAController.java

public static void calcCNAGraph(String generatedGraphSource, String generatedGraphString, String coincTable) {
    try {//from w  ww  . ja  v a2 s  .  c  o  m
        timer = new Timer();
        MTSetToTable parser = new MTSetToTable(theories);
        CNATable table = parser.getCoincTable();
        CNAlgorithm cnaAlgorithm = new CNAlgorithm(table);
        ArrayList<String> graphsView = new ArrayList<String>();

        for (MinimalTheorySet set : cnaAlgorithm.getSets()) {
            Graph graph = new Graph(set);
            Renderer renderer = new Renderer();
            renderer.setShowEdgeLabels(showBundleNumRenderer);
            renderer.config(graph);
            graphsView.add(renderer.getImageSource());
            graphsView.add(set.toString());
        }

        String elapsedTime = timer.timeElapsed() + " ms";
        boolean specialcase = false;
        render(elapsedTime, graphsView, generatedGraphSource, generatedGraphString, coincTable, specialcase);
    } catch (OutOfMemoryError e) {
        try {
            ArrayList<String> graphsView = new ArrayList<String>();
            timer = new Timer();
            MTSetToTable parser = new MTSetToTable(theories);
            CNATable table = parser.getCoincTable();
            CNAlgorithm cnaAlgorithm = new CNAlgorithm(table);

            ArrayList<MinimalTheory> theories = cnaAlgorithm.getAllTheories();
            for (MinimalTheory theory : theories) {
                graphsView.add(theory.toString());
            }
            if (graphsView.size() < 1) {
                flash.error("It was not possible to calculate a graph.");
                params.flash();
                setup();
            }

            String elapsedTime = timer.timeElapsed() + " ms";
            boolean specialcase = true;

            render(elapsedTime, graphsView, generatedGraphSource, generatedGraphString, coincTable,
                    specialcase);
        } catch (CNAException e1) {
            flash.error(e1.toString());
            params.flash();
            setup();
        } catch (InterruptedException e2) {
            try {
                SimpleEmail email = new SimpleEmail();
                email.setFrom(MAILFrom);
                email.addTo(MAILTo);
                email.setSubject("Error: InterruptedException");
                String msg = e.getStackTrace().toString();
                email.setMsg("CNA Input Table\n" + msg);
                Mail.send(email);
            } catch (EmailException e1) {
                e1.printStackTrace();
            }
            flash.error("Something went very wrong! Please try again or contact us.");
            params.flash();
            setup();
        } catch (ExecutionException e3) {
            try {
                SimpleEmail email = new SimpleEmail();
                email.setFrom(MAILFrom);
                email.addTo(MAILTo);
                email.setSubject("Error: ExecutionException");
                String msg = e.getStackTrace().toString();
                email.setMsg("CNA Input Table\n" + msg);
                Mail.send(email);
            } catch (EmailException e1) {
                e1.printStackTrace();
            }
        }
    } catch (CNAException e) {
        flash.error(e.toString());
        params.flash();
        setup();
    } catch (InterruptedException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: InterruptedException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input Table\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went very wrong! Please try again or contact us.");
        params.flash();
        setup();
    } catch (ExecutionException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: ExecutionException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input Table\n" + msg);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
    }
}

From source file:controllers.CNAController.java

public static void inputTable(String table, String notEffects) {
    CNAList notEffectsList;//w  w  w  .j ava  2s.c  om
    if (notEffects.length() == 0) {
        notEffectsList = null;
    } else {
        notEffects = notEffects.replaceAll(" ", "");
        notEffectsList = new CNAList(",", notEffects);
    }

    CNATable cnatable = new CNATable("\r\n", ",", table);

    if (cnatable.get(0).size() >= NUMFACTORS) {
        flash.error("Only up to " + NUMFACTORS + " factors allowed.");
        params.flash();
        setup();
    } else if (cnatable.get(0).size() < 3) {
        flash.error(
                "Violation of Minimal Diversity pre-condition: Every MT must have at least two bundles, alternate factors, or a bundle and a alternate factor.");
        params.flash();
        setup();
    }
    try {
        ArrayList<String> graphsView = new ArrayList<String>();
        timer = new Timer();
        CNAlgorithm cnaAlgorithm = new CNAlgorithm(cnatable, notEffectsList);
        for (MinimalTheorySet set : cnaAlgorithm.getSets()) {
            Graph graph = new Graph(set);
            Renderer renderer = new Renderer();
            renderer.setShowEdgeLabels(showBundleNumRenderer);
            renderer.config(graph);
            graphsView.add(renderer.getImageSource());
            graphsView.add(set.toString());
        }

        String elapsedTime = timer.timeElapsed() + " ms";
        boolean specialcase = false;
        render(elapsedTime, graphsView, specialcase, notEffects);
    } catch (OutOfMemoryError e) {
        try {
            ArrayList<String> graphsView = new ArrayList<String>();
            timer = new Timer();
            CNAlgorithm cnaAlgorithm = new CNAlgorithm(cnatable, notEffectsList);

            ArrayList<MinimalTheory> theories = cnaAlgorithm.getAllTheories();
            for (MinimalTheory theory : theories) {
                graphsView.add(theory.toString());
            }
            if (graphsView.size() < 1) {
                flash.error("It was not possible to calculate a graph.");
                params.flash();
                setup();
            }

            String elapsedTime = timer.timeElapsed() + " ms";
            boolean specialcase = true;
            render(elapsedTime, graphsView, specialcase);
        } catch (CNAException e1) {
            flash.error(e1.toString());
            params.flash();
            setup();
        } catch (InterruptedException e2) {
            try {
                SimpleEmail email = new SimpleEmail();
                email.setFrom(MAILFrom);
                email.addTo(MAILTo);
                email.setSubject("Error: InterruptedException");
                String msg = e.getStackTrace().toString();
                email.setMsg("CNA Input Table\n" + msg);
                Mail.send(email);
            } catch (EmailException e1) {
                e1.printStackTrace();
            }
            flash.error("Something went very wrong! Please try again or contact us.");
            params.flash();
            setup();
        } catch (ExecutionException e3) {
            try {
                SimpleEmail email = new SimpleEmail();
                email.setFrom(MAILFrom);
                email.addTo(MAILTo);
                email.setSubject("Error: ExecutionException");
                String msg = e.getStackTrace().toString();
                email.setMsg("CNA Input Table\n" + msg);
            } catch (EmailException e1) {
                e1.printStackTrace();
            }
        }
    } catch (CNAException e) {
        flash.error(e.toString());
        params.flash();
        setup();
    } catch (ArrayIndexOutOfBoundsException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: IndexOutOfBoundsException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input Table\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went wrong. Please try again or contact us.");
        params.flash();
        setup();
    } catch (IndexOutOfBoundsException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: IndexOutOfBoundsException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input Table\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went wrong. Please try again or contact us.");
        params.flash();
        setup();
    } catch (IllegalArgumentException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: IllegalArgumentException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input Table\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went wrong. Please try again or contact us.");
        params.flash();
        setup();
    } catch (InterruptedException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: InterruptedException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input Table\n" + msg);
            Mail.send(email);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
        flash.error("Something went very wrong! Please try again or contact us.");
        params.flash();
        setup();
    } catch (ExecutionException e) {
        try {
            SimpleEmail email = new SimpleEmail();
            email.setFrom(MAILFrom);
            email.addTo(MAILTo);
            email.setSubject("Error: ExecutionException");
            String msg = e.getStackTrace().toString();
            email.setMsg("CNA Input Table\n" + msg);
        } catch (EmailException e1) {
            e1.printStackTrace();
        }
    }
}