Example #1
0
 public void sendEmailUsingJava(String to, String content, String subjectContext) {
   // int smtpPort = 465;
   // int smtpPort = 587;
   // String smtpHost = "smtp.gmail.com";
   // int smtpPort = 25;
   // String smtpHost = "exsmtp.wvu.edu";
   String subject = constants.getString(subjectContext);
   int smtpPort = Integer.parseInt(constants.getString("smtpPort"));
   String smtpHost = constants.getString("smtpHost");
   String urPassword = constants.getString("password");
   String from = constants.getString("emailaddress");
   // String content = contentText;
   // Create a mail session
   java.util.Properties props = new java.util.Properties();
   props = System.getProperties();
   props.put("mail.smtp.host", smtpHost);
   // props.put("mail.smtp.starttls.enable", "true");
   props.put("mail.smtps.auth", "true");
   SmtpAuthenticator authentication = new SmtpAuthenticator();
   // Session session = Session.getInstance(props, null);
   Session session = Session.getInstance(props, authentication);
   session.setDebug(true);
   // Construct the message
   try {
     MimeMessage msg = new MimeMessage(session);
     msg.setFrom(new InternetAddress(from));
     msg.setRecipient(RecipientType.TO, new InternetAddress(to));
     msg.setSubject(subject);
     msg.setText(content);
     msg.setSentDate(new Date());
     Transport tr = null;
     tr = session.getTransport("smtp");
     tr.connect(smtpHost, smtpPort, from, urPassword);
     tr.sendMessage(msg, msg.getAllRecipients());
   } catch (AddressException e) {
     System.out.println(e.getClass().getName() + ": " + e.getMessage());
     Log.info(e.getClass().getName() + ": " + e.getMessage());
     StackTraceElement[] trace = e.getStackTrace();
     for (int i = 0; i < trace.length; i++) {
       System.out.println("\t" + trace[i].toString());
       Log.info("\n\t" + trace[i].toString());
     }
     e.printStackTrace();
   } catch (MessagingException e) {
     System.out.println(e.getClass().getName() + ": " + e.getMessage());
     Log.info(e.getClass().getName() + ": " + e.getMessage());
     StackTraceElement[] trace = e.getStackTrace();
     for (int i = 0; i < trace.length; i++) {
       System.out.println("\t" + trace[i].toString());
       Log.info("\n\t" + trace[i].toString());
     }
     e.printStackTrace();
   }
 }