使用Java通过电子邮件发送日历邀请 [英] Send calendar invite per email with java

查看:702
本文介绍了使用Java通过电子邮件发送日历邀请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java通过电子邮件发送日历邀请.收件人会收到电子邮件,但不会向其显示邀请接受或拒绝的邀请,而是会将事件自动添加到他的日历中.

I'm trying to send calendar invites per email with java. The recipient gets the email but instead of being shown an invitation to accept or decline, the event is automatically added to his calendar.

我正在使用ical4j.jar构建事件/邀请

I'm building the event/invite with 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
    }

}

但是,当我收到电子邮件(以gmail格式)时,没有看到邀请,该活动会自动添加到我的日历中.我只能通过单击日历中的事件来接受或拒绝.

But when I get the email (in gmail), I see no invitation, the event is automatically added to my calendar. I can only accept or decline by clicking on the event in the calendar.

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

I have tried to just send the invite, then what happens is that I get an email with an ics attachment.

我想念什么?

推荐答案

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

You are creating a new calendar, that's why the calendar is added automatically. See the documentation https://github.com/ical4j/ical4j/wiki/Examples#Creating_a_new_calendar try the "Creating a meeting of four hour duration" and see if you still have a problem.

这篇关于使用Java通过电子邮件发送日历邀请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