Why Outlook is not getting synchronized - c#

I am developing a C# application that uses Outlook COM object library.
After adding the appointment to the calendar, it is immediately shown in Outlook desktop application but the appointment is not found at Outlook web application. The miserable thing is that some appointments got synchronized and it stopped synchronizing suddenly.
Here's how I saved the appointment:
Outlook.Application olApp = new Outlook.Application();
Outlook.NameSpace mapiNS = olApp.GetNamespace("MAPI");
string profile = "";
mapiNS.Logon(profile, null, null, null);
Outlook.AppointmentItem apt = olApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
apt.Save();
How can I make it get synchronized?

I had to call send method after the save method, otherwise the Outlook sends the appointment whenever it wants.
Outlook.Application olApp = new Outlook.Application();
Outlook.NameSpace mapiNS = olApp.GetNamespace("MAPI");
string profile = "";
mapiNS.Logon(profile, null, null, null);
Outlook.AppointmentItem apt = olApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
apt.Save();
apt.Send();

Related

Get Outlook AppointmentItem using GlobalAppointmentID

I am developing a calendar for my firm that synchronizes with Outlook Calendar.
Atm I can:
import appointments from Outlook and show them in my calendar
update my appointments when Outlook appointments get updated
create Outlook appointments when appointments get created in my calendar
The only issue I have is updating/deleting Outlook appointments when my appointments update/delete.
I have the GlobalAppointmentID of the corresponding appointments but I can't seem to search on that ID.
I tried:
using Microsoft.Office.Interop;
private void GetAppointment(string myGlobalAppointmentID)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace mapiNamespace = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder calendarFolder = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items outlookCalendarItems = calendarFolder.Items;
Outlook.AppointmentItem appointmentItem = (Outlook.AppointmentItem)outlookCalendarItems.Find("[GlobalAppointmentID] = '{0}'", myGlobalAppointmentID));
//update or delete appointmentItem here (which I know how to do)
}
I keep getting 'Condition is not valid' Exception.
Apparently Outlook does not allow to search on binary properties (such as GlobalAppointmentID).
I use the same outlookCalendarItems.Find() and calendarFolder.Items.Restrict() without problems in other instances.
I tried using Redemption but I couldn't get it to work either.
Does anybody have experience or a suggestion?
Yes, OOM won't let you search on binary properties (as well as recipients or attachments), but Redemption (I am its author) should work. The following script (VBA) worked just fine for me:
set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Folder = Session.GetDefaultFolder(olFolderCalendar)
set appt = Folder.Items.Find("GlobalAppointmentID = '040000008200E00074C5B7101A82E00800000000D0FECEE58FEAD70100000000000000001000000041C887A3FA12694F8A0402FEFFAD0BBB'")
MsgBox appt.Subject
The Outlook object model doesn't support searching for binary properties such as GlobalAppointmentId (any other PT_BINARY property) with Items.Find/Items.FindNext/Items.Restrict.
The only workaround is to loop through all item in the Calendar folder (which is inefficient) or search using Extended MAPI (or using third-party wrappers such as Redemption.
What I ended up doing after looking further:
I added a text UserProperty where I put the GlobalAppointmentID("GAID") in.
You can Filter on those.
And it seems to do the trick.
private void AddGAIDIfNeeded(Outlook.AppointmentItem app)
{
bool GAIDexists = false;
if (app.UserProperties.Count != 0)
{
foreach (UserProperty item in app.UserProperties)
{
if (item.Name == "GAID")
{
GAIDexists = true;
break;
}
}
}
if (GAIDexists == false)
{
app.UserProperties.Add("GAID", Outlook.OlUserPropertyType.olText);
app.UserProperties["GAID"].Value = app.GlobalAppointmentID;
app.Save();
}
}
And to find a specific AppointmentItem:
private void DeleteOutlookAppointmentByGAID(string globalAppointmentID)
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace mapiNamespace = oApp.GetNamespace("MAPI");
Outlook.MAPIFolder calendarFolder = mapiNamespace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items outlookCalendarItems = calendarFolder.Items;
try
{
Outlook.AppointmentItem appointmentItem = null;
appointmentItem = outlookCalendarItems.Find(String.Format("[GAID] = '{0}'", globalAppointmentID));
if (appointmentItem != null)
{
appointmentItem.Delete();
}
}
catch (Exception ex)
{
classExecptionLogging.LogErrorToFolder(ex);
}
}

