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.

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

}

}

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.

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

What am I missing?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值