Monday, 17 November 2014

Send EMail reminder from Excel spreadsheet (Read TO Address from Excel sheet)

Description:  
Sending automatic mail from java code based on Date of births

1. Read Data from Excel sheet

2. Compare Date of Birth and Current Date
3. Fetch Name and mail Address  based on Date of birth and Current date (If equal)
4. Send mail to particular person (Send Mail with random images) with help of above deatils


Note :

Excel sheet content : (Date Format US English (mm/dd) )




 //Read Excel sheet compare Date of Birth and Current Date

package sample;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import java.util.Calendar;
import java.util.GregorianCalendar;

import java.util.*;


public class ReadExcel {

 
  private String inputFile;
  public ArrayList<String> items = new ArrayList();

 
  public void setInputFile(String inputFile) {
    this.inputFile = inputFile;
  }

  public void read() throws IOException  {
    File inputWorkbook = new File(inputFile);
    Workbook w;
    try {
      w = Workbook.getWorkbook(inputWorkbook);
      // Get the first sheet
      Sheet sheet = w.getSheet(0);
      // Loop over first 10 column and lines

      for (int j = 0; j < sheet.getColumns(); j++) {
        for (int i = 0; i < sheet.getRows(); i++) {
          Cell cell = sheet.getCell(j, i);
          CellType type = cell.getType();
          
               
          if (type == CellType.DATE) {
              if( GetDate().equals(cell.getContents()))
                 
                      {
                 
                  System.out.println("Date Matched");
                 
                  System.out.println("Current Date "+GetDate());
                 
                  Cell a2 = sheet.getCell(j-1,i);
                //  name = a2.getContents();
                  items.add(a2.getContents());
                 
                 
                  Cell a1 = sheet.getCell(j+1,i);
                 // email = a1.getContents();
                  items.add(a1.getContents());
                    
                  System.out.println("Name  :" + a2);
                  System.out.println("Email  :"+ a1);
                     System.out.println("I got a Date " + cell.getContents());                
                 
            }else
            {
                System.out.println("Date not matched with current date");
            }
          }
         
           

        }
      }
    } catch (BiffException e) {
      e.printStackTrace();
    }
  }

 String GetDate(){
     int day, month;
     String CurrentDate;
     GregorianCalendar date = new GregorianCalendar();

     day = date.get(Calendar.DAY_OF_MONTH);
     month = date.get(Calendar.MONTH);
     CurrentDate = (month+1)+"/"+day;
 
     System.out.println("Current date is  " +CurrentDate);

    return CurrentDate;
     
  }
  public static void main(String[] args) throws IOException {
    ReadExcel test = new ReadExcel();
   // test.setInputFile("C:/Users/IBM_ADMIN/Documents/sample.xls");
    //test.GetDate();
    test.read();
    Iterator itr= test.items.iterator();
    while(itr.hasNext())
    {
        System.out.println("Name :" + itr.next() + "\n Email :" + itr.next());
 
    }
   
  }

}



//Sending Mail with Random Images 

package sample;

import java.io.IOException;
import java.util.*; 

import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

import sample.ReadExcel;
 
public class SendEmail extends ReadExcel  

 public static void main(String [] args) throws IOException {
   
     
   
      String from = "abcde@gmail.com";//change accordingly
   
      String host = "127.0.0.1";//or IP address 
     
     
 
     //Get the session object 
      Properties properties = System.getProperties(); 
      properties.setProperty("mail.smtp.host", host); 
      Session session = Session.getDefaultInstance(properties); 
   
      String charsetNum = "123456789";
      StringBuffer image=new StringBuffer("images");
      ReadExcel test = new ReadExcel();
      test.setInputFile("C:/Users/IBM_ADMIN/Documents/sample.xls");
      test.GetDate();
      test.read();
      for(Iterator itr = test.items.iterator(); itr.hasNext();)
      {
          String name = (String)itr.next();
          String mail = (String)itr.next();
          String cc= (String) itr.next();
          System.out.println((new StringBuilder("Name :")).append(name).append("\n Email :").append(mail).append("\n CC :").append(cc).toString());
          try
          {
              java.util.Random rand = new java.util.Random(System.currentTimeMillis());

              for (int n = 1; n <2; n++) {
                  int pos = rand.nextInt(charsetNum.length());
                  image.append(charsetNum.charAt(pos));
              }
             
              String randomImageName = image.toString();
              System.out.println("randomImageName- "+randomImageName);
             
             
           
              MimeMessage message = new MimeMessage(session);
              message.setFrom(new InternetAddress(from));
              message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(mail));
              message.addRecipient(javax.mail.Message.RecipientType.CC, new InternetAddress(cc));
              message.setSubject("Wishing you a Very Happy Birthday -"+name);
             
              MimeMultipart multipart = new MimeMultipart();
              BodyPart messageBodyPart = new MimeBodyPart();
                                        
              String htmlText = "<H2 style=\"font-family:Calibri;color:red;\">Wishing you a Very Happy Birthday.. !!**.."+name+"..**!!</H2>";
              messageBodyPart.setContent(htmlText, "text/html");               
             
              multipart.addBodyPart(messageBodyPart);
            
             
              messageBodyPart = new MimeBodyPart();
              String cntent1 = "<H3 style=\"font-family:Calibri;color:green;\"><br><br>Wishes you for Healthy Long Life...</H3>";
              messageBodyPart.setContent(cntent1,"text/html");
              multipart.addBodyPart(messageBodyPart);
             
              messageBodyPart = new MimeBodyPart();
              String cntent2 = "<H4 style=\"font-family:Calibri;color:red;\"><br><br>From ABC.</H4><br><img src=\"cid:image\"><br><br>" +
                      "PLEASE DO NOT REPLY TO ALL!!!!<br><br><b>AA PMO TEAM</b>";
              messageBodyPart.setContent(cntent2, "text/html");               
              multipart.addBodyPart(messageBodyPart);               
             
              messageBodyPart = new MimeBodyPart();
              javax.activation.DataSource fds1 = new FileDataSource("C:/Users/IBM_ADMIN/Pictures/"+randomImageName+".jpg");
              messageBodyPart.setDataHandler(new DataHandler(fds1));
              messageBodyPart.addHeader("Content-ID", "<image>");
              multipart.addBodyPart(messageBodyPart);
             
             
              message.setContent(multipart);
              Transport.send(message);
              System.out.println("message sent successfully....");
          }   

        catch (MessagingException mex) {mex.printStackTrace();} 
         
   
      }
     
 
     //compose the message 
     
   }

 












No comments:

Post a Comment