Changing the sender address to default server in Outlook Object Mail

I have the following code to send emails automatically when looping through data retrieved from db:
public void sendMailV2(string subject, string body, string emailAddress)
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Get the NameSpace and Logon information.
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using a dialog box to choose the profile.
oNS.Logon(Missing.Value, Missing.Value, true, true);
// Alternate logon method that uses a specific profile.
// TODO: If you use this logon method,
// change the profile name to an appropriate value.
//oNS.Logon("YourValidProfile", Missing.Value, false, true);
// Create a new mail item.
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
// Set the subject.
oMsg.Subject = subject;
// Set HTMLBody.
oMsg.HTMLBody = body;
// Add a recipient.
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
// TODO: Change the recipient in the next line if necessary.
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(emailAddress);
oRecip.Resolve();
// Send.
oMsg.Send();
// Log off.
oNS.Logoff();
// Clean up.
oRecip = null;
oRecips = null;
oMsg = null;
oNS = null;
oApp = null;
}
However, I want the emails to be sent from the server, not my own outlook. I have the username and password for the server(someserver#serving.com) but I can't figure out how and where to implement them.
I would appreciate any help.
First of all, there is no need to create a new Application instance if you need to send multiple emails. You may consider moving the following lines of code outside of the method and create the Application instance at the global scope.
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
If you have got another accounts configured in Outlook, you can use the SendUsingAccount property of the MailItem class which allows to set an Account object that represents the account under which the MailItem is to be sent.
If you don't have the required account configured in Outlook you may consider using the BCL classes for getting the job done. See How to send email from C# for more information.

Interop Outlook - Sending Appointment from another mailbox

I have two mailboxes setup in Outlook.
I'll refer to them as "email1#mail.com" and "email2#mail.com".
I would like to use Interop to create and send an appointment to a specific email address calender, not just to the default outlook account.
using System;
using System.Diagnostics;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace Program
{
class Program
{
public static void Main(string[] args)
{
// Create the Outlook application.
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Outlook.Account account = oApp.Session.Accounts["email2#mail.com"];
// Get the NameSpace and Logon information.
Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using a dialog box to choose the profile.
oNS.Logon(Missing.Value, Missing.Value, true, true);
// Create a new mail item.
Microsoft.Office.Interop.Outlook.MailItem oMsg =(Microsoft.Office.Interop.Outlook.MailItem) oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
// Set the subject.
oMsg.Subject = "test";
// Set HTMLBody.
oMsg.HTMLBody = "test";
oMsg.To = "test#gmail.com";
//oMsg.CC = _cc;
//oMsg.BCC = _bcc;
oMsg.Save();
oMsg.SendUsingAccount = account;
// Add a recipient.
//Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
// TODO: Change the recipient in the next line if necessary.
//Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(_recipient);
//oRecip.Resolve();
// Send.
(oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send();
// Log off.
oNS.Logoff();
// Clean up.
//oRecip = null;
//oRecips = null;
oMsg = null;
oNS = null;
oApp = null;
}
}
}
This code works flawlessly in sending an email automatically to "test#gmail.com" from my email "email2#mail.com".
However, I would like to automatically create an appointment/meeting for a specific email address.
This is my current attempt:
using System;
using System.Diagnostics;
using System.Reflection;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace SendEventToOutlook
{
class Program
{
public static void Main(string[] args)
{
try
{
// Create the Outlook application.
Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
Outlook.Account account = oApp.Session.Accounts["email2#mail.com"];
// Get the nameSpace and logon information.
Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
// Log on by using a dialog box to choose the profile.
oNS.Logon(Missing.Value, Missing.Value, true, true);
// Create a new Appointment item.
Microsoft.Office.Interop.Outlook.AppointmentItem appt =
(Microsoft.Office.Interop.Outlook.AppointmentItem)
oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
appt.Start = DateTime.Now;
appt.End = DateTime.Now.AddDays(7);
appt.Location = "Test";
appt.Body = "Test";
appt.AllDayEvent = false;
appt.Subject = "Test";
appt.Save();
appt.SendUsingAccount = account;
// Log off.
oNS.Logoff();
appt = null;
oNS = null;
oApp = null;
}
catch (Exception ex)
{
Debug.WriteLine("The following error occurred: " + ex.Message);
}
}
}
}
This code does create an appointment successfully, but it keeps creating an appointment for "email1#mail.com" instead of "email2#mail.com", which shouldn't happen as I've specified the sending account to be "email2#mail.com" from the lines:
Outlook.Account account = oApp.Session.Accounts["email2#mail.com"];
and then
appt.SendUsingAccount = account;
This is how my two email addresses are set up in Outlook: http://i.imgur.com/0eopV8A.png
Both the email addresses have different user names and are from different domains/mail servers, as shown in that screenshot.
Would anyone be able to see the problem I'm making or if there's a different solution?
Thank you.
It is not clear whether you have got two accounts set up in the single Mail profile or separate profiles.
The SendUsingAccount property of the AppointmentItem class allows to set an Account object that represents the account under which the AppointmentItem is to be sent. So, The SendUsingAccount property can be used to specify the account that should be used to send the AppointmentItem when the Send method is called. It is not what you are looking for I suppose.
Anyway, you can use the GetDefaultFolder method of the Store class which returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereas NameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.
Thus, you can get the Calendar folder for the required account and add a new appointment there.
You may find the following articles in MSDN helpful:
How to: Create a Sendable Item for a Specific Account Based on the Current Folder (Outlook)
Using Multiple Accounts for the Same Profile on Outlook

How to set an appointment to other users on outlook?

Let's say I log in to the OS with administrator account and have permissions to set appointments to other users, without sending a mail.
How can I do it in the code?
I could only find examples working with AppontmentItem and set an appointment to the local machine's outlook. How can I do it for external users?
Many thanks in advance!
private static void AddAppointment()
{
Outlook.Application outlookApp = new Outlook.Application(); // creates new outlook app
Outlook.AppointmentItem oAppointment =
(Outlook.AppointmentItem) outlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
// creates a new appointment
oAppointment.Subject = "Enquiry Changes made to john enquiry"; // set the subject
oAppointment.Body = "This is where the appointment body of the appointment is written"; // set the body
oAppointment.Location = "Nicks Desk!"; // set the location
oAppointment.Start = DateTime.Now.AddHours(2);
oAppointment.End = DateTime.Now.AddHours(3);
oAppointment.ReminderSet = true; // Set the reminder
oAppointment.ReminderMinutesBeforeStart = 15; // reminder time
oAppointment.Importance = Outlook.OlImportance.olImportanceHigh; // appointment importance
oAppointment.BusyStatus = Outlook.OlBusyStatus.olBusy;
oAppointment.Save();
Outlook.MailItem mailItem = oAppointment.ForwardAsVcal();
}
Use Namespace.GetSharedDefaultFolder() to open other user's Calendar folder, then create an appointment using MAPIFolder.Items.Add.

Update an Existing Outlook Appointment

Having created an outlook appointment doing something like this;
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook.AppointmentItem appt = null;
app = new Microsoft.Office.Interop.Outlook.Application();
appt = (Microsoft.Office.Interop.Outlook.AppointmentItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olAppointmentItem);
appt.Subject = sub;
appt.Body = body;
appt.Location = Loc;
appt.Recipients.Add(email);
appt.Save();
appt.Send();
Outlook.MailItem mailItem = appt.ForwardAsVcal();
mailItem.To = email;
mailItem.Body = body;
mailItem.Send();
Looking for pointers, as I'm having difficulty finding a definitive answer via Google.
How do I then (at a later date..) retrieve this appointment again from one or more outlook accounts and update some details?
Or is it possible to release a new appointment that will overwrite the existing one?
If you are always accessing the appointment from the same mailbox, store the value of the AppointmentItem.EntryID property and reopen it at a later point using Namespace.GetItemfromID.
If you need to access the appointment from multiple mailboxes, read the value of the AppointmentItem.GlobalAppointmentID. Unfortunately Outlook Object Model would not let you search on a binary property (MAPIFolder.Items.Find). You would need to either drop down to the Extended MAPI level (C++ or Delphi) or use Redemption - see
http://social.msdn.microsoft.com/Forums/en-US/outlookdev/thread/63ea7bda-9767-4145-8ced-78e1095a49f8/

Categories

Resources