用于重新安排Outlook日历事件的Java代码?

时间:2014-06-23 12:18:43

标签: java outlook icalendar

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:abc_m@company.com\n"
                + "ORGANIZER:MAILTO:xyz_m@company.com\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 = "xyz@company.com"; // from internet address
        String to = "abc@company.com"; // 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");
    }

}

用于创建日历事件的示例代码。我想为已安排的日历发送重新安排日历活动。我尝试的另一个选项是发送取消邮件,然后再次发送新的日历活动。但为此,我将被要求发送两个不同的邮件。有什么方法可以用一封邮件来做。

1 个答案:

答案 0 :(得分:1)

关键是:

  • 保留与初始请求中相同的UID
  • 提升SEQUENCE(和DTSTAMP)

我还在你的代码中注意到,虽然你在iCalendar流中正确设置METHOD:REQUEST,但你的内容类型仍然表示method = CANCEL。