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

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

Introduction

In this page you can find the example usage for org.apache.commons.mail Email 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:AccessControl.java

/**
 * Send email message for external notifications
 *
 * @param subject Email Subject Text/*from w w  w.  ja  v a 2 s .c  o  m*/
 * @param message Email Message Text
 */
private static void sendEmail(String subject, String message) {

    try {
        if (EMAIL_ENABLED && EMAIL_FROM != null && !EMAIL_FROM.isEmpty()) {
            Email email = new SimpleEmail();
            email.setHostName(EMAIL_SERVER);
            email.setSmtpPort(EMAIL_PORT);
            email.setFrom(EMAIL_FROM);
            email.addTo(EMAIL_TO);
            email.setSubject(subject);
            email.setMsg(message);
            email.send();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:at.treedb.util.Mail.java

public static void sendMail(String smtpHost, int smtpPort, TransportSecurity transportSecurity, String smtpUser,
        String smtpPassword, String mailTo, String mailFrom, String subject, String message) throws Exception {
    Objects.requireNonNull(mailTo, "Mail.sendMail(): mailTo can not be null!");
    Objects.requireNonNull(subject, "Mail.sendMail(): subject can not be null!");
    Objects.requireNonNull(message, "Mail.sendMail(): message can not be null!");
    Email email = new SimpleEmail();
    email.setHostName(smtpHost);/* w w w. ja  v a  2 s  .co m*/

    email.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword));
    if (transportSecurity == TransportSecurity.SSL) {
        email.setSSLOnConnect(true);
        email.setSSLCheckServerIdentity(false);
    } else if (transportSecurity == TransportSecurity.STARTTLS) {
        email.setStartTLSRequired(true);
    }
    email.setSmtpPort(smtpPort);
    if (mailFrom != null && !mailFrom.isEmpty()) {
        email.setFrom(smtpUser);
    }
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(mailTo);
    email.send();
}

From source file:FacultyAdvisement.StudentRepository.java

public static void adminUpdate(DataSource ds, Student student, String oldUsername) throws SQLException {
    Connection conn = ds.getConnection();
    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }//  w ww  .j  av a  2 s. c  o  m
    try {

        PreparedStatement ps;
        ps = conn.prepareStatement(
                "Update STUDENT set EMAIL=?, FIRSTNAME=?, LASTNAME=?, MAJORCODE=?, PHONE=?, ADVISED=? where STUID=?");
        ps.setString(1, student.getUsername());
        ps.setString(2, student.getFirstName());
        ps.setString(3, student.getLastName());
        ps.setString(4, student.getMajorCode());
        ps.setString(5, student.getPhoneNumber());
        if (student.isAdvised()) {
            ps.setString(6, "true");
        } else {
            ps.setString(6, "false");
        }
        ps.setString(7, student.getId());
        ps.executeUpdate();

        ps = conn.prepareStatement("Update USERTABLE set USERNAME=? where USERNAME=?");
        ps.setString(1, student.getUsername());
        ps.setString(2, oldUsername);
        ps.executeUpdate();

        ps = conn.prepareStatement("Update GROUPTABLE set USERNAME=? where USERNAME=?");
        ps.setString(1, student.getUsername());
        ps.setString(2, oldUsername);
        ps.executeUpdate();

        if (student.isResetPassword()) {
            String newPassword = UUID.randomUUID().toString();
            String encryptedPassword = SHA256Encrypt.encrypt(newPassword);
            ps = conn.prepareStatement("Update USERTABLE set PASSWORD=? where USERNAME=?");
            ps.setString(1, encryptedPassword);
            ps.setString(2, student.getUsername());
            ps.executeUpdate();

            Email email = new HtmlEmail();
            email.setHostName("smtp.googlemail.com");
            email.setSmtpPort(465);
            email.setAuthenticator(new DefaultAuthenticator("uco.faculty.advisement", "!@#$1234"));
            email.setSSLOnConnect(true);
            email.setFrom("uco.faculty.advisement@gmail.com");
            email.setSubject("UCO Faculty Advisement Password Change");
            email.setMsg("<font size=\"3\">An admin has resetted your password, your new password is \""
                    + newPassword + "\"." + "\n<p align=\"center\">UCO Faculty Advisement</p></font>");
            email.addTo(student.getUsername());
            email.send();

        }

    } catch (EmailException ex) {
        Logger.getLogger(StudentRepository.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        conn.close();
    }

    //students = (HashMap<String, StudentPOJO>) readAll(); // reload the updated info
}

From source file:controller.SendMailMachine.java

public static void sendMail(Cart cart, String recipientEmail) {
    Email email = new SimpleEmail();

    email.setHostName(HOST_NAME);//  w w  w.  j  ava2 s  . c o m
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator(EMAIL_SENDER, PASSWORD));
    email.setSSLOnConnect(true);

    String subject = "Thng tin ha n";
    String content = "\t\t\tTRUNG TM MUA SM BITTORRENT\n\nChi tit n hng ca bn: \n";
    for (Map.Entry<Integer, Item> entrySet : cart.getCartItem().entrySet()) {
        Item item = entrySet.getValue();
        content += item.getWatch().getName() + "\t\t\t\t\t\t" + item.getQuantity() + " x "
                + ((int) item.getWatch().getPrice()) + "000 VND\n";
    }
    content += "Tng ha n: " + ((int) cart.sumPrice()) + "000 VND";
    try {
        email.setFrom(EMAIL_SENDER);
        email.setCharset("UTF-8");
        email.setSubject(subject);
        email.setMsg(content);

        email.addTo(recipientEmail);

        email.send();
    } catch (EmailException ex) {

        ex.printStackTrace();
    }
}

