我想使用 java 在 Outlook 日历邀请中附加多个文件

发布于 2024-11-08 16:20:27 字数 3323 浏览 1 评论 0原文

我想使用 java 在日历邀请中附加多个文件。 目前,我可以使用 html 正文创建邀请,但无法向该邀请添加附件。

有谁知道如何附加文件。

我不会将邀请作为附件发送。它按照正常的接受/拒绝方式进行。

请尽快发布。 提前致谢

代码如下:

    MimetypesFileTypeMap mimetypes = (MimetypesFileTypeMap) MimetypesFileTypeMap.getDefaultFileTypeMap();
    mimetypes.addMimeTypes("text/calendar ics ICS");

    MailcapCommandMap mailcap = (MailcapCommandMap) MailcapCommandMap.getDefaultCommandMap();
    mailcap.addMailcap("text/calendar;; x-java-content-handler=com.sun.mail.handlers.text_plain");


    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "  ");
    props.setProperty("mail.host", mailServer);
    //props.setProperty("mail.user", "emailuser");
    //props.setProperty("mail.password", "");

    Session mailSession = Session.getDefaultInstance(props, null);


    MimeMessage message = new MimeMessage(mailSession);
    //message.addHeaderLine("text/calendar;method=REQUEST;charset=UTF-8");

  /*  String emailAddress = invite_email;
    String fullName = invite_name;*/

    String emailAddress = "[email protected]";
    String fullName = "ABCD";
    message.setFrom(new InternetAddress(replyEmail, replyEmailName));
    javax.mail.Address address = new InternetAddress(emailAddress, fullName);

    message.addRecipient(MimeMessage.RecipientType.TO, address);
    message.setSubject("abc" + invite_sub);

    // Create a Multipart
    Multipart multipart = new MimeMultipart("alternative");

    //part 1, html text
    BodyPart messageBodyPart = buildHtmlTextPart(team_id);
    multipart.addBodyPart(messageBodyPart);


    // Add part two, the calendar
    BodyPart calendarPart = buildCalendarPartNew(emailAddress , fullName , invite_sub , invite_uuid ,start_date , finish_date , invite_seq , invite_status , invite_timezone );
    multipart.addBodyPart(calendarPart);

    // Add attachments to the body


    multipart =  addAttachment(multipart,Req_List);


    //update the requisition id list back to " " once the attachment process is over
    Req_List = " ";

    // Put parts in message
    System.out.println("setting the content of message");
    message.setContent(multipart);



    // send message
    try {
        Transport transport = mailSession.getTransport();
        transport.connect();
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
    catch (Exception ex) {
        System.out.println(ex.toString());
        throw ex;
    }

附件的功能主要包含:

FileDataSource fds1 = new FileDataSource(sharepath_name);
        attachment.setDataHandler(new DataHandler(fds1));
attachment.setFileName(fds1.getName());
        attachment.setHeader("MIME-Version", "1.0");
        attachment.setHeader("Content-Type", " "+mime_type+ "; name=\"" + sharepath_name + "\"");
        attachment.setHeader("Content-Disposition", "attachment;          filename=\"" + sharepath_name + "\"");
        attachment.setHeader("Content-Transfer-Encoding", "base64");
        multipart.addBodyPart(attachment);
return multipart;

没有错误,邀请是用文本生成的,但主要问题是我想要邀请中的附件,我无法在其中附加文件邀请,我不知道如何在邀请中附加文件? 另外,我需要在邀请中提供多个附件。

提前致谢

I want to attach multiple files inside a calendar invite using java.
Currently i am able to create an invite with the html body text but i am not able to add attachments to that invite.

Does anyone knows how to attach files.

I am not sending the invite as attachment. It is going as normal accept/decline way.

Please post ASAP .
Thanks in advance

CODE AS FOLLOWS :

    MimetypesFileTypeMap mimetypes = (MimetypesFileTypeMap) MimetypesFileTypeMap.getDefaultFileTypeMap();
    mimetypes.addMimeTypes("text/calendar ics ICS");

    MailcapCommandMap mailcap = (MailcapCommandMap) MailcapCommandMap.getDefaultCommandMap();
    mailcap.addMailcap("text/calendar;; x-java-content-handler=com.sun.mail.handlers.text_plain");


    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "  ");
    props.setProperty("mail.host", mailServer);
    //props.setProperty("mail.user", "emailuser");
    //props.setProperty("mail.password", "");

    Session mailSession = Session.getDefaultInstance(props, null);


    MimeMessage message = new MimeMessage(mailSession);
    //message.addHeaderLine("text/calendar;method=REQUEST;charset=UTF-8");

  /*  String emailAddress = invite_email;
    String fullName = invite_name;*/

    String emailAddress = "[email protected]";
    String fullName = "ABCD";
    message.setFrom(new InternetAddress(replyEmail, replyEmailName));
    javax.mail.Address address = new InternetAddress(emailAddress, fullName);

    message.addRecipient(MimeMessage.RecipientType.TO, address);
    message.setSubject("abc" + invite_sub);

    // Create a Multipart
    Multipart multipart = new MimeMultipart("alternative");

    //part 1, html text
    BodyPart messageBodyPart = buildHtmlTextPart(team_id);
    multipart.addBodyPart(messageBodyPart);


    // Add part two, the calendar
    BodyPart calendarPart = buildCalendarPartNew(emailAddress , fullName , invite_sub , invite_uuid ,start_date , finish_date , invite_seq , invite_status , invite_timezone );
    multipart.addBodyPart(calendarPart);

    // Add attachments to the body


    multipart =  addAttachment(multipart,Req_List);


    //update the requisition id list back to " " once the attachment process is over
    Req_List = " ";

    // Put parts in message
    System.out.println("setting the content of message");
    message.setContent(multipart);



    // send message
    try {
        Transport transport = mailSession.getTransport();
        transport.connect();
        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
    catch (Exception ex) {
        System.out.println(ex.toString());
        throw ex;
    }

THE FUNCTION FOR ATTACHMENT MAINLY CONTAINS :

FileDataSource fds1 = new FileDataSource(sharepath_name);
        attachment.setDataHandler(new DataHandler(fds1));
attachment.setFileName(fds1.getName());
        attachment.setHeader("MIME-Version", "1.0");
        attachment.setHeader("Content-Type", " "+mime_type+ "; name=\"" + sharepath_name + "\"");
        attachment.setHeader("Content-Disposition", "attachment;          filename=\"" + sharepath_name + "\"");
        attachment.setHeader("Content-Transfer-Encoding", "base64");
        multipart.addBodyPart(attachment);
return multipart;

there is no error as such , the invite is getting generated with the text , but the main problem is i want attachments inside the invite, i am not able to attach files inside the invite, i don't know how to attach files inside invite ?
Also the attachments i need to provide multiple attachments inside the invite.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事↓在人 2024-11-15 16:20:27

您是否尝试过不手动设置附件标题?它们应该由 MimeMessage 设置。

Have you tried without setting the attachment headers manually? They should be set by MimeMessage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文