Send and edit a meeting request to outlook using a non-office library

Source: Internet
Author: User
Tags mailmessage smtpclient

Originally, I wanted to focus on WPF. Recently, I was busy with the Product Demo. I need to study how to use the C # code to operate Meeting Request in Outlook, and I found something on some forums both at home and abroad, it is a pity that the Demo will be lost later, but after all it takes time. To sum up and share it, we are more comfortable (a lot of knowledge comes from stackoverflow, which is recommended here ). This section mainly summarizes the functional integrity and availability. You are welcome to add your Office interoperability experience.
The gossip is over and the subject is started.

In Outlook, we can initiate a meeting in the Calendar and modify or cancel it. Using Outlook APIs, we can implement these functions. However, in many cases, Outlook is not installed on our server or is not allowed to be installed, use C # code to implement these functions? How can we initiate a Calendar Meeting Request to others in the name of a third party? This is what we will explain in this article.

In general, the process is relatively simple, that is, C # builds the ICS file and sends it to the mail server. When the recipient receives the file, Outlook parses the ICS file and performs corresponding operations.

 

1. Create an ICS file.

public string BuildIcsFormatString(DateTime startTime, DateTime endTime, ICollection<string> attendees, string organizer,            string subject, string description, string guid, string location)        {            System.Text.StringBuilder sw = new System.Text.StringBuilder();            sw.AppendLine("BEGIN:VCALENDAR");            sw.AppendLine("VERSION:2.0");            sw.AppendLine("METHOD:REQUEST");            sw.AppendLine("BEGIN:VEVENT");            if (attendees != null)            {                foreach (string attendee in attendees)                {                    sw.AppendLine("ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:mailto:" + attendee);                }            }            sw.AppendLine("CLASS:PUBLIC");            sw.AppendLine(string.Format("CREATED:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));            sw.AppendLine("DESCRIPTION:" + description);            sw.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime));            sw.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));            sw.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime));            sw.AppendLine("ORGANIZER;CN=\"NAME\":mailto:" + organizer);            sw.AppendLine("SEQUENCE:0");            sw.AppendLine("UID:" + guid);            sw.AppendLine("LOCATION:" + location);            sw.AppendLine("SUMMARY;LANGUAGE=en-us:" + subject);            sw.AppendLine("BEGIN:VALARM");            sw.AppendLine("TRIGGER:-PT720M");            sw.AppendLine("ACTION:DISPLAY");            sw.AppendLine("DESCRIPTION:Reminder");            sw.AppendLine("END:VALARM");            sw.AppendLine("END:VEVENT");            sw.AppendLine("END:VCALENDAR");            return sw.ToString();        }

Tips1-1In the ICS file, ROLE = REQ-PARTICIPANT indicates that the current attendee type is Required. If we need an attendee that is Optional, Use ROLE = OPT-PARTICIPANT. Therefore, this code can also enhance the attendee used to distinguish Required or Optional.

Tips1-2: How can we differentiate Create and Update through ICS? ICS uses IDs to differentiate between different Calendar Meeting requests. Therefore, we issue An ICS file. If this ID does not exist in the recipient's email system, outlook performs the Create operation. If it exists, Outlook recognizes the request as an Update operation.

Tips1-3: How do we perform the Delete operation through ICS? According to 1-2, we first need an existing ID, and the METHOD: REQUEST in the ICS file should be changed to METHOD: CANCEL (STATUS: CANCELLED should also be added after METHOD: CANCEL, but no difference is found after actual test ).

 

2. Send with SMTP

                        string meetingInfo = BuildIcsFormatString(...);                    System.Net.Mime.ContentType mimeType = new System.Net.Mime.ContentType("text/calendar; method=REQUEST");                    AlternateView icsView = AlternateView.CreateAlternateViewFromString(meetingInfo, mimeType);                    MailMessage message = new MailMessage();                    if (attendees != null)                    {                        foreach (string attendee in attendees)                        {                            message.To.Add(attendee);                        }                    }                    message.From = new MailAddress(organizer);                    message.AlternateViews.Add(icsView);                    using (SmtpClient client = new SmtpClient("MailServerName", 25))                    {                        client.Send(message);                    }

 

3. Relationship between From, To, Organizer, and Attendees

In fact, according To steps 1 and 2, this example is already very complete. However, you may think that the relationship between From, To, Organizer and Attendees is unclear, we need to test it. The following is my detailed test conclusion and I hope to help you.

Test premise: for example we now have 3 people A, B, C, mail addresses are aaa@abc.com, bbb@abc.com, ccc@abc.com.

3-1: MailMessage From is required. If it is null, an error is returned. However, the recipient of the Calendar Meeting Request does not see any information about the From address. The recipient sees the Organizer in the ICS file. This allows us to initiate a Calendar Meeting Request in the name of a third party. For example, I can write A program that uses A as the Organizer and B as the Attendee to initiate A Calendar Meeting Request. After B's Outlook receives the Request, in B's opinion, this Request is issued by, no information is visible to me.

3-2: If C is in MailMessage. From and ICS as Organizer, A is in MailMessage. To, but A and B are both in ICS as Attendees-only A will receive the Request.

3-3: If C is in MailMessage. From and ICS as Organizer, A and B are in MailMessage. To, both A and B receive A Request in ICS as Attendees --- A and B.

3-4: If C is in MailMessage. From and ICS as Organizer, A and B are in MailMessage. To, only A is in ICS as Attendees --- only A will receive the Request.

3-5: If C is in MailMessage. from, ICS as Organizer, A, B, C in MailMessage. to, A, B, and C will receive the Request in ICS as Attendees --- A, B, and C. However, in C's view, you are an Organizer and your name will not appear in the Required or Optional list of Outlook.

The above conclusions are sufficient for our normal use, but I believe that the theoretical rules will be detailed and complex. If you have a good understanding of this, please add and correct them.

4. added questions

The problem persists: The Organizer can receive the Request, but the Calendar of the Organizer cannot be updated accordingly. When Outlook finds that the current receiver is Organizer, it does not allow the accept Request. As a result, when a third-party program initiates a Request in the name of another person, the Calendar of the initiator cannot be updated accordingly. I have sent this question to MSDN and stackoverflow, but I have not responded to it. I hope you can help me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.