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

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

Introduction

In this page you can find the example usage for org.apache.commons.mail HtmlEmail 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:com.smi.travel.controller.mail.SendMail.java

public static void main(String args[]) throws Exception {
    EmailAttachment attachment = new EmailAttachment();
    HtmlEmail email = new HtmlEmail();
    try {//from w ww .  j  a v  a2  s  .c o  m
        //            attachment.setPath("C:\\Users\\chonnasith\\Desktop\\test.txt");
        //            attachment.setDescription("file attachment");
        //            attachment.setName("test.txt");
        //            email.attach(attachment);
        email.setHostName(hostname);
        email.setSmtpPort(port);
        email.setAuthentication(username, password);
        email.setSSLOnConnect(true);
        //            email.setStartTLSEnabled(true);
        //            email.setStartTLSRequired(true);
        email.setFrom(username);
        email.setSubject(subject);
        email.addTo(addto);
        email.setHtmlMsg(message);
        email.send();
        System.out.println("Email Send");
        System.out.println("Port : " + email.getSmtpPort());
    } catch (EmailException ex) {
        System.out.println("Email Exception");
        System.out.println("Port : " + email.getSmtpPort());
        ex.printStackTrace();
    }

    //        InetAddress host = InetAddress.getByName("mail.foobar.com");
    //        System.out.println("host.isReachable(1000) = " + host.isReachable(1000));

    //        int port = 587;
    //        String host = "smtp.gmail.com";
    //        String user = "username@gmail.com";
    //        String pwd = "email password";
    //
    //        try {
    //            Properties props = new Properties();
    //            // required for gmail 
    //            props.put("mail.smtp.starttls.enable","true");
    //            props.put("mail.smtp.auth", "true");
    //            // or use getDefaultInstance instance if desired...
    //            Session session = Session.getInstance(props, null);
    //            Transport transport = session.getTransport("smtp");
    //            transport.connect(host, port, user, pwd);
    //            transport.close();
    //            System.out.println("success");
    //         } 
    //         catch(AuthenticationFailedException e) {
    //               System.out.println("AuthenticationFailedException - for authentication failures");
    //               e.printStackTrace();
    //         }
    //         catch(MessagingException e) {
    //               System.out.println("for other failures");
    //               e.printStackTrace();
    //         }

    //        Properties prop=new Properties();
    //        prop.put("mail.smtp.auth", "true");
    //        prop.put("mail.smtp.host", "smtp.gmail.com");
    //        prop.put("mail.smtp.port", "587");
    //        prop.put("mail.smtp.starttls.enable", "true");
    //
    //        Session session = Session.getDefaultInstance(prop,
    //        new javax.mail.Authenticator() {
    //            protected PasswordAuthentication getPasswordAuthentication() {
    //                return new PasswordAuthentication("finance@wendytour", "wendytr");
    //          }
    //        });
    //        
    //        try {
    //            String body="Dear Renish Khunt Welcome";
    //            String htmlBody = "<strong>This is an HTML Message</strong>";
    //            String textBody = "This is a Text Message.";
    //            Message message = new MimeMessage(session);
    //            message.setFrom(new InternetAddress(username));
    //            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(addto));
    //            message.setSubject("Testing Subject");
    //            MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    //            mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    //            mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    //            mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    //            mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    //            mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    //            CommandMap.setDefaultCommandMap(mc);
    //            message.setText(htmlBody);
    //            message.setContent(textBody, "text/html");
    //            Transport.send(message);
    //
    //            System.out.println("Done");
    //
    //        } catch (MessagingException e) {
    //            e.printStackTrace();
    //        }

    //        final String fromEmail = "finance@wendytour"; //requires valid gmail id
    //        final String password = "wendytr"; // correct password for gmail id
    //        final String toEmail = "wee.chonnasith@gmail.com"; // can be any email id 
    //         
    //        System.out.println("TLSEmail Start");
    //        Properties props = new Properties();
    //        props.put("mail.smtp.host", ""); //SMTP Host
    //        props.put("mail.smtp.port", "587"); //TLS Port
    //        props.put("mail.smtp.auth", "true"); //enable authentication
    //        props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
    //         
    //                //create Authenticator object to pass in Session.getInstance argument
    //        Authenticator auth = new Authenticator() {
    //            //override the getPasswordAuthentication method
    //            protected PasswordAuthentication getPasswordAuthentication() {
    //                return new PasswordAuthentication(fromEmail, password);
    //            }
    //        };
    //        Session session = Session.getInstance(props, auth);
    //         
    //        EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject", "TLSEmail Testing Body");
    //         

}

From source file:com.duroty.utils.mail.MessageUtilities.java

