How can I abort the NewMail

  • Thread starter Thread starter Anthony Christianson
  • Start date Start date
A

Anthony Christianson

So far, I am able to intercept incoming mail, check to see if it meets
certain conditions, and then deleting it or letting it through.

When the item gets deleted, the little popup or the folder (depending on
which is turned on) still shows up.

What I would like to do, is intercept incoming mail, if the mail is valid,
show popup/folder. If incoming mail is not valid, delete it without having
the folder popup.



So far, I have been successful in intercepting, evaluating and deleting if
necessary, just havent figured out the popup/tray icon part.

Here is the Item received code:
private void Items_ItemAdd(object Item)
{
Outlook.MailItem mail = Item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
}
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();
}
}
}
}
}
 
Thanks Ken. FYI, here is the VB code adapted to .NET C#. I made a few
changes.

using System;
using System.Text;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;


namespace OutlookAddIn
{

public class MailManager
{
private const int WUM_RESETNOTIFICATION = 1031;
private const int NIM_DELETE = 2;

private delegate bool EnumWindowProc( IntPtr hwnd, int i);
[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()
{
}

public void ValidateNewMail(object item)
{
Outlook.MailItem mail = item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
RemoveNewMailIcon();
}
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();
RemoveNewMailIcon();
}
}
}
}
}


private 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,WUM_RESETNOTIFICATION,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(NIM_DELETE,ref nid);
if( hResult != 0)
bReturn = true;

return bReturn;
}
}
}


Ken Slovak - said:
Look at the Outlook VBA code sample at
http://www.slipstick.com/dev/code/clearenvicon.htm for how to clear
the envelope icon in the System Tray. You can adapt that code.




Anthony Christianson said:
So far, I am able to intercept incoming mail, check to see if it meets
certain conditions, and then deleting it or letting it through.

When the item gets deleted, the little popup or the folder (depending on
which is turned on) still shows up.

What I would like to do, is intercept incoming mail, if the mail is valid,
show popup/folder. If incoming mail is not valid, delete it without having
the folder popup.



So far, I have been successful in intercepting, evaluating and deleting if
necessary, just havent figured out the popup/tray icon part.

Here is the Item received code:
private void Items_ItemAdd(object Item)
{
Outlook.MailItem mail = Item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
}
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();
}
}
}
}
}
 
There is a timing issue though. I am still working on a fix for it.


Anthony Christianson said:
Thanks Ken. FYI, here is the VB code adapted to .NET C#. I made a few
changes.

using System;
using System.Text;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;


namespace OutlookAddIn
{

public class MailManager
{
private const int WUM_RESETNOTIFICATION = 1031;
private const int NIM_DELETE = 2;

private delegate bool EnumWindowProc( IntPtr hwnd, int i);
[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()
{
}

public void ValidateNewMail(object item)
{
Outlook.MailItem mail = item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
RemoveNewMailIcon();
}
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();
RemoveNewMailIcon();
}
}
}
}
}


private 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,WUM_RESETNOTIFICATION,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(NIM_DELETE,ref nid);
if( hResult != 0)
bReturn = true;

return bReturn;
}
}
}


Ken Slovak - said:
Look at the Outlook VBA code sample at
http://www.slipstick.com/dev/code/clearenvicon.htm for how to clear
the envelope icon in the System Tray. You can adapt that code.




Anthony Christianson said:
So far, I am able to intercept incoming mail, check to see if it meets
certain conditions, and then deleting it or letting it through.

When the item gets deleted, the little popup or the folder (depending on
which is turned on) still shows up.

What I would like to do, is intercept incoming mail, if the mail is valid,
show popup/folder. If incoming mail is not valid, delete it without having
the folder popup.



So far, I have been successful in intercepting, evaluating and deleting if
necessary, just havent figured out the popup/tray icon part.

Here is the Item received code:
private void Items_ItemAdd(object Item)
{
Outlook.MailItem mail = Item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
}
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();
}
}
}
}
}
 
Final working code.

One draw back of this code, if you receive a valid email followed by an
invalid email, this will cause the Icon to be removed.



In the Add-In Class

//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.NewMail
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;
}
}
Anthony Christianson said:
There is a timing issue though. I am still working on a fix for it.


Anthony Christianson said:
Thanks Ken. FYI, here is the VB code adapted to .NET C#. I made a few
changes.

using System;
using System.Text;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;


namespace OutlookAddIn
{

public class MailManager
{
private const int WUM_RESETNOTIFICATION = 1031;
private const int NIM_DELETE = 2;

private delegate bool EnumWindowProc( IntPtr hwnd, int i);
[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()
{
}

public void ValidateNewMail(object item)
{
Outlook.MailItem mail = item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
RemoveNewMailIcon();
}
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();
RemoveNewMailIcon();
}
}
}
}
}


private 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,WUM_RESETNOTIFICATION,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(NIM_DELETE,ref nid);
if( hResult != 0)
bReturn = true;

return bReturn;
}
}
}


Ken Slovak - said:
Look at the Outlook VBA code sample at
http://www.slipstick.com/dev/code/clearenvicon.htm for how to clear
the envelope icon in the System Tray. You can adapt that code.




in message So far, I am able to intercept incoming mail, check to see if it
meets
certain conditions, and then deleting it or letting it through.

When the item gets deleted, the little popup or the folder
(depending on
which is turned on) still shows up.

What I would like to do, is intercept incoming mail, if the mail is
valid,
show popup/folder. If incoming mail is not valid, delete it without
having
the folder popup.



So far, I have been successful in intercepting, evaluating and
deleting if
necessary, just havent figured out the popup/tray icon part.

Here is the Item received code:
private void Items_ItemAdd(object Item)
{
Outlook.MailItem mail = Item as Outlook.MailItem;
if( mail != null)
{
if( mail.Subject == "InterScan NT Alert")
{
mail.UnRead = false;
mail.Delete();
}
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();
}
}
}
}
}
 
Back
Top