如何在ICS文件中添加附件

时间:2016-07-22 07:18:47

标签: calendar google-calendar-api icalendar

我尝试在ics文件中添加附件,但在打开它时它没有显示在outlook中。我正在尝试添加附件,例如当我们发送会议请求并在Outlook中添加附件时,附件也可以从日历查看。这是我的ics文件:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//appform.com//NONSGML kigkonsult.se iCalcreator 2.18//
METHOD:PUBLISH
X-WR-TIMEZONE:UTC
X-MS-OLK-FORCEINSPECTOROPEN:TRUE
BEGIN:VEVENT
UID:20160719T144014-694839744@appform.com
DTSTAMP:20160719T124014Z
ATTACH;FMTTYPE="application/msword; charset=binary";FILENAME=1468827330fCrj
 O.doc:/var/www/html/basearch.appform.com/application/../public/uploads/146
 8827330fCrjO.doc
ATTENDEE;RSVP=TRUE;SENT-BY="MAILTO:sajal@mailinator.com";CN=satyendra@hirest
orm.com;DIR="/var/www/html/app/application/../public/uplo
ads/1468827330fCrjO.doc";LANGUAGE=us-EN;X-AGENDA=Interview;X-LENGTH=30 min
 :MAILTO:satyendra@mailinator.com
DESCRIPTION:Name: Dean Nestle Jones G\nVacancy: test\nEmployer: Zend\nDate:
Wednesday\, 20thJuly 2016\nTime: 1430 to 1500\n\nSubmit Feedback : http:/
/hirestorm.com/tms/a/Mg==/appid/NDU4/vacid/MTY4/candid/MTY=\n\nCandida
te CV : https://f12b1a775b358d1fc463-637e94f874af614e321f6.ssl.
cf2.rackcdn.com/1468827330fCrjO.doc\nOther Documents   : https://f12b1a775
b358d1fc463-637e94f874af614cdn.com/146297361
8PwEwE.jpeg\n
DTSTART:20160720T090000Z
DTEND:20160720T093000Z
LOCATION:1 2 Elmshorn Schleswig-Holstein Germany
SEQUENCE:0
SUMMARY:New Interview Confirmed: Dean Nestle Jones G for test
BEGIN:VALARM
ACTION:PROCEDURE
DESCRIPTION:Name: Dean Nestle Jones G\nVacancy: test\nEmployer: Zend\nDate:
 Wednesday\, 20thJuly 2016\nTime: 1430 to 1500\n\nSubmit Feedback : http:/
 /hirestorm.com/tms/a/Mg==/appid/NDU4/vacid/MTY4/candid/MTY=\n\nCandida
 te CV : https://f12b1a775b358d1fc463-637e94f874af614ce048a5e321d7d0f6.ssl.
 cf2.rackcdn.com/1468827330fCrjO.doc\nOther Documents   : https://f12b1a775
 b358d1fc463-637e94f874af614ce048a5e32cdn.com/146297361
 8PwEwE.jpeg\n
TRIGGER:-PT0H15M0S
END:VALARM
END:VEVENT
END:VCALENDAR

2 个答案:

答案 0 :(得分:0)

您的iCalendar文件包含对以下内容的引用:

/var/www/html/basearch.appform.com/application/../public/uploads/1468827330fCrjO.doc

这显然是你机器上的东西。您没有嵌入任何文件,只是粘贴了本地路径。除非您在Linux上运行Outlook,否则它无法找到该路径。

答案 1 :(得分:0)

我试图在一个简单的Java中完成

import java.io.IOException;
import java.net.URI;
import java.text.ParseException;
import java.util.HashMap;

import javax.activation.DataHandler;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import com.tba.readProps.Constants;
import com.tba.readProps.ReadProps;

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Period;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.parameter.Cn;
import net.fortuna.ical4j.model.parameter.Role;
import net.fortuna.ical4j.model.property.Attendee;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Uid;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.RandomUidGenerator;
import net.fortuna.ical4j.util.UidGenerator;


public class EmailWithCalendar{

