2014-06-23 42 views
1
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"); 
    } 

} 

創建日曆事件的示例代碼。我想爲已安排的日曆發送重新計劃日曆事件。我嘗試的另一個選項是發送取消郵件,然後再發送新的日曆事件。但爲此,我需要發送兩封不同的郵件。有什麼辦法可以在一封郵件中完成嗎?Java代碼重新安排Outlook日曆事件?

回答

1

的關鍵是:

  • 保持相同的UID在初始請求
  • 顛簸起來

我也注意到在你的代碼序列(和DTSTAMP),雖然您在iCalendar流中正確設置METHOD:REQUEST,您的內容類型仍指示method = CANCEL。

+0

非常感謝arnaudq。這些更改按照我的要求工作。 – Gikar