1

我想使用 C# 应用程序发送会议请求,所以我使用了 dday.ical 库。通过使用 ical 库,我创建了 .ics 文件,但现在我想更新我在互联网上搜索的已经创建的会议,我发现我们可以使用 uid 更新日历,但是当我使用相同的 uid 创建新的 cal 并运行时,我现有的 .ics 文件会替换新文件没有与现有文件合并

下面是我的代码

private iCalendar CreateOneTimeCalendarEvent(string title,Dictionary<string,string> attendees, string body, DateTime startDate, double duration, string location, string organizer,
                                     string eventId, bool allDayEvent)
{
   var iCal = new iCalendar
    {
        Method = "PUBLISH", //PUBLISH
        Version = "2.0"
    };

    //Creating event evt    
    var evt = iCal.Create<Event>();
    //Title to event
    evt.Summary = title;

    //Start time to event
    evt.Start = new iCalDateTime(startDate.Year,
                                 startDate.Month, startDate.Day, startDate.Hour,
                                 startDate.Minute, startDate.Second);


    // Description to event
    evt.Description = body;

    // Location to event
    evt.Location = location;

    //Check for All day event
    evt.IsAllDay = allDayEvent;

    //End time for event
    if (!allDayEvent)
    {
        // Duration to event
        evt.Duration = TimeSpan.FromHours(duration);

    }

    //Event unique identifier id
    evt.UID = new Guid().ToString();

    //Orgnizer to event
    if (!String.IsNullOrEmpty(organizer))
    {
        evt.Organizer = new Organizer(organizer);
    }
    else
    {
        throw new Exception("Organizer provided was null");
    }
    var attendes = new List<IAttendee>();

    foreach ( var atten in attendees)
    {

        IAttendee attendee1 = new DDay.iCal.Attendee("MAILTO:"+atten.Key)
        {
            Role = atten.Value
        };
        attendes.Add(attendee1);

    }

    //Adding attendees into event

    evt.Attendees = attendes;


    if (!String.IsNullOrEmpty(eventId))
    {
        evt.UID = eventId;
    }

    //Creating Alarm for event
    var alarm = new Alarm
    {
        Duration = new TimeSpan(0, 15, 0),
        Trigger = new Trigger(new TimeSpan(0, 15, 0)),
        Action = AlarmAction.Display,
        Description = "Reminder"
    };


    evt.Alarms.Add(alarm);

    // Save into calendar file.
    var serializer = new iCalendarSerializer(iCal);

    //   serializer.SerializeToString(iCal);

    serializer.Serialize(iCal, "OneTimeCalendarEvent.ics");

    return iCal;
}

我的第二个问题是当我使用带有 ics 文件附件的 smtp 时,它只是将文件附加到邮件中,不能像日历请求事件那样工作下面是我的代码:

static void Main(string[] args)
{
    Dictionary<string,string> attendees =new Dictionary<string,string>();
    attendees.Add("abc11@gmail.com", "REQ-PARTICIPANT");
    attendees.Add("abc21@gmail.com", "OPT-PARTICIPANT");

    attendees.Add("abc32@gmail.com", "OPT-PARTICIPANT");
    attendees.Add("abc43@gmail.com", "REQ-PARTICIPANT");
    attendees.Add("abc54@gmail.com", "OPT-PARTICIPANT");

    Program obj = new Program();
    iCalendar iCal= obj.CreateOneTimeCalendarEvent("Gnd Party", attendees,
        "Every budy shawa shawa", 
        DateTime.Now, 30.0, "Pune", 
        "rajendra.b", 
        Guid.NewGuid().ToString(),

        false);

    MailMessage message =obj.initMailMessage();

    //Add the attachment, specify it is a calendar file.
    System.Net.Mail.Attachment attachment =
    System.Net.Mail.Attachment.CreateAttachmentFromString(
    iCal.ToString(), new ContentType("text/calendar"));
    attachment.TransferEncoding = TransferEncoding.Base64;
    attachment.Name = "EventDetails.ics"; //not visible in outlook
    message.Attachments.Add(attachment);
    sendMailMessage(message);
}

发送邮件的方法:

private static void sendMailMessage(MailMessage mailMessage)
{
    SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

    SmtpServer.Port = 587;
    SmtpServer.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "@12345");
    SmtpServer.EnableSsl = true;

    SmtpServer.Send(mailMessage);
    Console.WriteLine("mail send");
    Console.ReadLine();
}
4

0 回答 0