Reputation: 690
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?
Upvotes: 0
Views: 1113
Reputation: 66215
Make sure syncObject
variable is declared on the global level, otherwise it will get Garbage Collected.
Also, get rid of the mailSentEvent.WaitOne()
- it stops your app, and the event will never fire since it needs the Windows message pump to run.
Upvotes: 1
Reputation: 49397
You can use the Start method of the SyncObject
class to begin synchronizing a user's folders using the specified Send\Receive group if it is not perfomed automatically.
Public Sub Sync()
Dim nsp As Outlook.NameSpace
Dim sycs As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Dim i As Integer
Dim strPrompt As Integer
Set nsp = Application.GetNamespace("MAPI")
Set sycs = nsp.SyncObjects
For i = 1 To sycs.Count
Set syc = sycs.Item(i)
strPrompt = MsgBox( _
"Do you wish to synchronize " & syc.Name &"?", vbYesNo)
If strPrompt = vbYes Then
syc.Start
End If
Next
End Sub
This problem can occur if the Send immediately when connected option is not enabled, as shown in the following figure from Outlook 2016.
This setting is tied to the following registry data, so this setting can also be configured by an administrator through a modification of the registry.
Key: HKEY_CURRENT_USER\Software\Microsoft\Office\x.0\Outlook\Options\Mail
or
Policy key: HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\x.0\Outlook\Options\Mail
DWORD: Send Mail Immediately
Value: 0
Note In the above registry key paths, x.0 represents the Outlook version (16.0 = Outlook 2016, Outlook 2019 or Outlook for Office 365, 15.0 = Outlook 2013, 14.0 = Outlook 2010, 12.0 = Outlook 2007).
There are a lot of reasons why messages can be stuck in the outbox folder - the Work offline
mode is enabled in Outlook, another Outlook process is hanging in memory and etc.
Upvotes: 1