/**
 * DOCUMENT ME!/*from www.j  a  va 2s . c o  m*/
 *
 * @param args DOCUMENT ME!
 *
 * @throws UnsupportedEncodingException DOCUMENT ME!
 * @throws EmailException DOCUMENT ME!
 * @throws MessagingException DOCUMENT ME!
 */
public static void main(String[] args) throws UnsupportedEncodingException, EmailException, MessagingException {
    InternetAddress[] aux1 = new InternetAddress[5];
    aux1[0] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[1] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[2] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[3] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");
    aux1[4] = new InternetAddress("duroty@iigov.net", "Jordi Marqus");

    System.out.println(MessageUtilities.decodeAddressesEmail(aux1));

    HtmlEmail email = new HtmlEmail();
    email.setHostName("10.0.0.68");
    email.setFrom("duroty@iigov.net");
    email.addReplyTo("duroty@iigov.net");

    email.addTo("cagao@ii.org");

    email.addCc("raul1@iigov.org");
    email.addCc("raul2@iigov.org");
    email.addCc("raul3@iigov.org");
    email.addCc("raul4@iigov.org");
    email.addCc("raul5@iigov.org");

    email.addBcc("caca1@iigov.org");

    email.setHtmlMsg("<html>la merda fa pudor</html>");

    email.buildMimeMessage();

    MimeMessage mime = email.getMimeMessage();

    System.out.println(MessageUtilities.decodeAddressesEmail(mime.getAllRecipients()));
}

From source file:com.sigaf.bean.MailReset.java

public static HtmlEmail conectaEmail() throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName(HOSTNAME);//  www  . j a  v a  2  s .c  om
    email.setSmtpPort(587);
    email.setSSL(true);
    email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
    email.setTLS(true);
    email.setFrom(EMAILORIGEM);
    return email;
}

From source file:function.Email.java

public static void sendOrderEmail(String OrderID, String CustomnerEmail) {
    try {/*from   ww  w.  j a v a2s  . c  om*/
        HtmlEmail email = new HtmlEmail();

        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator(MY_EMAIL, MY_PASSWORD));
        email.setSSLOnConnect(true);
        email.setFrom(MY_EMAIL);
        email.addTo(CustomnerEmail);
        email.setSubject("Thanh ton thnh cng.");
        email.setHtmlMsg("<html><h2>Ha n ca bn vi m ha n <font color='red'>" + OrderID
                + "</font>  thanh ton thnh cng.</h2>"
                + "<b>Cm n bn  ng h chng ti!!!</b></div>" + "</html>");
        email.setCharset("UTF-8");
        email.setTextMsg("Trnh duyt khng h tr nh dng html!");
        String a = email.send();
        System.out.println(a);
    } catch (EmailException eex) {
        System.err.println(eex);
    }
}

From source file:function.Email.java

public static void sendVerifyEmail(Customers customer, String hash) {
    try {//from   w  w w. ja  v  a  2s  .c  o  m
        HtmlEmail email = new HtmlEmail();

        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator(MY_EMAIL, MY_PASSWORD));
        email.setSSLOnConnect(true);
        email.setFrom(MY_EMAIL);
        email.addTo(customer.getEmail());
        email.setSubject("Xc nhn ti khon TiviStore");
        email.setHtmlMsg("<html><h2>Cho " + customer.getCustomerName() + ",</h2>"
                + "<div><b>Vui lng click vo <a href='http://localhost:8084/ThuongMaiDienTu/Verify?u="
                + customer.getCustomerID() + "&hash=" + hash
                + "'>y</a>  xc nhn ti khon...</b></div><br />"
                + "<b>Cm n bn  ng h!!!</b></div>" + "</html>");
        email.setCharset("UTF-8");
        email.setTextMsg("Trnh duyt khng h tr nh dng html!");
        String a = email.send();
        System.out.println(a);
    } catch (EmailException eex) {
        System.err.println(eex);
    }
}

From source file:com.ning.metrics.meteo.publishers.AlertListener.java

private void createAndSendAlertEmail(String body) {
    try {/*from www.  j  a va  2s  . c o  m*/
        log.info(String.format("Sending alert email to [%s]: %s", config.getRecipients(), body));

        HtmlEmail email = new HtmlEmail();

        email.setTextMsg(body);
        email.setFrom("esper-is-awesome@example.com");
        email.setTo(Arrays.asList(new InternetAddress(config.getRecipients())));
        email.setHostName(config.getHost());
        email.setSmtpPort(config.getPort());
        email.send();
    } catch (Exception ex) {
        log.warn("Could not create or send email", ex);
    }
}

From source file:com.dattack.jtoolbox.commons.email.HtmlEmailBuilder.java

