package com.mrjw.guestbook; import java.util.Properties; import java.util.*; import javax.mail.*; import javax.mail.internet.*; import java.text.*; public class SendEmail { private String host; private String from; private String sendTo; private Vector sendToList; private String messToSend; private String subToSend; public SendEmail() { // Get system properties subToSend = "New email has arrived"; sendToList = new Vector(); } public String getMessToSend() {return messToSend; } public Vector getSendToList() {return sendToList; } public String getSendTo() {return sendTo; } public String getSubToSend() {return subToSend; } public String getTime() { String monthTwoDigits=""; String dayTwoDigits=""; //GregorianCalendar liftOffApollo11 = new GregorianCalendar(DateFormat.getDateTimeInstance() ); Date d = new Date(); DateFormat df1 = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT); String s1 = df1.format(d); String s2 = df2.format(d); Calendar c = Calendar.getInstance(); if (c.get(Calendar.MONTH) < 10) monthTwoDigits = "0"; if (c.get(Calendar.DAY_OF_MONTH) < 10) dayTwoDigits = "0"; System.out.println(c.get(Calendar.YEAR)+"-"+monthTwoDigits+c.get(Calendar.MONTH)+"-"+dayTwoDigits+c.get(Calendar.DAY_OF_MONTH)+" "+s2); return c.get(Calendar.YEAR)+"-"+monthTwoDigits+c.get(Calendar.MONTH)+"-"+dayTwoDigits+c.get(Calendar.DAY_OF_MONTH)+" "+s2; } public void sendMail() throws Exception { try { Properties props = System.getProperties(); // Setup mail server props.put("mail.smtp.host", host); // Get session Session session = Session.getInstance(props, null); // Define message MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(sendTo)); message.setSubject(subToSend); message.setText(messToSend); // Send message Transport.send(message); } catch (Exception e) {e.printStackTrace(); throw e; } } public void sendMailToList() throws Exception { try { Properties props = System.getProperties(); // Setup mail server props.put("mail.smtp.host", host); // Get session Session session = Session.getInstance(props, null); // Define message MimeMessage message; for (int x=0; x < sendToList.size(); x++) { message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(sendToList.elementAt(x).toString())); message.setSubject(subToSend); message.setText(messToSend); // Send message Transport.send(message); } // end of for loop } catch (Exception e) {e.printStackTrace(); throw e; } } public void setMessToSend(String messToSend) {this.messToSend = messToSend;} public void setSendToList(Vector sendToList) {this.sendToList = sendToList; } public void setSendTo(String sendTo) {this.sendTo = sendTo; } public void setSubToSend(String subToSend) {this.subToSend = subToSend;} public void setHost(String host) {this.host = host; } public void setFrom(String from) {this.from = from; } }