Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Interop / November 2003

Tip: Looking for answers? Try searching our database.

How can I abort the NewMail

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anthony Christianson - 19 Nov 2003 16:46 GMT
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();
               }
           }
       }
   }
}
Ken Slovak - [MVP - Outlook] - 20 Nov 2003 15:00 GMT
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Absolute Beginners Guide to Microsoft Office Outlook 2003
Reminder Manager, Extended Reminders, Attachment Options
http://www.slovaktech.com/products.htm

> 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.
[quoted text clipped - 35 lines]
>     }
> }
Anthony Christianson - 20 Nov 2003 17:24 GMT
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;
 }
}
}

> Look at the Outlook VBA code sample at
> http://www.slipstick.com/dev/code/clearenvicon.htm for how to clear
[quoted text clipped - 53 lines]
> >     }
> > }
Anthony Christianson - 20 Nov 2003 17:59 GMT
There is a timing issue though.  I am still working on a fix for it.

> Thanks Ken.  FYI, here is the VB code adapted to .NET C#.  I made a few
> changes.
[quoted text clipped - 176 lines]
> > >     }
> > > }
Anthony Christianson - 20 Nov 2003 20:09 GMT
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[i] 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;
}
}
> There is a timing issue though.  I am still working on a fix for it.
>
[quoted text clipped - 179 lines]
> > > >     }
> > > > }
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.