List of usage examples for org.apache.commons.mail HtmlEmail getSmtpPort
public String getSmtpPort()
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 ww . j av a 2 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.github.robozonky.notifications.EmailHandler.java
@Override public void send(final SessionInfo sessionInfo, final String subject, final String message, final String fallbackMessage) throws Exception { final HtmlEmail email = createNewEmail(sessionInfo); email.setSubject(subject);/*from ww w . j a v a 2 s. c o m*/ email.setHtmlMsg(message); email.setTextMsg(fallbackMessage); LOGGER.debug("Will send '{}' from {} to {} through {}:{} as {}.", email.getSubject(), email.getFromAddress(), email.getToAddresses(), email.getHostName(), email.getSmtpPort(), getSmtpUsername()); email.send(); }
From source file:com.baasbox.service.user.UserService.java
public static void sendResetPwdMail(String appCode, ODocument user) throws Exception { final String errorString = "Cannot send mail to reset the password: "; //check method input if (!user.getSchemaClass().getName().equalsIgnoreCase(UserDao.MODEL_NAME)) throw new PasswordRecoveryException(errorString + " invalid user object"); //initialization String siteUrl = Application.NETWORK_HTTP_URL.getValueAsString(); int sitePort = Application.NETWORK_HTTP_PORT.getValueAsInteger(); if (StringUtils.isEmpty(siteUrl)) throw new PasswordRecoveryException(errorString + " invalid site url (is empty)"); String textEmail = PasswordRecovery.EMAIL_TEMPLATE_TEXT.getValueAsString(); String htmlEmail = PasswordRecovery.EMAIL_TEMPLATE_HTML.getValueAsString(); if (StringUtils.isEmpty(htmlEmail)) htmlEmail = textEmail;//from w w w . j a v a2s .c om if (StringUtils.isEmpty(htmlEmail)) throw new PasswordRecoveryException(errorString + " text to send is not configured"); boolean useSSL = PasswordRecovery.NETWORK_SMTP_SSL.getValueAsBoolean(); boolean useTLS = PasswordRecovery.NETWORK_SMTP_TLS.getValueAsBoolean(); String smtpHost = PasswordRecovery.NETWORK_SMTP_HOST.getValueAsString(); int smtpPort = PasswordRecovery.NETWORK_SMTP_PORT.getValueAsInteger(); if (StringUtils.isEmpty(smtpHost)) throw new PasswordRecoveryException(errorString + " SMTP host is not configured"); String username_smtp = null; String password_smtp = null; if (PasswordRecovery.NETWORK_SMTP_AUTHENTICATION.getValueAsBoolean()) { username_smtp = PasswordRecovery.NETWORK_SMTP_USER.getValueAsString(); password_smtp = PasswordRecovery.NETWORK_SMTP_PASSWORD.getValueAsString(); if (StringUtils.isEmpty(username_smtp)) throw new PasswordRecoveryException(errorString + " SMTP username is not configured"); } String emailFrom = PasswordRecovery.EMAIL_FROM.getValueAsString(); String emailSubject = PasswordRecovery.EMAIL_SUBJECT.getValueAsString(); if (StringUtils.isEmpty(emailFrom)) throw new PasswordRecoveryException(errorString + " sender email is not configured"); try { String userEmail = ((ODocument) user.field(UserDao.ATTRIBUTES_VISIBLE_ONLY_BY_THE_USER)).field("email") .toString(); String username = (String) ((ODocument) user.field("user")).field("name"); //Random String sRandom = appCode + "%%%%" + username + "%%%%" + UUID.randomUUID(); String sBase64Random = new String(Base64.encodeBase64(sRandom.getBytes())); //Save on DB ResetPwdDao.getInstance().create(new Date(), sBase64Random, user); //Send mail HtmlEmail email = null; URL resetUrl = new URL(Application.NETWORK_HTTP_SSL.getValueAsBoolean() ? "https" : "http", siteUrl, sitePort, "/user/password/reset/" + sBase64Random); //HTML Email Text ST htmlMailTemplate = new ST(htmlEmail, '$', '$'); htmlMailTemplate.add("link", resetUrl); htmlMailTemplate.add("user_name", username); htmlMailTemplate.add("token", sBase64Random); //Plain text Email Text ST textMailTemplate = new ST(textEmail, '$', '$'); textMailTemplate.add("link", resetUrl); textMailTemplate.add("user_name", username); textMailTemplate.add("token", sBase64Random); email = new HtmlEmail(); email.setHtmlMsg(htmlMailTemplate.render()); email.setTextMsg(textMailTemplate.render()); //Email Configuration email.setSSL(useSSL); email.setSSLOnConnect(useSSL); email.setTLS(useTLS); email.setStartTLSEnabled(useTLS); email.setStartTLSRequired(useTLS); email.setSSLCheckServerIdentity(false); email.setSslSmtpPort(String.valueOf(smtpPort)); email.setHostName(smtpHost); email.setSmtpPort(smtpPort); email.setCharset("utf-8"); if (PasswordRecovery.NETWORK_SMTP_AUTHENTICATION.getValueAsBoolean()) { email.setAuthenticator(new DefaultAuthenticator(username_smtp, password_smtp)); } email.setFrom(emailFrom); email.addTo(userEmail); email.setSubject(emailSubject); if (BaasBoxLogger.isDebugEnabled()) { StringBuilder logEmail = new StringBuilder().append("HostName: ").append(email.getHostName()) .append("\n").append("SmtpPort: ").append(email.getSmtpPort()).append("\n") .append("SslSmtpPort: ").append(email.getSslSmtpPort()).append("\n") .append("SSL: ").append(email.isSSL()).append("\n").append("TLS: ").append(email.isTLS()) .append("\n").append("SSLCheckServerIdentity: ").append(email.isSSLCheckServerIdentity()) .append("\n").append("SSLOnConnect: ").append(email.isSSLOnConnect()).append("\n") .append("StartTLSEnabled: ").append(email.isStartTLSEnabled()).append("\n") .append("StartTLSRequired: ").append(email.isStartTLSRequired()).append("\n") .append("SubType: ").append(email.getSubType()).append("\n") .append("SocketConnectionTimeout: ").append(email.getSocketConnectionTimeout()).append("\n") .append("SocketTimeout: ").append(email.getSocketTimeout()).append("\n") .append("FromAddress: ").append(email.getFromAddress()).append("\n").append("ReplyTo: ") .append(email.getReplyToAddresses()).append("\n").append("BCC: ") .append(email.getBccAddresses()).append("\n").append("CC: ").append(email.getCcAddresses()) .append("\n") .append("Subject: ").append(email.getSubject()).append("\n") //the following line throws a NPE in debug mode //.append("Message: ").append(email.getMimeMessage().getContent()).append("\n") .append("SentDate: ").append(email.getSentDate()).append("\n"); BaasBoxLogger.debug("Password Recovery is ready to send: \n" + logEmail.toString()); } email.send(); } catch (EmailException authEx) { BaasBoxLogger.error("ERROR SENDING MAIL:" + ExceptionUtils.getStackTrace(authEx)); throw new PasswordRecoveryException( errorString + " Could not reach the mail server. Please contact the server administrator"); } catch (Exception e) { BaasBoxLogger.error("ERROR SENDING MAIL:" + ExceptionUtils.getStackTrace(e)); throw new Exception(errorString, e); } }