THe Java code in the attachment would pick up a list of employee details from a CSV file. If the physical access expiry date is less than 30 from the current date, It would send an email alert to the employee.
The CSV file would contain the following columns
Here is CsvReader Class
- Cardholder name(Employee name)
- Expiry Date
- EMAILID
- Status(active or inactive)
public static void main(String[] args) { try { // csv file containing data String strFile = "./Book2.csv"; Date tmpDate; // create BufferedReader to read csv file BufferedReader br = new BufferedReader(new FileReader(strFile)); String strLine = "", dateStr, empname, emailId, isActive; StringTokenizer st = null; List<employeebean> employeeList = new ArrayList<employeebean>(); Calendar calendar1 = getCalendarForNow(); calendar1.set(Calendar.DAY_OF_MONTH, calendar1.getActualMaximum(Calendar.DAY_OF_MONTH)); Calendar calendar2 = getCalendarForNow(); calendar2.set(Calendar.DAY_OF_MONTH, calendar1.getActualMinimum(Calendar.DAY_OF_MONTH)); Date end = calendar1.getTime(); Date start = calendar2.getTime(); System.out.println("end date =" + end.toString()); SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); // read comma separated file line by line int lineNo = 0; while ((strLine = br.readLine()) != null) { if (lineNo == 0) { lineNo += 1; continue; } st = new StringTokenizer(strLine, ","); EmployeeBean bean = null; while (st.hasMoreTokens()) { bean = new EmployeeBean(); empname = st.nextToken(); dateStr = st.nextToken(); emailId = st.nextToken(); isActive = st.nextToken(); tmpDate = sdf.parse(dateStr); bean.setEmployeeName(empname); bean.setExpiryDate(tmpDate); if (end.after(tmpDate) && start.before(tmpDate) &&isActive.equals("Active")) { employeeList.add(bean); } bean.setEmailId(emailId); } } System.out.println("Employee List : " + employeeList); EmailAlerter alerter = new EmailAlerter(); Iterator<employeebean> itr = employeeList.iterator(); while (itr.hasNext()) { EmployeeBean employee = (EmployeeBean) itr.next(); empname = employee.getEmployeeName(); emailId = employee.getEmailId(); dateStr = sdf.format(employee.getExpiryDate()); String from = "XXXXXXXXXX@gmail.com"; String to = emailId; String subject = "Regarding Physical security Access Expiry (Test)"; String mailBody = "Dear " + empname + "," + "nn your physical security access to BT module is getting expired on " + dateStr + "." + "nn Kindly check with your manager and furnish the following details to Acess POC for your team." + "nn Name: " + "nn EIN: " + "nn Employee Number: " + "nn Access card Number: " + "nn BT Email ID: " + "nn Email ID: "; alerter.sendMail(from, to, subject, mailBody); System.out.println("Email sent to " + empname + " " + emailId); // Thread.sleep(100000); } } catch (Exception e) { System.out.println("Exception Occured: " + e); } } private static Calendar getCalendarForNow() { Calendar calendar = GregorianCalendar.getInstance(); calendar.setTime(new Date()); return calendar; }
Here is CsvReader Class
package com.bt.caplan.CsvReader; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class EmailAlerter { public static void main(String[] args) { } public void sendMail(String from, String to,String subject,String mailBody) { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "XXXXXXXXXX@gmail.com", "XXXXXXX"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); message.setText(mailBody); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } } }