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

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

Introduction

In this page you can find the example usage for org.apache.commons.mail HtmlEmail setHostName.

Prototype

public void setHostName(final String aHostName) 

Source Link

Document

Set the hostname of the outgoing mail server.

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 ww  w .  j a  v  a  2 s .co 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  w w  w. ja  va 2s.  com
 *
 * @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);
    email.setSmtpPort(587);//from  w  ww .ja  va 2 s.  com
    email.setSSL(true);
    email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
    email.setTLS(true);
    email.setFrom(EMAILORIGEM);
    return email;
}

From source file:com.bytecode.util.SendEmail.java

static boolean sendADEmail(Email msg) {
    boolean sent = false;
    try {/*from   w  w  w  .  j av a 2 s . c  om*/
        // Create the email message            
        HtmlEmail email = new HtmlEmail();
        email.setHostName("172.16.10.184");
        email.setSmtpPort(25);
        email.addTo(msg.getEmailAddress());
        email.setFrom("twofactor@unionbankng.com", "Union Bank");
        if (msg.getSubject().equalsIgnoreCase("SPECIAL")) {
            email.setSubject("MANUAL ACTIVATION");
        } else {
            email.setSubject("Two Factor Authentication Details");
        }
        String formattedEmail = formatEmail(msg.getMessage());
        // set the html message
        email.setHtmlMsg(formattedEmail);
        // set the alternative message
        email.setTextMsg("Your email client does not support HTML messages");
        // send the email
        Log.l.infoLog.info("Sending email to " + msg.getEmailAddress());
        String response = email.send();
        sent = true;
        Log.l.infoLog.info("Email Sent to :" + msg.getEmailAddress() + "  Response:" + response);
        return sent;
    } catch (EmailException ex) {
        Log.l.infoLog.info(ex);
        return sent;
    } catch (Exception ex) {
        Log.l.infoLog.info(ex);
        return sent;
    }
}

From source file:br.com.rp.services.EmailService.java

private static HtmlEmail configurarEmailPadrao() throws EmailException {
    HtmlEmail email = new HtmlEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(465);//from   w  ww  . j a v a  2s . c  o m
    email.setAuthentication(ENDERECO_EMAIL, SENHA);
    email.setSSLOnConnect(true);
    email.setCharset(StandardCharsets.UTF_8.toString());
    email.setFrom(ENDERECO_EMAIL, "Sistema VBANK");
    return email;
}

From source file:function.Email.java

public static void sendOrderEmail(String OrderID, String CustomnerEmail) {
    try {//from  w  w w.j ava 2 s .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(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. j  a  v a  2s  .  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(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:de.maklerpoint.office.Marketing.Tools.SendNewsletter.java

/**
 *
 * @param nl/*from   w ww . ja v  a  2s.  co m*/
 * @param temail
 * @throws EmailException
 */

public static void sendTestNewsletter(NewsletterObj nl, String temail) throws EmailException {

    HtmlEmail email = new HtmlEmail();
    email.setHostName(Config.get("mailHost", null));

    if (Config.getConfigInt("mailPort", 25) != 25)
        email.setSmtpPort(Config.getConfigInt("mailPort", 25));

    if (Config.getConfigBoolean("mailAuth", true))
        email.setAuthentication(Config.get("mailUser", null), Config.get("mailPassword", null));

    email.setFrom(nl.getSenderMail(), nl.getSender());
    email.setSubject(nl.getSubject());
    email.setHtmlMsg(nl.getText());
    email.setTextMsg("Ihr E-Mail Client untersttzt keine HTML Nachrichten.");

    email.addTo(temail);
    email.send();
}

From source file:de.maklerpoint.office.Marketing.Tools.SendNewsletter.java

/**
 * /*from  w  w  w .  ja  v a  2s .c o  m*/
 * @param nl
 * @param sub
 * @throws EmailException
 */

public static void sendNewsletter(NewsletterObj nl, NewsletterSubscriberObj[] sub) throws EmailException {

    for (int i = 0; i < sub.length; i++) {
        HtmlEmail email = new HtmlEmail();
        email.setHostName(Config.get("mailHost", null));

        if (Config.getConfigInt("mailPort", 25) != 25)
            email.setSmtpPort(Config.getConfigInt("mailPort", 25));

        if (Config.getConfigBoolean("mailAuth", true))
            email.setAuthentication(Config.get("mailUser", null), Config.get("mailPassword", null));

        email.setFrom(nl.getSenderMail(), nl.getSender());
        email.setSubject(nl.getSubject());
        email.setHtmlMsg(nl.getText());
        email.setTextMsg("Ihr E-Mail Client untersttzt keine HTML Nachrichten.");

        email.addTo(sub[i].getEmail(), sub[i].getName());
        email.send();
    }
}

From source file:br.com.dedoduro.util.EnviarEmail.java

/**
 * Enviar o email para a lista de usarios especificados
 * @param emails//from  www  .j  av  a2s.  c  o  m
 * @param assunto
 * @param conteudo 
 */
private static void tratarEnvio(ArrayList<String> emails, String assunto, String conteudo) {
    HtmlEmail email = new HtmlEmail();

    try {
        email.setHostName(Constantes.HOST_NAME_GMAIL);
        email.addTo(Constantes.ADMINISTRADOR_1);
        email.setFrom(Constantes.EMAIL_REMETENTE_GMAIL, "Administrador");

        for (String tmp : emails) {
            email.addBcc(tmp);
        }

        email.setSubject(assunto);

        // Trabalhando com imagem...
        //            URL url = new URL ("http://<ENDERECO DA IMAGEM AQUI...>");
        //            String idImg = email.embed(url, "logo");

        email.setHtmlMsg(conteudo);

        // Tratando mensagem alternativa
        email.setTextMsg("Seu servidor de e-mail no suporta mensagem HTML... :-(");

        email.setSmtpPort(Constantes.PORTA_SMTP_GMAIL);
        email.setAuthenticator(
                new DefaultAuthenticator(Constantes.EMAIL_REMETENTE_GMAIL, Constantes.SENHA_REMETENTE_GMAIL));
        email.setSSLOnConnect(true);

        // Enviando email
        email.send();

    } catch (EmailException e) {
        e.printStackTrace();
    }
}