Help creating a Meeting Request for Outlook using JAVA

A

al

I have been trying to figure out a way to create a meeting request
programmatically using JAVA Mail. So far I was able to create an email
and a meeting that goes out as an attachment, which users can open and
accept/decline and so forth.

I have not been able to send this as an actual meeting request. I want
it to work as if a meeting reminder that comes from Google Calendar
(if set up to send reminders to other emails that use Outlook) and
shows up on Outlook Calendar.

I have tried several things but no success. I have not modified the
code after doing several attempts to fix it, so it might have a lot of
redundant information.

Any Help would be appreciated...

Thanks!!


public class CalendarRequest {
public CalendarRequest() {
}

public static void main(String[] args) {
try {
CalendarRequest email = new CalendarRequest();
email.send();}
catch (Exception e) { e.printStackTrace(); }
}

public void send() throws Exception {
String host = "smtp.host.com";
String from = "(e-mail address removed)";
String to = "(e-mail address removed)";
Properties prop = new Properties();
// prop.put("mail.smtp.host", "mailhost"); -- format
prop.put("mail.smtp.host", host);


try {
Session session = Session.getDefaultInstance(prop, null);
// Define message
MimeMessage message = new MimeMessage(session);

message.addHeader("Mime-Version", "1.0");
message.addHeader("PRODID","-//Microsoft Corporation//Outlook 10.0
MIMEDIR//EN");
message.addHeader("version","2.0");
message.addHeader("method","REQUEST"); // Can be CANCEL to cancel
meeting
message.addHeader("charset","UTF-8");
message.addHeader("component","VEVENT");
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to));
message.setSubject("Outlook Meeting Request Using JavaMail");

StringBuffer sb ;
StringBuffer msg = CalendarRequest.EmailMessage();

// Microsoft
StringBuffer buffer = CalendarRequest.createMeetingText();

StringDataSource sdSource = new StringDataSource(buffer.toString(),
"text/calendar", "meetingRequest");

// Fill the message
MimeBodyPart meetingPart = new MimeBodyPart(); // for meeting
request
MimeBodyPart messageBody = new MimeBodyPart(); // for email message
MimeBodyPart attachmentBody = new MimeBodyPart(); // for
attachments

meetingPart.setDataHandler(new DataHandler(sdSource));
meetingPart.setContent("",sdSource.getContentType());
messageBody.setHeader("Content-Class","urn:content-
classes:calendarmessage");
messageBody.setHeader("Content-ID", "calendar_message");
messageBody.setContent(msg.toString(), "text/plain");


// Create a Multipart
Multipart multipart = new MimeMultipart();

// Add part one - meeting request - not working right now
multipart.addBodyPart(meetingPart); // adds the actual meeting
// add part two - actual email message
multipart.addBodyPart(messageBody); // add the email description

// Part three is attachment


String filename = "DCM Request.ics"; // need to change this later
attachmentBody.setFileName(filename);
attachmentBody.setContent(buffer.toString(), "text/plain");

// Add part two
multipart.addBodyPart(attachmentBody);

// Put parts in message
message.setContent(multipart);

// send message
Transport.send(message);

} catch (MessagingException me) {
me.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static StringBuffer EmailMessage()
{
StringBuffer bf = new StringBuffer();
bf.append("Here is the line1 of email\n"
+ "Here is line2 of message\n"
+ "Create an HTML file instead"
);
return bf;
}

public static StringBuffer createMeetingText()
{
StringBuffer sb = new StringBuffer();
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:[email protected]
\n"
+ "ORGANIZER:MAILTO:[email protected]\n"
+ "DTSTART:20070316T053000Z\n"
+ "DTEND:20070316T060000Z\n"
+ "LOCATION:Conference room\n"
+ "TRANSP:OPAQUE\n"
+ "SEQUENCE:0\n"
+ "UID:
040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\n"
+ " 000004377FE5C37984842BF9440448399EB02\n"
+ "DTSTAMP:20051206T120102Z\n"
+ "CATEGORIES:Meeting\n"
+ "DESCRIPTION:This the description of the meeting.\n\n"
+ "SUMMARY:Test meeting request\n" + "PRIORITY:5\n"
+ "CLASS:pUBLIC\n" + "BEGIN:VALARM\n"
+ "TRIGGER:pT1440M\n" + "ACTION:DISPLAY\n"
+ "DESCRIPTION:Reminder\n" + "END:VALARM\n"
+ "END:VEVENT\n" + "END:VCALENDAR");

return sb;
}

private static class StringDataSource implements DataSource {
private String contents;
private String mimetype;
private String name;

public StringDataSource(String contents, String mimetype, String
name) {
this.contents = contents;
this.mimetype = mimetype;
this.name = name;
}

public String getContentType() {
return (mimetype);
}

public String getName() {
return (name);
}

public InputStream getInputStream() {
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(
"c:\\temp\\tempfile.txt"));

} catch (Exception ex) {
System.out.println("Exception occured: " + ex.getMessage());
}
return is;

}

public OutputStream getOutputStream() {

ByteArrayOutputStream out = new ByteArrayOutputStream();
return out; // doing nothing - including error handling later...
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top