From source file:com.clavain.alerts.Methods.java

private static void sendMail(String title, String message, String emailaddy) {
    try {/*from w  ww  . j  ava  2s . c o  m*/
        Email email = new SimpleEmail();
        email.setHostName(p.getProperty("mailserver.host"));
        email.setSmtpPort(Integer.parseInt(p.getProperty("mailserver.port")));
        if (p.getProperty("mailserver.useauth").equals("true")) {
            email.setAuthentication(p.getProperty("mailserver.user"), p.getProperty("mailserver.pass"));
        }
        if (p.getProperty("mailserver.usessl").equals("true")) {
            email.setSSLOnConnect(true);
        } else {
            email.setSSLOnConnect(false);
        }
        email.setFrom(p.getProperty("mailserver.from"));
        email.setSubject("[MuninMX] " + title);
        email.setMsg(message);
        email.addTo(emailaddy);
        email.send();
    } catch (Exception ex) {
        logger.warn("Unable to send Mail: " + ex.getLocalizedMessage());
    }
}

From source file:ch.sbb.releasetrain.utils.emails.SMTPUtilImpl.java

@Override
public void send(String absender, String empfaenger, String betreff, String text) {
    try {// w ww  .j  a  v a 2  s . c  o  m
        final Email email = new SimpleEmail();
        email.setHostName(mailhost);
        email.setSmtpPort(mailport);
        email.setFrom(absender);
        email.setSubject(betreff);
        email.setMsg(text);
        email.addTo(empfaenger);
        email.send();
        log.info("mail sent to: " + empfaenger);
    } catch (final EmailException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:br.com.recursive.biblioteca.servicos.EmailService.java

public void sendHtmlEmail(Pessoa pessoa) throws EmailException {

    Email email = new HtmlEmail();
    email.setAuthenticator(new DefaultAuthenticator("claupwd@gmail.com", "@claupwd2014"));
    email.setHostName("smtp.gmail.com");
    email.setFrom("claupwd@gmail.com");
    email.setSubject("SIB Online - Recuperao de Senha");
    email.setMsg(createMessage(pessoa));
    email.addTo(pessoa.getContato().getEmail());
    email.setSSL(true);/*  w ww  .  j a  va2  s  .  c  om*/
    //Se true, exibe na saida todo o processo do envio do email
    email.setDebug(true);
    email.send();
}

From source file:com.patrolpro.beans.ContactUsBean.java

public String sendEmail() {
    try {/*from   w w  w . ja  v a2 s  .  c  om*/
        StringBuilder emailMessage = new StringBuilder();
        emailMessage.append("From: " + this.name + "\r\n");
        emailMessage.append("Email: " + this.email + "\r\n");
        emailMessage.append("Phone: " + this.phone + "\r\n");
        emailMessage.append(message);

        Email htmlEmail = new SimpleEmail();
        htmlEmail.setMsg(message);
        htmlEmail.setFrom("contact@patrolpro.com");
        htmlEmail.setSubject("Contact Us Email");
        htmlEmail.addTo("rharris@ainteractivesolution.com");
        htmlEmail.addCc("ijuneau@ainteractivesolution.com");
        htmlEmail.addCc("jc@champ.net");
        htmlEmail.setMsg(emailMessage.toString());

        htmlEmail.setAuthenticator(new MailAuthenticator("schedfox", "Sch3dF0x4m3"));
        htmlEmail.setHostName("mail2.champ.net");
        htmlEmail.setSmtpPort(587);
        htmlEmail.send();
        return "sentContactEmail";
    } catch (Exception exe) {

    }
    return "invalid";
}

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  w w w . j a v  a  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  .  j a  v  a 2  s .  com*/
            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);
        }
    }
}