Click here to Skip to main content
15,887,214 members
Home / Discussions / C#
   

C#

 
AnswerRe: what wrong with my code Pin
Dave Kreskowiak7-Mar-10 15:51
mveDave Kreskowiak7-Mar-10 15:51 
AnswerRe: what wrong with my code Pin
Wes Aday7-Mar-10 17:53
professionalWes Aday7-Mar-10 17:53 
AnswerNo formatting, no text, no question. Pin
Luc Pattyn7-Mar-10 18:00
sitebuilderLuc Pattyn7-Mar-10 18:00 
AnswerRe: what wrong with my code Pin
Ravi Bhavnani7-Mar-10 18:24
professionalRavi Bhavnani7-Mar-10 18:24 
AnswerRe: what wrong with my code Pin
Anubhava Dimri7-Mar-10 20:24
Anubhava Dimri7-Mar-10 20:24 
AnswerRe: what wrong with my code Pin
Pete O'Hanlon7-Mar-10 22:41
mvePete O'Hanlon7-Mar-10 22:41 
AnswerRe: what wrong with my code Pin
dybs8-Mar-10 14:45
dybs8-Mar-10 14:45 
QuestionOutlook 2007 addin causes outlook to hand + high CPU Pin
mosh_337-Mar-10 10:37
mosh_337-Mar-10 10:37 
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<string> 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<string> addresses = new List<string>();
foreach (AddressEntry entry in addressEntry.Members)
{
addresses.AddRange(GetEmailsList(entry));
}
return addresses;
}

private static List<string> GetInvalidEmails(List<string> 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<string> invalidEmails = new List<string>();
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<string, Recipient> toDic = new Dictionary<string, Recipient>();
Dictionary<string, Recipient> ccDic = new Dictionary<string, Recipient>();
Dictionary<string, Recipient> bccDic = new Dictionary<string, Recipient>();

List<string> emailToCheck = new List<string>();
List<string> 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<Recipient> recList = new List<Recipient>();
ICollection<string> 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<string, Recipient> kvp in toDic)
{
if (invalidEmails == null || !invalidEmails.Contains(kvp.Key.Replace("'", "")))
{
toList.AppendFormat("{0};", kvp.Key);
}
}

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

foreach (KeyValuePair<string, Recipient> 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

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

#endregion
}
}
AnswerRe: Outlook 2007 addin causes outlook to hand + high CPU Pin
Dan Mos7-Mar-10 11:10
Dan Mos7-Mar-10 11:10 
AnswerRe: Outlook 2007 addin causes outlook to hand + high CPU Pin
Luc Pattyn7-Mar-10 11:28
sitebuilderLuc Pattyn7-Mar-10 11:28 
AnswerRe: Outlook 2007 addin causes outlook to hand + high CPU Pin
Wes Aday7-Mar-10 17:55
professionalWes Aday7-Mar-10 17:55 
QuestionAppend to Excel Pin
noamtzu007-Mar-10 5:03
noamtzu007-Mar-10 5:03 
AnswerRe: Append to Excel Pin
Dan Mos7-Mar-10 5:07
Dan Mos7-Mar-10 5:07 
GeneralRe: Append to Excel Pin
noamtzu007-Mar-10 5:31
noamtzu007-Mar-10 5:31 
GeneralRe: Append to Excel Pin
Dan Mos7-Mar-10 5:37
Dan Mos7-Mar-10 5:37 
GeneralRe: Append to Excel Pin
Pete O'Hanlon7-Mar-10 9:23
mvePete O'Hanlon7-Mar-10 9:23 
AnswerNo matter how many times you ask the same question... Pin
Not Active7-Mar-10 9:07
mentorNot Active7-Mar-10 9:07 
AnswerRe: Append to Excel Pin
Dave Kreskowiak7-Mar-10 15:49
mveDave Kreskowiak7-Mar-10 15:49 
AnswerRe: Append to Excel Pin
Wes Aday7-Mar-10 17:58
professionalWes Aday7-Mar-10 17:58 
QuestionInputLanguage Pin
ali_reza_zareian7-Mar-10 4:42
ali_reza_zareian7-Mar-10 4:42 
AnswerRe: InputLanguage Pin
Dan Mos7-Mar-10 4:49
Dan Mos7-Mar-10 4:49 
GeneralRe: InputLanguage Pin
ali_reza_zareian7-Mar-10 4:54
ali_reza_zareian7-Mar-10 4:54 
GeneralRe: InputLanguage Pin
Dan Mos7-Mar-10 5:03
Dan Mos7-Mar-10 5:03 
QuestionC# interface inheritance Pin
Shane55557-Mar-10 3:06
Shane55557-Mar-10 3:06 
AnswerRe: C# interface inheritance Pin
#realJSOP7-Mar-10 3:13
mve#realJSOP7-Mar-10 3:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.