JavaMail Outlook邀请邮件问题

时间:2019-07-11 18:11:40

标签: java outlook office365 javamail

我试图在javamail,mimemessage的帮助下发送Outlook邀请,并且一切正常,直到2019年5月为止,但是现在不是带有接受拒绝选项的邀请,而是不支持邀请ics文件附件的邮件。请查看我的代码,让我知道如何解决此问题。

以下代码生成的ICS文件如下所示:

BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:Microsoft Exchange Server
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:20190716T130000Z
DTSTART:20190716T130000Z
DTEND:20190716T140000Z
SUMMARY:Candidate Interview Invite: Testing Prod
UID:4839441021
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="Nandi":MAILTO:some@value
ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN="Nandi":MAILTO:some@value
ACTION;RSVP=TRUE;CN="some@value":MAILTO:some@value
ORGANIZER;CN="RCN":mailto:some@value
LOCATION:test
DESCRIPTION:Candidate Interview Invite: Testing Prod
SEQUENCE:0
PRIORITY:5
CLASS:PRIVATE
STATUS:CONFIRMED
X-MICROSOFT-CDO-BUSYSTATUS:BUSY
X-MICROSOFT-CDO-INSTTYPE:0
X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY
X-MICROSOFT-CDO-ALLDAYEVENT:FALSE
X-MICROSOFT-CDO-IMPORTANCE:1
X-MICROSOFT-CDO-OWNERAPPTID:-1
TRANSP:OPAQUE
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:REMINDER
TRIGGER;RELATED=START:-PT00H15M00S
END:VALARM
END:VEVENT
END:VCALENDAR
public class EmailInvite{
final Logger log = Logger.getLogger(EmailInvite.class);

@SuppressWarnings("unchecked")
public long sendInterviewInvite(Map<String, Object> params,
        String location, String organizerEmail, String[] interviewers) throws Exception {
    log.info("Send Interview Invite method");
    log.info("location: "+location);
    log.info("organizerEmail: "+organizerEmail);

    Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", "some value");
    properties.setProperty("mail.smtp.port", " some value");
    Session session = Session.getDefaultInstance(properties);

    Calendar startCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    startCal.setTime((Date) params.get("interviewDate"));
    startCal.set(Calendar.HOUR_OF_DAY,
            ((Long) params.get("interviewHour")).intValue());
    startCal.set(Calendar.MINUTE,
            ((Long) params.get("interviewMinute")).intValue());
    startCal.set(Calendar.SECOND, 0);

    Date interviewDate = (Date) params.get("interviewDate");
    int hour = ((Long) params.get("interviewHour")).intValue();
    int minute = ((Long) params.get("interviewMinute")).intValue();

    String gmtOffset = (String) params.get("interviewTimeZone");
    int offsetInt = 0;
    int gmtOffMins = 0;
    if (gmtOffset.indexOf(":") > 0) {
        String offsetVal = gmtOffset.substring(3, gmtOffset.indexOf(":"));
        if (offsetVal.contains("+")) {
            if (gmtOffset.endsWith("30"))
                gmtOffMins = 30;
            offsetVal = offsetVal.substring(1);
        } else {
            if (gmtOffset.endsWith("30"))
                gmtOffMins = -30;
        }
        offsetInt = Integer.valueOf(offsetVal);
    }

    Calendar endCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    endCal.set(Calendar.HOUR_OF_DAY, startCal.get(Calendar.HOUR_OF_DAY));
    endCal.set(Calendar.MINUTE, startCal.get(Calendar.MINUTE));
    String intType = (String) params.get("interviewType");
    if (intType.equals("Sreening") || intType.equals("Comprehensive"))
        endCal.add(Calendar.MINUTE, 60);
    else
        endCal.add(Calendar.MINUTE, 30);

    SimpleDateFormat format = new SimpleDateFormat("EEEE MMMM dd, yyyy");
    @SuppressWarnings("rawtypes")
    Map model = new HashMap();
    model.putAll(params);
    model.put("organizerEmail", organizerEmail);
    model.put("interviewCoordinatorName", "RCN");
    model.put("iasFormLink", iasFormLink.toString());
    model.put("interviewDateFmt",
            format.format((Date) params.get("interviewDate")));
    String text = "xyzxyz<br>thanks<br>Chinmoy
    text = text.replace("${iasFormLink}", iasFormLink.toString());
    String subject;
    if (intType.equals("some value "))
        subject = intType + " " + "invite";
    else
        subject = "Candidate Interview Invite: "
                + params.get("candidateName");
    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 mimeMessage = new MimeMessage(session);

    Multipart multipart = new MimeMultipart("mixed");
    int cnt = 0;


    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(text, "text/html; charset=UTF-8");
    multipart.addBodyPart(messageBodyPart, cnt++);
    String organizerName = "RCN";
    long guid = (long) Math.floor(Math.random() * 9000000000L) + 1000000000L;
    BodyPart calendarPart = buildCalendarPart(organizerName,organizerEmail, interviewers, subject, location, hour, minute,endCal.get(Calendar.HOUR_OF_DAY), endCal.get(Calendar.MINUTE),interviewDate, offsetInt, gmtOffMins, guid, false);

    multipart.addBodyPart(calendarPart, cnt);

    mimeMessage.setContent(multipart);

    mimeMessage.setFrom(new InternetAddress("some value "));

    mimeMessage.setSubject(subject);
    Address[] toAddr = new InternetAddress[interviewers.length];
    for (int i = 0; i < interviewers.length; i++) {
        Address a = new InternetAddress(interviewers[i]);
        toAddr[i] = a;
    }

    mimeMessage.addRecipients(RecipientType.TO, toAddr);
    Transport.send(mimeMessage);
    return guid;
}


private BodyPart buildCalendarPart(String organizerName,
        String organizerEmail, String[] attendees, String subject,
        String location, int hour, int minute, int endHour, int 
endMinute, Date interviewDate, int offsetInt, int gmtOffMins, long guid,
        boolean cancel) throws Exception {
    log.info("Build Calendar Part");
    SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat(
            "yyyyMMdd'T'HHmmss'Z'");
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");


    Calendar cal = Calendar.getInstance();
    cal.setTime(interviewDate);

    int startTzHr = hour - offsetInt;
    int startTznOffMin = minute - gmtOffMins;
    if (endHour == 0)
        endHour = 24;
    int endTzHr = endHour - offsetInt;
    int endTzOffMin = endMinute - gmtOffMins;

    if (startTzHr > 23) {
        startTzHr = startTzHr - 24;
        if (endHour != 0)
            endTzHr = endTzHr - 24;
        cal.add(Calendar.DAY_OF_YEAR, 1);
    }

    if (gmtOffMins < 0 && minute >= 30)
        startTzHr++;
    if (gmtOffMins < 0 && endMinute >= 30)
        endTzHr++;
    if (gmtOffMins > 0 && minute == 0)
        startTzHr--;
    if (gmtOffMins > 0 && endMinute == 0)
        endTzHr--;
    if (startTznOffMin == 60)
        startTznOffMin = 0;
    if (endTzOffMin == 60)
        endTzOffMin = 0;


    int stMin = Math.abs(startTznOffMin);
    int endMin = Math.abs(endTzOffMin);
    StringBuffer startFmtBuffer = new StringBuffer();
    startFmtBuffer.append(sdf.format(cal.getTime())).append("T")
            .append(startTzHr < 10 ? "0" + startTzHr : startTzHr)
            .append(stMin == 0 ? "00" : stMin).append("00").append("Z");
    StringBuffer endFmtBuffer = new StringBuffer();
    endFmtBuffer.append(sdf.format(cal.getTime())).append("T")
            .append(endTzHr < 10 ? "0" + endTzHr : endTzHr)
            .append(endMin == 0 ? "00" : endMin).append("00").append("Z");
    log.info(startFmtBuffer+" --- "+endFmtBuffer);
    BodyPart calendarPart = new MimeBodyPart();

    StringBuffer attendeesData = new StringBuffer();
    for (int i = 0; i < attendees.length; i++) {
        attendeesData
                .append("ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=\"")
                .append("Nandi")
                .append("\":MAILTO:").append(attendees[i]).append("\r\n");
    }



    String calendarContent = "BEGIN:VCALENDAR\n" + "METHOD:"
            + (cancel ? "CANCEL" : "REQUEST")
            + "\r\n"
            + "PRODID:Microsoft Exchange Server\r\n"
            + "VERSION:2.0\r\n"
            + "BEGIN:VEVENT\r\n"
            +
            "DTSTAMP:" + startFmtBuffer.toString() + "\n" + "DTSTART:"
            + startFmtBuffer.toString() + "\n" + "DTEND:"
            + endFmtBuffer.toString() + "\n" + "SUMMARY:" + subject + "\n"
            + "UID:" + guid + "\n" + attendeesData + "ACTION;RSVP=TRUE;"
            + "CN=\"" + organizerEmail + "\":MAILTO:" + organizerEmail
            + "\r\n" + "ORGANIZER;CN=\"" + organizerName + "\":mailto:"
            + organizerEmail + "\r\n" + "LOCATION:" + location + "\n"
            + "DESCRIPTION:" + subject + "\n" + "SEQUENCE:0\n"
            + "PRIORITY:5\n" + "CLASS:PRIVATE\n" + "STATUS:"
            + (cancel ? "CANCEL" : "CONFIRMED") + "\n"
            + "X-MICROSOFT-CDO-BUSYSTATUS:BUSY\r\n"
            + "X-MICROSOFT-CDO-INSTTYPE:0\r\n"
            + "X-MICROSOFT-CDO-INTENDEDSTATUS:BUSY\r\n"
            + "X-MICROSOFT-CDO-ALLDAYEVENT:FALSE\r\n"
            + "X-MICROSOFT-CDO-IMPORTANCE:1\r\n"
            + "X-MICROSOFT-CDO-OWNERAPPTID:-1\r\n" + "TRANSP:OPAQUE\n";
    if (!cancel) {
        calendarContent += "BEGIN:VALARM\n" + "ACTION:DISPLAY\n"
                + "DESCRIPTION:REMINDER\n"
                + "TRIGGER;RELATED=START:-PT00H15M00S\n" + "END:VALARM\n";
    }
    calendarContent += "END:VEVENT\n" + "END:VCALENDAR";
    log.info(calendarContent);
    calendarPart.addHeader("Content-Class",
            "urn:content-classes:calendarmessage");
    calendarPart.setContent(calendarContent, "text/calendar;method=CANCEL");

    return calendarPart;
}

我想在Outlook中收到带有接受拒绝选项的邀请邮件,而不是附件。并且仅使用javamail。

0 个答案:

没有答案