المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Outlook addin integration problem with Outlook 2007



C# Programming
03-12-2010, 03:40 AM
Hi,

This is my second post, I hope to do better that the first one..

I wrote a small addin in C# to Outlook 2007.
It connects to a local DB and should remove mail addresses which are either "Bounced" (not vaild) or asked to un-subscribe from the mailing list.

The addin was written using Visual Studio 2008 VSTO tools.
It does what it should, when I debug, yet when I step out of my managed code, the Outlook hangs, CPU is 100% and the message I see at the system tray is "Outlook is synchronizing folders".

When I remove my logic and use a "hard coded" address - it works.

I would also like to attach my code, since it's very long, I will only put some part of it here,
I can send the complete file (300 lines totally) by mail.

I will appreciate if someone is familiar with this http://www.barakasoft.com/script/Forums/Images/smiley_smile.gif

TIA,
Amit.


void App_ItemSend(object Item, ref bool Cancel)
{
Outlook.MailItem Mail = (Outlook.MailItem)Item;

//Get the addresses from the "To" field
string[] toArray =
Mail.To == null ? new string[] { } : Mail.To.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);

//Internal Data Structures to hold the "To"recipients
Dictionary<string, Recipient> toDic = new Dictionary<string, Recipient>();

List<string> emailToCheck = new List<string>();
List<string> invalidEmails = null;

//Remove spaces from the "To" list (originally recieved as "tuktuk@blabla.com; oved@sabih.com")
for (int i = 0; i < toArray.Length; i++)
{
toArray[i] = toArray[i].Replace(" ", "");
}

try
{
while (Mail.Recipients.Count > 0)
{
//Get the first recipient object in the recipient list
Recipient rec = Mail.Recipients[1];
//Remove the first object from the recipient list
Mail.Recipients.Remove(1);

List<Recipient> recList = new List<Recipient>();
ICollection<string> emailList = GetEmailsList(rec.AddressEntry);

foreach (string email in emailList)
{
//Create a new recipient
Recipient newRec = Mail.Session.CreateRecipient(email);
if (!newRec.Resolve())
{
//if eMail address can't be verified against the Outlook address book - skip it
continue;
}

//if eMail address is valid against the address book, add it to the current recipient list
recList.Add(newRec);

//Add the new recipient to the list of emails to check "Bounced"/"Un-Subscribed"
if (!emailToCheck.Contains(newRec.Address))
{
emailToCheck.Add(newRec.Address);
}
}

//If the current recipient is contained in the original "To" list, then go over the recipient list
//For each such recipient under that recipient name, add it the the "To" group
if (Array.IndexOf(toArray, rec.Name) >= 0)
{
foreach (Recipient r in recList)
{
toDic[r.Address.ToLower()] = r;
}
}

}

// Get the list of invalid email lists
invalidEmails = GetInvalidEmails(emailToCheck);
}
catch (System.Exception e)
{
Cancel = true;
MessageBox.Show(e.Message + "\nMailing Canceled", "Error Validating Emails");
return;
}

//Create the final data structure for the email lists
StringBuilder toList = new StringBuilder();

//Go over each entry in the "To" dictionary. In case the current entry is valid, add it to the "To" final list
foreach (KeyValuePair<string, Recipient> kvp in toDic)
{
if (invalidEmails == null || !invalidEmails.Contains(kvp.Key.Replace("'", "")))
{
toList.AppendFormat("{0};", kvp.Key);
}
}


if (toList.Length == 0 && ccList.Length == 0 && bccList.Length == 0)
{
MessageBox.Show("All recipients addresses are invalid or unsubscribed.\nSend operation aborted.");
Cancel = true;
return;
}

Mail.To = null;

//Add the address lists back to the original "To" list of the Outlook 2007 object
if (toList.Length > 0)
{
Mail.To = toList.ToString().Substring(0, toList.Length - 1);
}
}