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

مشاهدة النسخة كاملة : Outlook 2007 addin causes outlook to hand + high CPU



C# Programming
03-08-2010, 02:51 AM
Hi,

I developed a simple application with C#, using the VSTO 2008 tools.

It is supposed to be used a mass-mailing item from Outlook 2007.
All it does is taking the mail recipients from the "To", "CC" & "BCC" and filter out the addresses which are either invalid or marked as non-subscribers. (There is a DB which stores all the email addresses)

The problem is not the logic inside, it works fine.

When the method is complete, the outlook starts to go crazy:
The icons at the bottom system tray flickers, CPU soars to 100% and the mail is stuck in the outbox.
Sometimes I can see the "Outlook is synchronizing folders".

Has anyone got the clue why is it happening?

I am attaching some of my code:
namespace SMOutlook2007AddIn
{
public partial class ThisAddIn
{

private Outlook.Application App = new Outlook.Application();

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
if (string.IsNullOrEmpty(ACCOUNT_NAME))
{
MessageBox.Show("Couldn't Load SM Addin.\n No ACCOUNT_NAME in config file.");
}
else
{
App.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(App_ItemSend);
}
}

private static ICollection GetEmailsList(AddressEntry addressEntry)
{
if (addressEntry.DisplayType == OlDisplayType.olUser)
{
return new string[] { addressEntry.Address };
}

if (addressEntry.DisplayType != OlDisplayType.olPrivateDistList)
{
throw new System.Exception("Unknown Recipient Type " + addressEntry.DisplayType + "\nRecipient Name: " + addressEntry.Name);
}

if (addressEntry.Members == null || addressEntry.Members.Count == 0)
{
return new string[0];
}

List addresses = new List();
foreach (AddressEntry entry in addressEntry.Members)
{
addresses.AddRange(GetEmailsList(entry));
}
return addresses;
}

private static List GetInvalidEmails(List Emails)
{
if (Emails == null || Emails.Count == 0)
{
return null;
}

StringBuilder emailsList = new StringBuilder();
for (int i = 0; i < Emails.Count; i++)
{
emailsList.AppendFormat("'{0}'", Emails[i]);
if (i != Emails.Count - 1)
{
emailsList.Append(",");
}
}

List invalidEmails = new List();
string queryString = QUERY.Replace("{0}", emailsList.ToString().Replace("''","'"));

SqlCommand cmd = new SqlCommand();
cmd.CommandText = queryString;
cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["StrongMail"].ConnectionString);

SqlDataReader dataReader = null;
try
{
cmd.Connection.Open();

dataReader = cmd.ExecuteReader();
while (dataReader.Read())
{
invalidEmails.Add(dataReader["Email"].ToString());
}
}
finally
{
if (dataReader != null && !dataReader.IsClosed)
{
dataReader.Close();
}
if (cmd.Connection.State == ConnectionState.Open)
{
cmd.Connection.Close();
}
}

return invalidEmails;
}

void App_ItemSend(object Item, ref bool Cancel)
{
Outlook.MailItem Mail = (Outlook.MailItem)Item;
if (!Mail.SendUsingAccount.DisplayName.Equals(ACCOUNT_NAME, StringComparison.OrdinalIgnoreCase))
{
return;
}

string[] toArray =
Mail.To == null ? new string[] { } : Mail.To.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);

string[] ccArray =
Mail.CC == null ? new string[] { } : Mail.CC.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);

string[] bccArray =
Mail.BCC == null ? new string[] { } : Mail.BCC.Split(new char[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);

Dictionary toDic = new Dictionary();
Dictionary ccDic = new Dictionary();
Dictionary bccDic = new Dictionary();

List emailToCheck = new List();
List invalidEmails = null;

for (int i = 0; i < toArray.Length; i++)
{
toArray[i] = toArray[i].Replace(" ", "");
}

for (int i = 0; i < ccArray.Length; i++)
{
ccArray[i] = ccArray[i].Replace(" ", "");
}

for (int i = 0; i < bccArray.Length; i++)
{
bccArray[i] = bccArray[i].Replace(" ", "");
}

try
{
while (Mail.Recipients.Count > 0)
{
Recipient rec = Mail.Recipients[1];
Mail.Recipients.Remove(1);

List recList = new List();
ICollection emailList = GetEmailsList(rec.AddressEntry);

foreach (string email in emailList)
{
Recipient newRec = Mail.Session.CreateRecipient(email);
if (!newRec.Resolve())
{
continue;
}
recList.Add(newRec);

if (!emailToCheck.Contains(newRec.Address))
{
emailToCheck.Add(newRec.Address);
}
}

if (Array.IndexOf(toArray, rec.Name) >= 0)
{
foreach (Recipient r in recList)
{
toDic[r.Address.ToLower()] = r;
}
}

if (Array.IndexOf(ccArray, rec.Name) >= 0)
{
foreach (Recipient r in recList)
{
ccDic[r.Address.ToLower()] = r;
}
}

if (Array.IndexOf(bccArray, rec.Name) >= 0)
{
foreach (Recipient r in recList)
{
bccDic[r.Address.ToLower()] = r;
}
}
}

invalidEmails = GetInvalidEmails(emailToCheck);
}
catch (System.Exception e)
{
Cancel = true;
MessageBox.Show(e.Message + "\nMailing Canceled", "Error Validating Emails");
return;
}

StringBuilder toList = new StringBuilder();
StringBuilder ccList = new StringBuilder();
StringBuilder bccList = new StringBuilder();

foreach (KeyValuePair kvp in toDic)
{
if (invalidEmails == null || !invalidEmails.Contains(kvp.Key.Replace("'", "")))
{
toList.AppendFormat("{0};", kvp.Key);
}
}

foreach (KeyValuePair kvp in ccDic)
{
if (invalidEmails == null || !invalidEmails.Contains(kvp.Key.Replace("'", "")))
{
ccList.AppendFormat("{0};", kvp.Key);
}
}

foreach (KeyValuePair kvp in bccDic)
{
if (invalidEmails == null || !invalidEmails.Contains(kvp.Key.Replace("'", "")))
{
bccList.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;
Mail.CC = null;
Mail.BCC = null;

if (toList.Length > 0)
{
Mail.To = toList.ToString().Substring(0, toList.Length - 1);
}

if (ccList.Length > 0)
{
Mail.CC = ccList.ToString().Substring(0, ccList.Length - 1);
}

if (bccList.Length > 0)
{
Mail.BCC = bccList.ToString().Substring(0, bccList.Length - 1);
}
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { }

#region VSTO generated code

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}