Email calendar invitation using java

I am trying to send calendar invitations by email using java. The recipient receives an email, but instead of showing an invitation to accept or decline, the event is automatically added to his calendar.

I am creating an event / invitation using 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;

}

And this is how I send the email:

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
    }

}

But when I receive an email (in gmail), I don’t see the invitation, the event is automatically added to my calendar. I can accept or decline by clicking on an event in the calendar.

I tried just sending an invitation, then what happens is that I receive an email with the ics attachment.

What am I missing?

+4

Source: https://habr.com/ru/post/1674310/


All Articles