11

I am looking to send an outlook Meeting Request from C#. i have the code below that it do the job but.

string startTime1 = Convert.ToDateTime(startTime).ToString("yyyyMMddTHHmmssZ");
string endTime1 = Convert.ToDateTime(endTime).ToString("yyyyMMddTHHmmssZ");
SmtpClient sc = new SmtpClient("");

MailMessage msg = new MailMessage();

msg.From = new MailAddress("", "HR Self Service");
msg.To.Add(new MailAddress(emailto));
msg.Subject = "Holiday Approval";
msg.Body = emailbody;

StringBuilder str = new StringBuilder();
str.AppendLine("BEGIN:VCALENDAR");

//PRODID: identifier for the product that created the Calendar object
str.AppendLine("PRODID:-//ABC Company//Outlook MIMEDIR//EN");
str.AppendLine("VERSION:2.0");
str.AppendLine("METHOD:REQUEST");

str.AppendLine("BEGIN:VEVENT");

str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime1));//TimeZoneInfo.ConvertTimeToUtc("BeginTime").ToString("yyyyMMddTHHmmssZ")));
str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmssZ}", DateTime.UtcNow));
str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime1));//TimeZoneInfo.ConvertTimeToUtc("EndTime").ToString("yyyyMMddTHHmmssZ")));
str.AppendLine(string.Format("LOCATION: {0}", "Location"));

// UID should be unique.
str.AppendLine(string.Format("UID:{0}", Guid.NewGuid()));
str.AppendLine(string.Format("DESCRIPTION:{0}", msg.Body));
str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", msg.Body));
str.AppendLine(string.Format("SUMMARY:{0}", msg.Subject));

str.AppendLine("STATUS:CONFIRMED");
str.AppendLine("BEGIN:VALARM");
str.AppendLine("TRIGGER:-PT15M");
str.AppendLine("ACTION:Accept");
str.AppendLine("DESCRIPTION:Reminder");
str.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
str.AppendLine("END:VALARM");
str.AppendLine("END:VEVENT");

str.AppendLine(string.Format("ORGANIZER:MAILTO:{0}", msg.From.Address));
str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", msg.To[0].DisplayName, msg.To[0].Address));

str.AppendLine("END:VCALENDAR");
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType("text/calendar");
ct.Parameters.Add("method", "REQUEST");
ct.Parameters.Add("name", "meeting.ics");
AlternateView avCal = AlternateView.CreateAlternateViewFromString(str.ToString(), ct);
msg.AlternateViews.Add(avCal);
//Response.Write(str);
// sc.ServicePoint.MaxIdleTime = 2;
sc.Send(msg);

when the invitation is send, it need the user to accept the invitation, and when the user accept the invitation, Outlook calendar shows the status as BUSY
is there any way to send the invitation that does not require user to accept it and the Outlook calendar shows as Out Of Office ? i have tried with this 2 part but no luck

str.AppendLine("ACTION:Accept");
str.AppendLine("X-MICROSOFT-CDO-BUSYSTATUS:BUSY");
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • It's a well-formed question. However, I can't imagine Exchange would let any old SMTP message automatically accept a meeting request. – itsme86 Dec 09 '14 at 16:29
  • itsme86 Thank you for comment, is there any new SMTP that allow me to send email that will automatically accept a meeting request ? –  Dec 09 '14 at 16:41
  • Not SMTP, but have you looked into Exchange Web Services (EWS) at all? http://msdn.microsoft.com/en-us/library/office/dd877045%28v=exchg.140%29.aspx – itsme86 Dec 09 '14 at 17:11
  • 1
    i am trying to use this code to send a meeting request from console application written in c# , is there is anything i should do to configure the server , it gives me an error (The SMTP host was not specified) – Laila Oct 04 '16 at 10:07
  • @Laila you have to specify your smtp host in the empty string on line 3: SmtpClient sc = new SmtpClient(""); – AnimNations Jan 23 '18 at 18:26
  • hi @user4031669 your code is working if i use gmail for sending mail but if use any office 365 email address to send the mail then it gives me error InvalidRecipientsException: A message can't be sent because it contains no recipients. [Hostname=SG2PR01MB2823.apcprd01.prod.exchangelabs.com] can u please tell me do i need to change anything for sending by office 365 account. Thanks – Amit Yadav May 07 '19 at 13:09
  • and if i remove str.AppendLine(string.Format("UID:{0}", Guid.NewGuid())); this line then mail sends but it sends unsupported calendar file .ics file and event does not added in the outlook calender. – Amit Yadav May 07 '19 at 13:11
  • i have changed port to 587 and host to smtp.office365.com – Amit Yadav May 07 '19 at 13:16

1 Answers1

1

If this is a resource mailbox, you can configure it to auto accept meeting invitations (File | Options | Calendar | Automatic Accept or Decline).

If this is an arbitrary mailbox, nothing happens without the owner's permission. The best you can do is directly access the mailbox using the Outlook Object Model / EWS/ MAPI if you have the user credentials.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78