    /**
     * @param emailAddr
     * @param subject
     * @param message
     */
    public static void sendHtmlEmail(String emailAddr, String subject, String message){

        HashMap propertyValues = null;
        String smtpHost = "";
        try {
            propertyValues = ReadProps.initializeProperties();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        smtpHost = (String) propertyValues.get(Constants.SMTP_HOST);

        //ManagerPropertiesBean mgrProps = new ManagerPropertiesBean();
        //String smtpHost = mgrProps.getMgrProperty("smtpHost");
        try {
            // start a session with given properties
            java.util.Properties props = new java.util.Properties(); 
            props.put("mail.smtp.host", smtpHost);
            Session mailSession = Session.getDefaultInstance(props, null);
            String fromAddress = null;
            fromAddress = (String) propertyValues.get(Constants.FROM_ADDRESS);
                if(!"undefined".equals(fromAddress)){
                InternetAddress fromAddr = new InternetAddress("test@test.com");
                InternetAddress toAddr = new InternetAddress(emailAddr);

                MimeMessage myMessage = new MimeMessage(mailSession);
                String replyToAddress = null;
                replyToAddress = (String) propertyValues.get(Constants.REPLY_ADDRESS); 
                InternetAddress replyToAddr[] =  new InternetAddress[1];
                if(!"undefined".equals(replyToAddress))
                    replyToAddr[0] = new InternetAddress(replyToAddress);

                 boolean isfromValid = true;
                 boolean isToValid = true;
                 boolean emailDomainCheck = true;


                 MimeBodyPart messageBodyPart =new MimeBodyPart();
                 messageBodyPart.setContent(message ,"text/html" );
                 Multipart multipart = new MimeMultipart();
                 multipart.addBodyPart(messageBodyPart);
                 myMessage.setContent(multipart);                

                 if(emailDomainCheck==true)
                 {           

                        myMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddr);

                            myMessage.setReplyTo(replyToAddr);                  

                 }
                 else
                 {
                    myMessage.setFrom(fromAddr);
                    myMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddr);
                    if(!"undefined".equals(replyToAddress))
                        myMessage.setReplyTo(replyToAddr);
                 }
                myMessage.setSentDate(new java.util.Date());
                myMessage.setSubject(subject);
                messageBodyPart.setDataHandler(new DataHandler(
                        new ByteArrayDataSource(createEvent().toString(), "text/calendar")));// very important

                //myMessage.setText(message);
                // now send the message!
                    if(emailDomainCheck==true)
                    {
                        if(isfromValid==true && isToValid==true)
                        {
                            Transport.send(myMessage);
                        }                       
                    }
                    else
                    {
                        Transport.send(myMessage);
                    }

                }
        } catch (Exception e) {     
            e.printStackTrace();
            System.out.println("Error occurred in ManagerEmailBean.sendHtmlEmail()");   
        }

    }
    private static Calendar createEvent() throws ParseException {


        // Create the event
        String eventName = "Progress Meeting";
        //DateTime start = new DateTime(startDate.getTime());
        //DateTime end = new DateTime(endDate.getTime());
        // Create the date range which is desired.
        DateTime start = new DateTime("20100101T070000Z");
        DateTime end = new DateTime("20100201T070000Z");;
        Period period = new Period(start, end);
        VEvent meeting = new VEvent(start, end, eventName);

        // add timezone info..
        //meeting.getProperties().add(tz.getTimeZoneId());

        // generate unique identifier..
        UidGenerator ug = new RandomUidGenerator();
        Uid uid = ug.generateUid();
        meeting.getProperties().add(uid);

        // add attendees..
        Attendee dev1 = new Attendee(URI.create("mailto:dev1@test.com"));
        dev1.getParameters().add(Role.REQ_PARTICIPANT);
        dev1.getParameters().add(new Cn("Developer 1"));
        meeting.getProperties().add(dev1);

        Attendee dev2 = new Attendee(URI.create("mailto:dev2@test.com"));
        dev2.getParameters().add(Role.OPT_PARTICIPANT);
        dev2.getParameters().add(new Cn("Developer 2"));
        meeting.getProperties().add(dev2);

        // Create a calendar
        net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
        icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
        icsCalendar.getProperties().add(Version.VERSION_2_0);
        icsCalendar.getProperties().add(CalScale.GREGORIAN);


        // Add the event and print
        icsCalendar.getComponents().add(meeting);
        System.out.println(icsCalendar);
        return icsCalendar;
    }
    public static void main(String[] args) {
        sendHtmlEmail("dev.test@gmail.com", "Test", "sjgsfgsf<p>dshdfsdf</p>");
    }

}