Reputation: 219
package necc.util.mail;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;
import javax.activation.MailcapCommandMap;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class EmailMeeting {
private static BodyPart buildHtmlTextPart() throws MessagingException {
MimeBodyPart descriptionPart = new MimeBodyPart();
// Note: even if the content is spcified as being text/html, outlook
// won't read correctly tables at all
// and only some properties from div:s. Thus, try to avoid too fancy
// content
String content = "<font face=\\\"verdana\\\">NECC TMS Estimation</font>";
descriptionPart.setContent(content, "text/html; charset=utf-8");
return descriptionPart;
}
// define somewhere the icalendar date format
private static SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat(
"yyyyMMdd'T'HHmm'00'");
private static BodyPart buildCalendarPart() throws Exception {
BodyPart calendarPart = new MimeBodyPart();
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
cal.set(Calendar.MINUTE, 0);
Date start = cal.getTime();
cal.add(Calendar.HOUR_OF_DAY, 1);
Date end = cal.getTime();
// 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(start)
+ "\n"
+ "DTSTART:"
+ iCalendarDateFormat.format(start)
+ "\n"
+ "DTEND:"
+ iCalendarDateFormat.format(end)
+ "\n"
+ "SUMMARY:NECC TMS Estimation\n"
+ "UID:324\n"
+ "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:[email protected]\n"
+ "ORGANIZER:MAILTO:[email protected]\n"
+ "LOCATION:Conference Room\n"
+ "DESCRIPTION:How can we complete in estimated PD's?\n"
+ "SEQUENCE:0\n"
+ "PRIORITY:5\n"
+ "CLASS:PUBLIC\n"
+ "STATUS:CONFIRMED\n"
+ "TRANSP:OPAQUE\n"
+ "BEGIN:VALARM\n"
+ "ACTION:DISPLAY\n"
+ "DESCRIPTION:REMINDER\n"
+ "TRIGGER;RELATED=START:-PT00H15M00S\n"
+ "END:VALARM\n"
+ "END:VEVENT\n" + "END:VCALENDAR";
calendarPart.addHeader("Content-Class",
"urn:content-classes:calendarmessage");
calendarPart.setContent(calendarContent, "text/calendar;method=CANCEL");
return calendarPart;
}
/* @param args */
public static void main(String[] args) throws Exception {
String host = "abc.abc.com";// hostname of the mail server
String from = "[email protected]"; // from internet address
String to = "[email protected]"; // to internet address
Properties prop = new Properties();
prop.put("mail.host", host);
Session session = Session.getDefaultInstance(prop, null);
// register the text/calendar mime type
MimetypesFileTypeMap mimetypes = (MimetypesFileTypeMap) MimetypesFileTypeMap
.getDefaultFileTypeMap();
mimetypes.addMimeTypes("text/calendar ics ICS");
// register the handling of text/calendar mime type
MailcapCommandMap mailcap = (MailcapCommandMap) MailcapCommandMap
.getDefaultCommandMap();
mailcap.addMailcap("text/calendar;; x-java-content-handler=com.sun.mail.handlers.text_plain");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setSubject("NECC TMS");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Create an alternative Multipart
Multipart multipart = new MimeMultipart("alternative");
// part 1, html text
BodyPart messageBodyPart = buildHtmlTextPart();
multipart.addBodyPart(messageBodyPart);
// Add part two, the calendar
BodyPart calendarPart = buildCalendarPart();
multipart.addBodyPart(calendarPart, 0);
// Put the multipart in message
message.setContent(multipart);
// send the message
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("Email Sent");
}
}
Sample code to create a calendar event. I want to send a reschedule calendar event for already scheduled calendar. The other option which i tried is send a cancellation mail and then again send new calendar event. But for this i would be required to send two different mail. Is there any way i can do it in a single mail.
Upvotes: 2
Views: 5233
Reputation: 4645
The key is to:
I also noticed in your code that, although you are correctly setting METHOD:REQUEST in the iCalendar stream, your content-type still indicates method=CANCEL.
Upvotes: 2