Java Calendar Invite

download Java Calendar Invite

of 4

Transcript of Java Calendar Invite

  • 8/12/2019 Java Calendar Invite

    1/4

    Java Outlook Calendar Invite

    Java Mail is equipped with simpler methods to create and send emails. But unfortunately the API support

    given to send an Outlook Calendar Invite is very limited as it was not originally introduced for it. So you

    need to use the same method for creating and sending an Invite, but in a different way. The message

    body part to be formed in such a way that the outlook can recognize the message as an Invite. Also the

    date set with the Invite is considered as EST time zone by default and the same needs to be converted to

    other zones if required. Below points should be kept in mind while creating a component for sending a

    Calendar Invite.

    1) The below headers need to be set to the message body part-

    messageBodyPart.setHeader("Content-Class","urn:content-classes:calendarmessage");messageBodyPart.setHeader("Content-ID", "calendar_message");

    2) Set the mime type as text/calendar

    3) Begin the message body part with BEGIN:VCALENDAR

    4) The calendar Invite name can be set in the header as below

    message.setHeader ("X-Mailer", "Test-Mailer");

    The CalenderInvite class (attached below) can be reused to send an Invite by calling its send() method.

    The send(), method expects the following parameters to be passed with it.

    fromDate date and time when the meeting starts (java.util.Date)Date toDate -- date and time when the meeting ends (java.util.Date)ConfRoom -- name of conference room (java.lang.String)toList email ids of Invitees in comma separated form (java.lang.String)from email id of the organizer (java.lang.String)Subject subject for the Invite (java.lang.String)Desc-- message content in the meeting Invite (java.lang.String)hostAddress-- exchange server address (java.lang.String)

    The source code is attached here

    CalenderInvite.java

    import java.io.ByteArrayInputStream;importjava.io.IOException;importjava.io.InputStream;import java.io.OutputStream;importjava.io.UnsupportedEncodingException;import java.text.DateFormat;import java.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;import java.util.Properties;

  • 8/12/2019 Java Calendar Invite

    2/4

    importjavax.activation.DataHandler;import javax.activation.DataSource;importjavax.mail.Message;import javax.mail.Multipart;importjavax.mail.Session;importjavax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;

    publicclassCalendarInvite {

    publicbooleansend(Date fromDate, Date toDate, String confRoom,String toList, String from, String Subject, String desc,String hostAddress) throwsException {

    String uniqueId = System.nanoTime() + "";

    toList += (","+ from);String meetingStartTime = getDateinCalendarFormat(fromDate);String meetingEndTime = getDateinCalendarFormat(toDate);Properties prop = newProperties();StringBuffer sb = newStringBuffer();StringBuffer buffer = null;Session session = Session.getDefaultInstance(prop, null);Message message = newMimeMessage(session);

    try{prop.put("mail.smtp.host", hostAddress);

    // Define message

    message.setFrom(newInternetAddress(from));message.setRecipients(Message.RecipientType.TO,

    InternetAddress.parse(toList, false));message.setSubject(Subject);message.setHeader("X-Mailer", "TD-Mailer");// Create the message partMimeBodyPart messageBodyPart = newMimeBodyPart();buffer = sb

    .append("BEGIN:VCALENDAR\n"+ "PRODID:-//Microsoft Corporation//Outlook 9.0

    MIMEDIR//EN\n"+ "VERSION:2.0\n"+ "METHOD:REQUEST\n"+ "BEGIN:VEVENT\n"

    + "ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:"+ toList+ "\n"+ "ORGANIZER:MAILTO:"+ from+ "\n"+ "DTSTART:"+ meetingStartTime+ "\n"

  • 8/12/2019 Java Calendar Invite

    3/4

    + "DTEND:"+ meetingEndTime+ "\n"+ "LOCATION:"+ confRoom+ "\n"+ "TRANSP:OPAQUE\n"+ "SEQUENCE:0\n"+ "UID:"+ uniqueId+ "@iquest.com\n"+ "DTSTAMP:"+ meetingEndTime+ "\n"+ "CATEGORIES:Meeting\n"+ "DESCRIPTION:"+ desc+ ".\n\n"+ "SUMMARY:"+ ""

    + "\n"+ "PRIORITY:1\n"+ "CLASS:PUBLIC\n"+ "BEGIN:VALARM\n"+ "TRIGGER:PT1440M\n"+ "ACTION:DISPLAY\n"+ "DESCRIPTION:Reminder\n"+ "END:VALARM\n"+ "END:VEVENT\n"+

    "END:VCALENDAR");// messageBodyPart.setFileName("TestMeeting.ics");messageBodyPart

    .setDataHandler(newDataHandler(newByteArrayDataSource(buffer.toString(), "text/calendar")));

    messageBodyPart.setHeader("Content-Class","urn:content-classes:calendarmessage");

    messageBodyPart.setHeader("Content-ID", "calendar_message");Multipart multipart = newMimeMultipart();multipart.addBodyPart(messageBodyPart);message.setContent(multipart);Transport.send(message);

    }// Catching only calendar invite exceptions. Other functionalities be// still workingcatch(Exception me) {

    // log exception here

    }returnfalse;}

    publicString getDateinCalendarFormat(Date date) {DateFormat requiredFormat = newSimpleDateFormat("yyyyMMdd'T'HHmmss");Calendar cal = Calendar.getInstance();cal.setTime(date);cal.add(Calendar.HOUR, -5);cal.add(Calendar.MINUTE, -30);

  • 8/12/2019 Java Calendar Invite

    4/4

    date = cal.getTime();returnrequiredFormat.format(date) + "Z";

    }

    privateclassByteArrayDataSource implementsDataSource {privatebyte[] data; // data for mail message

    privateString type; // content type/mime type

    ByteArrayDataSource(String data, String type)throwsUnsupportedEncodingException {

    // Assumption that the string contains only ascii// characters ! Else just pass in a charset into this // constructor and use it in getBytes()this.data= data.getBytes("iso-8859-1");this.type= type;

    }

    // DataSource interface methodspublicInputStream getInputStream() throwsIOException {

    if(data== null)thrownewIOException(

    "no data exception in ByteArrayDataSource");returnnewByteArrayInputStream(data);

    }

    publicOutputStream getOutputStream() throwsIOException {thrownewIOException("illegal operation in ByteArrayDataSource");

    }

    publicString getContentType() {returntype;

    }

    publicString getName() {return"dummy";

    }}

    }