How to tell which email item fired the event

O

oldprogrammer1234

I am writing a utility which is supposed to monitor my junk folder. Each time
a new email comes into the junk folder it gets the email address of the
sender and checks to see if it is in my customer database. Then it checks the
domain of the email address to see if it is in a list of domains that I care
about and lastly it checks the subject to see if it a topic I would be
interested in. If all this fails I delete the email. The program runs great
for a while. I will fire anywhere from once to about 24 is the most times I
have seen it. After that it just sits there and doesn't run anymore. So my
thought was maybe because a newmail event fires the code and the then check
every email in the junk folder that the new email that comes in must be
getting in the way somehow. So I tried to run the code just one time, but I
couldn't figure out to tell which email item is the one that fired the event.
Here is the code.

Any help would be appreciated.

public



Form1()

{
InitializeComponent();
Microsoft.Office.Interop.Outlook.Application myApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();

Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace =
myApp.GetNamespace("MAPI");

Microsoft.Office.Interop.Outlook.MAPIFolder myJunkBox =
mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderJunk);

myJunkBox.Application.NewMail +=new
Microsoft.Office.Interop.Outlook.ApplicationEvents_11_NewMailEventHandler(ClearJunk);
}

public void ClearJunk()
{
string Email;
bool BadEmail;
Microsoft.Office.Interop.Outlook.Application myApp = new
Microsoft.Office.Interop.Outlook.ApplicationClass();

Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace =
myApp.GetNamespace("MAPI");

Microsoft.Office.Interop.Outlook.MAPIFolder myJunkBox =
mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderJunk);

Microsoft.Office.Interop.Outlook.MAPIFolder myInbox =
mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

if (myJunkBox.Items.Count > 0)
{
while (myJunkBox.Items.Count > 0)
{
BadEmail =true;
Email = ((MOI.MailItem)myJunkBox.Items[1]).SenderEmailAddress;
BadEmail = CheckForPartialMatch(Email);
if(BadEmail){
BadEmail = CheckInDatabase(Email);
}
if(BadEmail){
BadEmail = CheckSubject((MOI.MailItem)myJunkBox.Items[1]);
}
if (BadEmail)
{
MOI.MailItem newMailItem = (MOI.MailItem)myJunkBox.Items[1];
newMailItem.Delete();
newMailItem =null;
}
else
{
MOI.MailItem KeepEmail = ((MOI.MailItem)myJunkBox.Items[1]);
KeepEmail.Subject ="Tested - " + KeepEmail.Subject;
KeepEmail.Move(myInbox);
KeepEmail = null;
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top