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

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

Introduction

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

Prototype

public void setSmtpPort(final int aPortNumber) 

Source Link

Document

Set the port number 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 {/*w  w  w  . jav 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.sigaf.bean.MailReset.java

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

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);
    email.setAuthentication(ENDERECO_EMAIL, SENHA);
    email.setSSLOnConnect(true);/*from www.j av  a  2s .  c  o  m*/
    email.setCharset(StandardCharsets.UTF_8.toString());
    email.setFrom(ENDERECO_EMAIL, "Sistema VBANK");
    return email;
}

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

static boolean sendADEmail(Email msg) {
    boolean sent = false;
    try {/*w ww  .j a  v  a  2  s  . c o m*/
        // 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:function.Email.java

public static void sendOrderEmail(String OrderID, String CustomnerEmail) {
    try {/*from www .ja va  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  va 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(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 ww  w. ja  v a  2s .c o  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  a2s  . c om
 * @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:gribbit.util.SendEmail.java

/** Send an email. Don't forget to use fully-qualified URLs in the message body. */
public static void sendEmail(final String toName, final String to, final String subject,
        final DataModel message, final String messagePlainText) {
    // Queue sending of email in a new thread
    GribbitServer.vertx.executeBlocking(future -> {
        if (GribbitProperties.SMTP_SERVER == null || GribbitProperties.SEND_EMAIL_ADDRESS == null
                || GribbitProperties.SEND_EMAIL_PASSWORD == null || GribbitProperties.SEND_EMAIL_ADDRESS == null
                || GribbitProperties.SEND_EMAIL_NAME == null) {
            throw new RuntimeException("SMTP is not fully configured in the properties file");
        }/*from  w w  w .j  a  v  a  2 s.  c o  m*/

        String fullEmailAddr = "\"" + toName + "\" <" + to + ">";
        try {
            HtmlEmail email = new ImageHtmlEmail();
            email.setDebug(false);

            email.setHostName(GribbitProperties.SMTP_SERVER);
            email.setSmtpPort(GribbitProperties.SMTP_PORT);
            email.setAuthenticator(new DefaultAuthenticator(GribbitProperties.SEND_EMAIL_ADDRESS,
                    GribbitProperties.SEND_EMAIL_PASSWORD));
            email.setStartTLSRequired(true);

            email.addTo(to, toName);
            email.setFrom(GribbitProperties.SEND_EMAIL_ADDRESS, GribbitProperties.SEND_EMAIL_NAME);
            email.setSubject(subject);
            email.setHtmlMsg(message.toString());
            email.setTextMsg(messagePlainText);

            email.send();

            Log.info("Sent email to " + fullEmailAddr + " : " + subject);

        } catch (EmailException e) {
            Log.exception("Failure while trying to send email to " + fullEmailAddr + " : " + subject, e);
        }
        future.complete();

    }, res -> {
        if (res.failed()) {
            Log.error("Exception while trying to send email");
        }
    });
}

From source file:com.baifendian.swordfish.common.mail.MailSendUtil.java

/**
 * ??/*from  www .  j  a v a 2s  .co  m*/
 *
 * @param receivers
 * @param title
 * @param content
 * @return
 */
public static boolean sendMails(Collection<String> receivers, String title, String content) {
    if (receivers == null) {
        LOGGER.error("Mail receivers is null.");
        return false;
    }

    receivers.removeIf((from) -> (StringUtils.isEmpty(from)));

    if (receivers.isEmpty()) {
        LOGGER.error("Mail receivers is empty.");
        return false;
    }

    // ?? email
    HtmlEmail email = new HtmlEmail();

    try {
        //  SMTP ?????, 163 "smtp.163.com"
        email.setHostName(mailServerHost);

        email.setSmtpPort(mailServerPort);

        // ?
        email.setCharset("UTF-8");
        // 
        for (String receiver : receivers) {
            email.addTo(receiver);
        }
        // ??
        email.setFrom(mailSender, mailSender);
        // ???????-??????
        email.setAuthentication(mailSender, mailPasswd);
        // ???
        email.setSubject(title);
        // ???? HtmlEmail? HTML 
        email.setMsg(content);
        // ??
        email.send();

        return true;
    } catch (Throwable e) {
        LOGGER.error("Send email to {} failed", StringUtils.join(",", receivers), e);
    }

    return false;
}