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

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

Introduction

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

Prototype

public Email setFrom(final String email) throws EmailException 

Source Link

Document

Set the FROM field of the email to use the specified address.

Usage

From source file:jobs.Utils.java

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

    SimpleEmail email = new SimpleEmail();
    try {//from w  w w  .ja  v a  2 s  .c o m
        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:controllers.PasswordResetApp.java

private static boolean sendPasswordResetMail(User user, String hashString) {
    Configuration config = play.Play.application().configuration();
    String sender = config.getString("smtp.user") + "@" + config.getString("smtp.domain");
    String resetPasswordUrl = getResetPasswordUrl(hashString);

    try {/*from   w  ww .  j  av a  2s .  c  o  m*/
        SimpleEmail email = new SimpleEmail();
        email.setFrom(sender)
                .setSubject(
                        "[" + utils.Config.getSiteName() + "] " + Messages.get("site.resetPasswordEmail.title"))
                .addTo(user.email)
                .setMsg(Messages.get("site.resetPasswordEmail.mailContents") + "\n\n" + resetPasswordUrl)
                .setCharset("utf-8");

        Logger.debug("password reset mail send: " + Mailer.send(email));
        return true;
    } catch (EmailException e) {
        e.printStackTrace();
        return false;
    }
}

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  . jav a 2 s.c  o  m*/
        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/* w ww.  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:com.bc.appbase.ui.dialog.SendReportAction.java

private static Email getEmail(String host, int port, String emailAddress, String password, String subject) {

    final SimpleEmail email = new SimpleEmail();

    try {// w w w  .  j  a v a 2s .  c o  m
        email.addTo(emailAddress);
    } catch (EmailException e) {
        logger.log(Level.WARNING,
                "Error add `to address`: " + emailAddress + " to email with subject: " + subject, e);
        return null;
    }

    if (password != null) {
        email.setAuthentication(emailAddress, password);
    }
    email.setDebug(logger.isLoggable(Level.FINE));

    try {
        email.setFrom(emailAddress);
    } catch (EmailException e) {
        logger.log(Level.WARNING,
                "Error setting `from address` = " + emailAddress + " to email with subject: " + subject, e);
        return null;
    }

    email.setHostName(host);
    email.setSmtpPort(port);

    if (password != null) {
        email.setSSLOnConnect(true);
    }

    email.setSubject(subject);

    return email;
}

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 {//from w  w  w.j a v  a 2  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:controllers.CNAController.java

public static void inputMT(String mtset) {
    try {/*from  w ww  . j a  v a 2  s . c o m*/
        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 {//  w w  w  .j  a va  2s  . c  om
        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: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>
 * //from  www. ja  v a 2s .  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: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 {/*  w w w.  jav 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());
    }

}