简体   繁体   中英

Google Calender Api v3 .net Send email notification when attendee replies to event

I'm using Google Calendar Api v3 for .Net in my project. I need to create event and send notification when any attendee replies to event ("Yes", "No"). I read the api documentation and tried what it says. Below is the code that i create service account credential and create event with organizer and creator information.

I have the domain "example.net" and trying to create event with organizer that has email "organizer@example.net" , both organizer and attendees can be from different domains like "gmail, outlook".

But whenever i create the event, api automatically sends email to attendees about event information, but organizer looks like "calendar@example-calendar.iam.gserviceaccount.com" which is defined in "calendar.json" file. So whenever attendee replies the event, organizer cannot get email about what an attendee replied for event. I couldnt able to set organizer to "organizer@example.com", it is always "calendar@example-calendar.iam.gserviceaccount.com".

Please look at the code below and tell me how can i set the organizer,creator of event to "organizer@example.com" (which also can be in different email address with different domain like gmail, outlook), or how can i make google calendar sends email notification to event organizer when any attendee replies to event (yes or no)

  private static Event SendCalendarInvitation()
    {
        try
        {
            string credPath = @"C:\calendar.json";

            var json = File.ReadAllText(credPath);
            var cr = JsonConvert.DeserializeObject<PersonalServiceAccountCred>(json); 

            var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.client_email)
            {
                Scopes = new[] {
                    CalendarService.Scope.Calendar
                }
            }.FromPrivateKey(cr.private_key));

            // Create the service

            CalendarService service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = xCred
            });

            Event calendarEvent = new Event
            {
                Summary = "Example Event",
                Location = "Los Angeles",
                Description = "Description",
                Start = new EventDateTime()
                {
                    DateTime = new DateTime(2018, 6, 2, 10, 0, 0),
                    TimeZone = "America/Los_Angeles"
                },
                End = new EventDateTime()
                {
                    DateTime = new DateTime(2018, 6, 2, 12, 0, 0),
                    TimeZone = "America/Los_Angeles"
                },
                Attendees = new List<EventAttendee>()
                {
                    new EventAttendee() { Email = "organizer@example.com", Organizer = true},
                    new EventAttendee() { Email = "attendee@gmail.com"}
                },
                Creator = new Event.CreatorData()
                {
                    Email = "organizer@example.com",
                    DisplayName = "Organizer"
                },
                Organizer = new Event.OrganizerData()
                {
                    Email = "organizer@example.com",
                    DisplayName = "Organizer"
                },
                Reminders = new Event.RemindersData()
                {
                    UseDefault = false,
                    Overrides = new List<EventReminder>()
                    {
                        new EventReminder()
                        {
                            Method = "email",
                            Minutes = 30
                        },
                        new EventReminder()
                        {
                            Method = "email",
                            Minutes = 14400
                        }
                    }
                }
            };

            Event calendarEventResult = null;

            var statusCode = System.Net.HttpStatusCode.Accepted;
            try
            {
                var request = service.Events.Insert(calendarEvent, "primary");
                request.SendNotifications = true;
                calendarEventResult = request.Execute();
            }
            catch (Exception ex)
            {
                statusCode = System.Net.HttpStatusCode.BadRequest;
            }
            return calendarEventResult;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

Events have a single organizer which is the calendar containing the main copy of the event. Events can also have multiple attendees. An attendee is usually the primary calendar of an invited user.

If the service account is set to be the organizer then you are sending the events on behalf of the service account. Which is a dummpy user. When it invites a user to an even the user will be notified that the person who created the event has added them in this case its "calendar@example-calendar.iam.gserviceaccount.com" There is no way to change this.

I haven't tried this. But if you do an Event.patch and have the service account set your personal account to be organizer it should then send mails from you and not the service account.

// Building the initial request.
var request = service.Events.Patch(body, calendarId, eventId);

// Applying optional parameters to the request.                
request = (EventsResource.PatchRequest)SampleHelpers.ApplyOptionalParms(request, optional);

// Requesting data.
return request.Execute();

code ripped from Events sample

Order of testing this idea.

  1. Create an event.
  2. Patch the event updating the organizer to be yourself instead of the service account.
  3. Do an event get see wither or not you were able to update this field. Documentation says that it should be writable.
  4. Add an attendee to the event. check what email its sent from.

IF it still doesn't work try setting your email as owner as well as organizer but i am worried the service account wont be able to edit it then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM