Quantcast
Channel: Second Life of a Hungarian SharePoint Geek
Viewing all articles
Browse latest Browse all 206

Creating an Outlook add-in to handle mails waiting in outbox

$
0
0

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.

image

The final source code for ThisAddIn.cs is illustrated by the code snippet below.

Code Snippet
  1. using System;
  2. using Microsoft.Office.Interop.Outlook;
  3. // using Outlook = Microsoft.Office.Interop.Outlook;
  4. using Office = Microsoft.Office.Core;
  5. using System.Windows.Forms;
  6.  
  7. namespace OutboxWatcher
  8. {
  9.     public partial class ThisAddIn
  10.     {
  11.         private Timer _timer;
  12.         private MAPIFolder _outbox;
  13.         private bool _promtOpen;
  14.  
  15.         private void ThisAddIn_Startup(object sender, System.EventArgs e)
  16.         {
  17.             _outbox = this.Application.Session.GetDefaultFolder(OlDefaultFolders.olFolderOutbox);
  18.             StartWatching();
  19.         }
  20.  
  21.         private void StartWatching()
  22.         {
  23.             _timer = new Timer();
  24.             // we set a notification interval of 15 secs here
  25.             // that is OK for testing, in real life you should set a much longer duration,
  26.             // not to get disturbed so frequently during your work
  27.             _timer.Interval = 15000;
  28.             _timer.Tick += _timer_Tick;
  29.             _timer.Start();
  30.         }
  31.  
  32.         void _timer_Tick(object sender, EventArgs e)
  33.         {
  34.             // if the former prompt is opened, we don't open anoter one
  35.             if (!_promtOpen)
  36.             {
  37.                 if ((_outbox != null) && (_outbox.Items != null) && (_outbox.Items.Count > 0))
  38.                 {
  39.                     _promtOpen = true;
  40.  
  41.                     // in the offline mode we can't send mails from outbox, so we only display a warning message
  42.                     if (this.Application.Session.Offline)
  43.                     {
  44.                         MessageBox.Show("Outbox is not empty. You are in offline mode.");
  45.                     }
  46.                     else
  47.                     {
  48.                         DialogResult dr = MessageBox.Show("Outbox is not empty. Would you like to send mails now?",
  49.                                    "Outbox is not empty", MessageBoxButtons.YesNo);
  50.                         // try to send mails if requested
  51.                         if (dr == DialogResult.Yes)
  52.                         {
  53.                             SendMails();
  54.                         }
  55.                     }
  56.  
  57.                     _promtOpen = false;
  58.                 }
  59.             }
  60.         }
  61.  
  62.         private void SendMails()
  63.         {
  64.             if ((_outbox != null) && (_outbox.Items != null))
  65.             {
  66.                 foreach (var item in _outbox.Items)
  67.                 {
  68.                     MailItem mailItem = item as MailItem;
  69.                     if (mailItem != null)
  70.                     {
  71.                         mailItem.Send();
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.  
  77.         private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
  78.         {
  79.             _timer.Tick -= _timer_Tick;
  80.             _timer.Stop();
  81.             _timer = null;
  82.             _outbox = null;
  83.         }
  84.  
  85.         #region VSTO generated code
  86.  
  87.         /// <summary>
  88.         /// Required method for Designer support – do not modify
  89.         /// the contents of this method with the code editor.
  90.         /// </summary>
  91.         private void InternalStartup()
  92.         {
  93.             this.Startup += new System.EventHandler(ThisAddIn_Startup);
  94.             this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
  95.         }
  96.         
  97.         #endregion
  98.     }
  99. }

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.



Viewing all articles
Browse latest Browse all 206

Trending Articles