Write code to schedule the meeting in outlook, outlook meeting schedule in java

outlook meeting schedule in java

package com.sree.test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.MailcapCommandMap;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;


public class JavaMeetingSch {
// attendee is who is going to attand the meeting, if group of people please use string array

public void sendMeeting(String attendee) {

try{
MimetypesFileTypeMap mimetypes = (MimetypesFileTypeMap)MimetypesFileTypeMap.getDefaultFileTypeMap();
   mimetypes.addMimeTypes("text/calendar ics ICS");
   MailcapCommandMap mailcap = (MailcapCommandMap) MailcapCommandMap.getDefaultCommandMap();
   mailcap.addMailcap("text/calendar;; x-java-content-handler=com.sun.mail.handlers.text_plain");
 
   Properties props = new Properties();
props.put("mail.smtp.host", "host name");
Session session = Session.getDefaultInstance(props, null);

 MimeMessage message = new MimeMessage(session);
 InternetAddress addr = new InternetAddress(attendee);
   message.setFrom(new InternetAddress("srini@org.com"));
   message.setSubject("Monthly meeting");
   message.addRecipient(Message.RecipientType.TO, addr);
       Multipart multipart = new MimeMultipart("alternative");
 
   BodyPart calendarPart = buildCalendarPart();
   multipart.addBodyPart(calendarPart);

   message.setContent(multipart);
   System.out.println("message:"+message);
   // send the message
   Transport.send(message);
   System.out.println("Meeting sent sucessfully");
}catch(Exception e){
System.out.println("Error while sending meeting req:"+e);
}
//    Transport transport = session.getTransport("smtp");
//    transport.connect();
//    transport.sendMessage(message, message.getAllRecipients());
//    transport.close();
}
private static SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");

    private BodyPart buildCalendarPart() throws Exception {

        BodyPart calendarPart = new MimeBodyPart();

        Calendar cal = Calendar.getInstance();
       
        cal.add(Calendar.DAY_OF_MONTH, 1);
        Date start = cal.getTime();
       String inString= start.toString();
       Date today=null;
     
     
       today = new Date();
         if(inString!=null&inString!=""){
      String day= inString.substring(0, 3) ;
      System.out.println("Day is "+day);
      start=new Date(today.getYear(), today.getMonth(), today.getDate()+1, today.getHours()-4, today.getMinutes()) ;
      if(day.equalsIgnoreCase("Fri")||day.equalsIgnoreCase("Sat")){
   
      start=new Date(today.getYear(), today.getMonth(), today.getDate()+3, today.getHours()-4, today.getMinutes()) ;
     
   
      }
       }
              System.out.println("start: "+start);
              Date end = new Date(today.getYear(), today.getMonth(), today.getDate()+1, today.getHours()-3, today.getMinutes());
        System.out.println("end time:"+end);

        //check the icalendar spec in order to build a more complicated meeting request
        String calendarContent =
                "BEGIN:VCALENDAR\n" +
                        "METHOD:REQUEST\n" +
                        "PRODID: BCP - Meeting\n" +
                        "VERSION:2.0\n" +
                        "BEGIN:VEVENT\n" +
                        "DTSTAMP:" + iCalendarDateFormat.format(end) + "\n" +
                        "DTSTART:" + iCalendarDateFormat.format(start)+ "\n" +
                        "DTEND:"  + iCalendarDateFormat.format(end)+ "\n" +
                        "SUMMARY:test request\n" +
                        "UID:324\n" +
                        "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:srini@org.com\n" +
                        "ORGANIZER:MAILTO:srini@org.com\n" +
                        "LOCATION:Desk\n" +
                        "DESCRIPTION:Please attend the meeting\n" +
                        "SEQUENCE:0\n" +
                        "PRIORITY:1\n" +
                        "CLASS:PUBLIC\n" +
                        "STATUS:CONFIRMED\n" +
                        "TRANSP:OPAQUE\n" +
                        "BEGIN:VALARM\n" +
                        "TRIGGER:PT1440M\n" +
                        "ACTION:DISPLAY\n" +
                        "DESCRIPTION:REMINDER\n" +
                        "END:VALARM\n" +
                        "END:VEVENT\n" +
                        "END:VCALENDAR";

       
        calendarPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
        calendarPart.setHeader("Content-ID","calendar_message");
        calendarPart.setDataHandler(new DataHandler( new ByteArrayDataSource(calendarContent.toString(), "text/calendar")));
        return calendarPart;
    }



}


Share This

0 comments: