A few weeks ago I had a network problem that caused a rather strange issue. The (rather important and urgent) mails I sent during the network-outage were waiting in the Outbox in Outlook, so far so good. After the network come back, I thought my mails were sent automatically by Outlook and waited for response from the addresses of the mails, but received none. After a while I’ve checked the Outbox, and the original mails were still there, even in the meantime I sent and received several other mails.
At this point I decided to look up a free Outlook add-in (or the source code of one would be even better) that alerts me in such cases, but surprisingly found none of them, although found a lot of people complaining for problems similar to my experience. So I started to create my custom add-in, using the Outlook 2010 Add-in project template in Visual Studio 2010.
The final source code for ThisAddIn.cs is illustrated by the code snippet below.
- using System;
- using Microsoft.Office.Interop.Outlook;
- // using Outlook = Microsoft.Office.Interop.Outlook;
- using Office = Microsoft.Office.Core;
- using System.Windows.Forms;
- namespace OutboxWatcher
- {
- public partial class ThisAddIn
- {
- private Timer _timer;
- private MAPIFolder _outbox;
- private bool _promtOpen;
- private void ThisAddIn_Startup(object sender, System.EventArgs e)
- {
- _outbox = this.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderOutbox);
- StartWatching();
- }
- private void StartWatching()
- {
- _timer = new Timer();
- // we set a notification interval of 15 secs here
- // that is OK for testing, in real life you should set a much longer duration,
- // not to get disturbed so frequently during your work
- _timer.Interval = 15000;
- _timer.Tick += _timer_Tick;
- _timer.Start();
- }
- void _timer_Tick(object sender, EventArgs e)
- {
- // if the former prompt is opened, we don't open anoter one
- if (!_promtOpen)
- {
- if ((_outbox != null) && (_outbox.Items != null) && (_outbox.Items.Count > 0))
- {
- _promtOpen = true;
- // in the offline mode we can't send mails from outbox, so we only display a warning message
- if (this.Application.Session.Offline)
- {
- MessageBox.Show("Outbox is not empty. You are in offline mode.");
- }
- else
- {
- DialogResult dr = MessageBox.Show("Outbox is not empty. Would you like to send mails now?",
- "Outbox is not empty", MessageBoxButtons.YesNo);
- // try to send mails if requested
- if (dr == DialogResult.Yes)
- {
- SendMails();
- }
- }
- _promtOpen = false;
- }
- }
- }
- private void SendMails()
- {
- if ((_outbox != null) && (_outbox.Items != null))
- {
- foreach (var item in _outbox.Items)
- {
- MailItem mailItem = item as MailItem;
- if (mailItem != null)
- {
- mailItem.Send();
- }
- }
- }
- }
- private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
- {
- _timer.Tick -= _timer_Tick;
- _timer.Stop();
- _timer = null;
- _outbox = null;
- }
- #region VSTO generated code
- /// <summary>
- /// Required method for Designer support – do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InternalStartup()
- {
- this.Startup += new System.EventHandler(ThisAddIn_Startup);
- this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
- }
- #endregion
- }
- }
All this add-in does is to periodically check the content of the Outbox. If it is not empty, and Outlook is offline, then it displays a warning message. If we are in a connected mode and the user chooses this option, it tries to send the mails as well. I hope it helps you to avoid such frustrating situations.