使用java发送每封电子邮件的日历邀请

时间:2017-04-07 12:54:44

标签: java email calendar javamail ical4j

我正在尝试使用java发送每封电子邮件的日历邀请。收件人收到电子邮件,但该活动不会显示接受或拒绝的邀请,而是自动添加到他的日历中。

我正在使用ical4j.jar构建活动/邀请

private Calendar getInvite(Session session) {
    Calendar calendar = new Calendar();
    calendar.getProperties().add(Version.VERSION_2_0);
    calendar.getProperties().add(Method.REQUEST);

    VEvent event = new VEvent(
        new DateTime(sesion.getStartDate()), 
        new DateTime(sesion.getEndDate()), 
        session.getName());

    event.getProperties().add(Priority.MEDIUM);
    event.getProperties().add(Clazz.PUBLIC);

    try {
        UidGenerator ug = new UidGenerator("uidGen");
        Uid uid = ug.generateUid();
        event.getProperties().add(uid);
    } catch (SocketException e) {
        // Log things
    }

    for (Participant participant : session.getParticipants()) {
        Attendee attendee = new Attendee(URI.create("mailto:" + participant.getEmail()));
        attendee.getParameters().add(Role.OPT_PARTICIPANT);
        attendee.getParameters().add(new Cn(participant.getName()));
        attendee.getParameters().add(PartStat.NEEDS_ACTION);
        event.getProperties().add(attendee);
    }

    calendar.getComponents().add(event);

    return calendar;

}

这就是我发送电子邮件的方式:

public void sendEmail(String fromMail, String toMail, String subject, String text, net.fortuna.ical4j.model.Calendar calendar) {
    try {
        Session session = Session.getInstance(getMailProperties(), new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(getUser(), getPassword());
            }
        });

        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setHeader("Content-Transfer-Encoding:", "7bit");

        Address address = new InternetAddress(fromMail);
        mimeMessage.setFrom(address);

        mimeMessage.setSentDate(Calendar.getInstance().getTime());
        mimeMessage.setRecipients(Message.RecipientType.TO, toMail);

        mimeMessage.setSubject(subject);
        Calendar cal = Calendar.getInstance();
        mimeMessage.setSentDate(cal.getTime());

        Multipart multipart = new MimeMultipart("alternative");

        // First part - HTML readable text  
        MimeBodyPart msgHtml = new MimeBodyPart();
        msgHtml.setContent(text, "text/html; charset=UTF-8");

        multipart.addBodyPart(msgHtml);

        if (calendar != null) {
            // Another part for the calendar invite
            MimeBodyPart invite = new MimeBodyPart();
            invite.setHeader("Content-Class", "urn:content-  classes:calendarmessage");
            invite.setHeader("Content-ID", "calendar_message");
            invite.setHeader("Content-Disposition", "inline");
            invite.setContent(calendar.toString(), "text/calendar");
            multipart.addBodyPart(invite);
        }

        mimeMessage.setContent(multipart);

        Transport.send(mimeMessage);

    } catch (Exception e) {
        // Log things
    }

}

但是当我收到电子邮件(在Gmail中)时,我看不到邀请,该事件会自动添加到我的日历中。我只能通过点击日历中的活动来接受或拒绝。

我试图发送邀请,然后发生的是我收到一封带有ics附件的电子邮件。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

您正在创建新日历,这就是自动添加日历的原因。请参阅文档https://github.com/ical4j/ical4j/wiki/Examples#Creating_a_new_calendar尝试“创建四小时的会议”并查看是否仍有问题。