I create a MailItem
like following:
Application outlook = new Application();
NameSpace ns = outlook.GetNameSpace("MAPI");
Inspector inspector;
AutoResetEvent mailSentEvent;
private void Compose()
{
MailItem mailItem = outlook.CreateItem(OlItemType.olMailItem));
inspector = mailItem.GetInspector;
inspector.Display(false);
((InspectorEvents_10_Event)inspector).Close += MailItem_Close;
mailSentEvent = new AutoResetEvent(false);
mailSentEvent.WaitOne();
}
private void MailItem_Close()
{
Console.WriteLine("MailItem_Close ...");
SyncObject syncObject = ns.SyncObjects[1];
syncObject.SyncEnd += SyncObject_SyncEnd;
syncObject.Start();
}
private void SyncObject_SyncEnd()
{
Console.WriteLine("SyncObject_SyncEnd ...");
mailSentEvent.Set();
}
EDIT After user clicks on Send the output is
MailItem_Close ...
SyncObject_SyncEnd ...
END EDIT
Problem: after the user clicks Send, mail is stuck in outbox when Outlook is not already running before MailItem
is created.
As described here, use of SyncObject
should avoid this issue. But it doesn't work: when Outlook has not already been running, MailItem
is stuck in outbox.
What am I missing? Is it possible at all to ensure mail is being sent if Outlook has not been running in advance?