Javamail双周gmail日历邀请作为ICS文件发布

时间:2018-06-01 19:47:16

标签: java gmail javamail icalendar biweekly

我正在尝试将日历邀请发送到Gmail地址。我正在使用Spring,biweekly和javamail库。邀请已发送,但在gmail中它不作为日历进入,但我收到一封主题为“您被邀请参加会议”的电子邮件。它有一个附件ICS,其中包含我编码的邀请的详细信息。

请帮助了解我如何在谷歌中获得实际的邀请,而不是附带ICS文件的电子邮件。如果我发送到Outlook,邀请工作正常。

@Service
public class CalendarEvent {

private JavaMailSender javaMailSender;

@Autowired
public CalendarEvent(JavaMailSender javaMailSender) {
    this.javaMailSender = javaMailSender;
}

public void createCal() throws MessagingException, ParseException, IOException {

    ICalendar ical = new ICalendar();
    EmailInvite invite =new EmailInvite();
    VEvent event = new VEvent();

    Attendee attendee = new Attendee("xx", "xx.xx@gmail.com");
    attendee.setRsvp(true);
    attendee.setRole(Role.ATTENDEE);
    attendee.setParticipationStatus(ParticipationStatus.NEEDS_ACTION);
    attendee.setParticipationLevel(ParticipationLevel.REQUIRED);

    event.addAttendee(attendee);

    event.setSummary(invite.getSubject());

    SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:MM:SS"); 

    Date starts = new Date();
    Date ends  = new Date();
    try {
        starts=ft.parse(invite.getDateStart());
        ends=ft.parse(invite.getDateEnd());

    } catch (java.text.ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    DateStart thisStart = new DateStart(starts, true);
    DateEnd dateEnd = new DateEnd(ends, true);

    event.setDateStart(thisStart);
    event.setDateEnd(dateEnd);

    Duration reminder = new Duration.Builder().minutes(15).build();
    Trigger trigger = new Trigger(reminder, Related.START);
    Action action = new Action("DISPLAY");
    VAlarm valarm = new VAlarm(action, trigger);
    event.addAlarm(valarm);

    Duration duration = new Duration.Builder().hours(1).build();
    event.setDuration(duration);

    event.setUid(invite.getUID());
    event.setOrganizer(invite.getUserID());
    event.setLocation("Small");

    ical.addEvent(event);
    ical.setMethod("REQUEST");

    String str = Biweekly.write(ical).go();

    MimeMessage message = javaMailSender.createMimeMessage();
    message.addHeaderLine("charset=UTF-8");
    message.addHeaderLine("component=VEVENT");
    message.addHeaderLine("method=REQUEST");

    message.setFrom("abc@lxyz.com");
    message.addRecipient(Message.RecipientType.TO, new InternetAddress("xx.xx@gmail.com"));
    message.setSubject("You're Invited to a Meeting");

    // Create the message part
    BodyPart messageBodyPart = new MimeBodyPart();

    // Fill the message
    messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
    messageBodyPart.setHeader("Content-ID", "calendar_message");
    messageBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(str, "text/calendar")));// very important


    Multipart multipart = new MimeMultipart();


    multipart.addBodyPart(messageBodyPart);


    message.setContent(multipart);

    javaMailSender.send(message);

}
}

以下是生成的ICS文件,并作为附件发送到gmail。

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.6.2//EN
METHOD:REQUEST
BEGIN:VEVENT
DTSTAMP:20180601T193055Z
ATTENDEE;RSVP=TRUE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;CN=Gaurav:mailto:xx.xx@gmail.com
SUMMARY:some summary
DTSTART:20200602T130000Z
DTEND:20200602T140000Z
DURATION:PT1H
UID:2fb68918-c50e-4da2-ae97-ddcdc2d79b06
ORGANIZER:mailto:abc@lxyz.com
LOCATION:Small
BEGIN:VALARM
ACTION:DISPLAY
TRIGGER;RELATED=START:PT15M
END:VALARM
END:VEVENT
END:VCALENDAR

1 个答案:

答案 0 :(得分:0)

ics文件看起来不错,但如果您希望大多数客户都能识别您的邀请,您还需要遵循特定的MIME结构。例如见:

Multipart email with text and calendar: Outlook doesn't recognize ics

(上述问题与Outlook有关,但指南并非具体)