Hello Harolds,
I found some community member write a C# version of this solution.
http://www.pcreview.co.uk/forums/thread-1854584.php
Here is the code:
//MailManager object in the Add-In
private MailManager _manager= new MailManager();
//applicationObject is Microsoft.Office.Interop.Outlook.Application
applicationObject.NewMail += new
Microsoft.Office.Interop.Outlook.ApplicationEvents
_10_NewMailEventHandler(ap
plicationObject_NewMail);
//Event Handler for Microsoft.Office.Interop.Outlook.Application.NewMa il
event
private void applicationObject_NewMail()
{
_manager.CleanInbox(this.applicationObject);
if(_manager.RemoveIcon)
_manager.RemoveNewMailIcon();
}
//Class to encapsulate Inbox Cleaning
public class MailManager
{
//Function pointer to Callback
private delegate bool EnumWindowProc(IntPtr hwnd, int i);
private bool _removeIcon = false;
[StructLayout(LayoutKind.Sequential)]
internal struct NOTIFYICONDATA
{
public int cbSize;
public IntPtr hwnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public int hIcon;
public string szTip;
}
[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wParam,
int lParam);
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hwnd,
[In][Out]StringBuilder
lpClassName, int nMaxCount);
[DllImport("user32.dll")]
private static extern int EnumWindows(EnumWindowProc callback, int
lParam);
[DllImport("shell32.dll")]
private static extern int Shell_NotifyIcon(int dwMessage, ref
NOTIFYICONDATA lpData);
public MailManager()
{
}
//Property if the mail should clear the Icon
public bool RemoveIcon
{
get { return _removeIcon; }
set { _removeIcon = value; }
}
public void ValidateNewMail(object item)
{
//Here is where I deteremine whether to keep to toss item.
Outlook.MailItem mail = item as Outlook.MailItem;
_removeIcon = false;
if (mail != null)
{
if (mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
_removeIcon = true;
}
else if (mail.Attachments.Count > 0)
{
Outlook.Attachment attachment = mail.Attachments[1] as
Outlook.Attachment;
if (attachment != null)
{
if (attachment.FileName == "InterScan_SafeStamp.txt")
{
mail.UnRead = false;
mail.Delete();
_removeIcon = true;
}
}
}
}
}
//Loops through all items in the Inbox. New Items are Validated.
public void CleanInbox(Outlook.Application outlook)
{
Outlook.NameSpace ns = outlook.GetNamespace("MAPI");
Outlook.MAPIFolder inbox =
ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
int i = inbox.Items.Count;
while (i > 0)
{
try
{
Outlook.MailItem mail = inbox.Items
as Outlook.MailItem;
if (mail.UnRead)
ValidateNewMail(mail);
}
catch { }
finally
{
i--;
}
}
}
public void RemoveNewMailIcon()
{
EnumWindows(new EnumWindowProc(this.EnumWindowCallback), 0);
}
private bool EnumWindowCallback(IntPtr hwnd, int i)
{
bool bReturn = true;
StringBuilder sb = new StringBuilder(64);
GetClassName(hwnd, sb, 64);
string sClass = sb.ToString();
if (sClass == "rctrl_renwnd32")
{
if (KillNewMailIcon(hwnd))
{
bReturn = false;
SendMessage(hwnd, 1031, 0, 0);
}
}
return bReturn;
}
private bool KillNewMailIcon(IntPtr hwnd)
{
bool bReturn = false;
NOTIFYICONDATA nid = new NOTIFYICONDATA();
nid.cbSize = Marshal.SizeOf(typeof(NOTIFYICONDATA));
nid.hwnd = hwnd;
nid.uID = 0;
int hResult;
hResult = Shell_NotifyIcon(2, ref nid);
if (hResult != 0)
bReturn = true;
return bReturn;
}
}
Hope this will be helpful!
Sincerely,
Wei Lu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
(This posting is provided "AS IS", with no warranties, and confers no
rights.)