/**
 * Builder method./*  w ww.  ja v  a2s.  c o m*/
 *
 * @return the HtmlEmail
 * @throws EmailException
 *             if an error occurs while creating the email
 */
public HtmlEmail build() throws EmailException {

    if (hostname == null || hostname.isEmpty()) {
        throw new EmailException(String.format("Invalid SMTP server (hostname: '%s')", hostname));
    }

    if (from == null || from.isEmpty()) {
        throw new EmailException(String.format("Invalid email address (FROM: '%s'", from));
    }

    final HtmlEmail email = new HtmlEmail();
    email.setHostName(hostname);
    email.setFrom(from);
    email.setSubject(subject);

    if (message != null && !message.isEmpty()) {
        email.setMsg(message);
    }

    if (port > 0) {
        email.setSmtpPort(port);
    }

    if (username != null && !username.isEmpty()) {
        email.setAuthenticator(new DefaultAuthenticator(username, password));
    }

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

    if (startTlsEnabled != null) {
        email.setStartTLSEnabled(startTlsEnabled);
    }

    if (!toList.isEmpty()) {
        email.setTo(toList);
    }

    if (!ccList.isEmpty()) {
        email.setCc(ccList);
    }

    if (!bccList.isEmpty()) {
        email.setBcc(bccList);
    }
    return email;
}

From source file:io.marto.aem.utils.email.FreemarkerTemplatedMailer.java

private HtmlEmail constructEmail(final String[] recipients, String sender, final String subject,
        String template, Object model) throws EmailException {
    final HtmlEmail email = new HtmlEmail();

    email.setMsg(renderBody(template, model));
    if (subject != null) {
        email.setSubject(subject);//from  w w  w. ja v a2s. c  o  m
    }
    if (sender != null) {
        email.setFrom(sender);
    }

    for (String recipient : recipients) {
        email.addTo(recipient);
    }

    return email;
}

From source file:br.com.atmatech.sac.controller.Email.java

public void emailAtendimento(String smtp, String user, String password, Integer porta, Boolean ssl, Boolean tls,
        String emailto, String emailfrom, String solicitante, String nchamado, String razao, String data,
        String solicitacao, String realizacao, String tecnico, String imagem)
        throws EmailException, MalformedURLException {
    System.err.println(smtp + ":\n" + user + ":\n" + password + ":\n" + porta + ":\n" + ssl + ":\n" + tls
            + ":\n" + emailto + ":\n" + emailfrom + ":\n" + solicitante + ":\n" + nchamado + ":\n" + razao
            + ":\n" + data + ":\n" + solicitacao + ":\n" + realizacao + ":\n" + tecnico + ":\n" + imagem);
    HtmlEmail email = new HtmlEmail();
    // SimpleEmail email = new SimpleEmail();
    email.setHostName(smtp); // o servidor SMTP para envio do e-mail
    email.addTo(emailto); //destinatrio
    email.setFrom(emailfrom); // remetente        
    email.setSubject("Aviso de Atendimento - Suporte");
    // configura a mensagem para o formato HTML        
    email.setHtmlMsg(/*from w w  w . java  2  s.  co m*/
            "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">Prezado(a)Senhor(a).<br>"
                    + "<b>" + solicitante + "</b><p>" + "Informamos que o protocolo nmero&#013<b> " + nchamado
                    + "</b> foi finalizado por nossa Central de Suporte.<p>" + "Cliente<br>" + "" + razao
                    + "<p>" + "Data<br>" + "" + data + "<p>" + "Descrio do Problema<br>" + "" + solicitacao
                    + "<p>" + "Soluo<br>" + "" + realizacao + "<p>" + "Atendente<br>" + "" + tecnico + "<p>"
                    + "<b>Atenciosamente</b> Suporte Atmatech<p><p>" + imagem + " </html>");
    //email.setMsg("Teste");
    email.setAuthentication(user, password);
    email.setSmtpPort(porta);
    email.setSSL(ssl);
    email.setTLS(tls);
    email.send();
}

From source file:it.vige.greenarea.test.mail.SendMailTest.java

@Test
public void testSendMailToGoogle() throws Exception {
    HtmlEmail email = new HtmlEmail();
    try {//  w  ww . jav a  2s .com
        email.setSubject("prova");
        email.setHtmlMsg("<div>ciao</div>");
        email.addTo("luca.stancapiano@vige.it");
        email.setSmtpPort(587);
        email.setHostName("smtp.gmail.com");
        email.setFrom("greenareavige@gmail.com");
        email.setAuthentication("greenareavige@gmail.com", "vulitgreenarea");
        email.setTLS(true);
        email.send();
    } catch (EmailException e) {
        fail();
    }

}