Monday, 9 April 2012

How to create and send outlook apointment in c# without any warning from Outlook

I received a requirement that send outlook appointment to the attendees of the meeting automatically after they booked the meeting.    
For that I found a solution to send a outlook appointment to the attendees.

Please add reference 'Microsoft.Office.Interop.Outlook'  Verson 11


private void test(string name, string location, string startTime, string endTime, string body, string email)
{
  Microsoft.Office.Interop.Outlook.Application outlookApp = new Microsoft.Office.Interop.Outlook.Application(); // creates new outlook app
            Microsoft.Office.Interop.Outlook.AppointmentItem oAppointment = (Microsoft.Office.Interop.Outlook.AppointmentItem)outlookApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem); // creates a new
            oAppointment.Subject = "Meeting""; // set the subject
            oAppointment.Body = "This is important"; // set the body
            oAppointment.Location = "NewYork"; // set the location
            oAppointment.Start = Convert.ToDateTime(startTime); // Set the start date
            oAppointment.End = Convert.ToDateTime(endTime); // End date
            oAppointment.ReminderSet = true; // Set the reminder
            oAppointment.ReminderMinutesBeforeStart = 15; // reminder time
            oAppointment.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh; // appointment importance
            oAppointment.BusyStatus = Microsoft.Office.Interop.Outlook.OlBusyStatus.olBusy;
            oAppointment.Save();
            Microsoft.Office.Interop.Outlook.MailItem mailItem = oAppointment.ForwardAsVcal();
            // email address to send to
            mailItem.To = email;
            // send
            mailItem.Send();
        }

Now the appointment was sending successfully.....

Important note you must configure you outlook in your machine because the from address is automatically fetched from the outlook account.

But I received a warning message like 'A program is trying to send an e-mail message on your behalf. If this is unexpected, click
deny and verify you antivirus software is up-to-date.'

This is terrible because the administrator of the server is not responsible to click allow or deny each and every time.

To stop this please follow the steps;
In outlook 2007 
1. Go to Toools->Trust Center
2. Select 'Programatic Access'
3. There select 'Never warn me about suspicious access'  because my antivirus was expired...... so check your antivirus and select the first option i.e 'Warn me about suspicious access when my antivirus is in-active or out-of-date'

Happy Coding :)