在Java邮件API中将日历邀请作为邮件附件发送

时间:2017-05-06 08:33:42

标签: java email javamail attachment icalendar

我正在动态地在我的java代码中创建icalender邀请。现在,我想将此icalender邀请作为附件发送到邮件中,而不将其作为文件保存到我的服务器。

我怎样才能实现它?

我的代码是:

public static void sendNow(String subject, String[] toArray, String[] ccArray, BodyPart bodyPart, BodyPart headerPart, List <MimeMessage> attachments ) {
            MimeMessage message = new MimeMessage(getMimeSession());
            // Add From Email id
            // Add TO receipients
            // Add CC receipients
            // SET Subject

            // Create a Multipart
            Multipart multipart = new MimeMultipart();
            if (null != bodyPart) {
                multipart.addBodyPart(bodyPart);
            }
            if (null != headerPart) {
                multipart.addBodyPart(headerPart);
            }
             // Attaching MIMEMessage as attachment - IT IS NOT WORKING
                if(null != attachments) {

                //attach meeting invitation
                for (MimeMessage attach : attachments) {
                    message.setDataHandler(attach.getDataHandler());
                    message.setFileName(attach.getSubject() + ".ics");
                }
            }
            // Put parts in message
            message.setContent(multipart);
            // SEND

}

1 个答案:

答案 0 :(得分:0)

在内存中生成数据,并使用ByteArrayDataSource和DataHandler将其附加到邮件中。