OK, I'm trying to create an Add-In for Outlook that will retrieve a list of email addresses from the SQL server and compare those addresses to the addresses from the emails in my Outlook inbox. If there is a match, then I save the attachments to a folder and delete the email. I will post the code. Can anyone tell me where I'm going wrong?
private Outlook.Application applicationObject;
NameSpace ns = applicationObject.GetNamespace("MAPI");
MAPIFolder inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
DataSet emails = new DataSet();
conn = new SqlConnection(connStr);
conn.Open();
string query = "Select * from Emails";
comm = new SqlCommand(query, conn);
adapt = new SqlDataAdapter(comm);
adapt.Fill(emails, "Emails");
conn.Close();
int count = 0;
int attach = 0;
int numRows = emails.Tables["Emails"].Rows.Count;
string[] emails2 = new string[numRows];
for (int i = 0; i < numRows;i++)
{
emails2 = Convert.ToString(emails.Tables["Emails"].Rows["email"]);
}
foreach (object item in inbox.Items)
{
MailItem mi = item as MailItem;
for(int x = 0;x < emails2.Length;x++)
{
if ( emails2[x] == mi.SenderEmailAddress)
{
count ++;
Attachments attaches = mi.Attachments;
foreach (object atch in attaches)
{
Attachment att = atch as Attachment;
string attachName = att.FileName;
string path = "C:/temp_attach/" + attachName;
att.SaveAsFile(path);
attach ++;
}
mi.Delete();
}
}
mi = null;
}
string info = string.Format("Emails Deleted: {0}\n", count);
info += string.Format("Attachments Saved: {0}\n",attach);
MessageBox.Show(info, "Info");
private Outlook.Application applicationObject;
NameSpace ns = applicationObject.GetNamespace("MAPI");
MAPIFolder inbox = ns.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
DataSet emails = new DataSet();
conn = new SqlConnection(connStr);
conn.Open();
string query = "Select * from Emails";
comm = new SqlCommand(query, conn);
adapt = new SqlDataAdapter(comm);
adapt.Fill(emails, "Emails");
conn.Close();
int count = 0;
int attach = 0;
int numRows = emails.Tables["Emails"].Rows.Count;
string[] emails2 = new string[numRows];
for (int i = 0; i < numRows;i++)
{
emails2 = Convert.ToString(emails.Tables["Emails"].Rows["email"]);
}
foreach (object item in inbox.Items)
{
MailItem mi = item as MailItem;
for(int x = 0;x < emails2.Length;x++)
{
if ( emails2[x] == mi.SenderEmailAddress)
{
count ++;
Attachments attaches = mi.Attachments;
foreach (object atch in attaches)
{
Attachment att = atch as Attachment;
string attachName = att.FileName;
string path = "C:/temp_attach/" + attachName;
att.SaveAsFile(path);
attach ++;
}
mi.Delete();
}
}
mi = null;
}
string info = string.Format("Emails Deleted: {0}\n", count);
info += string.Format("Attachments Saved: {0}\n",attach);
MessageBox.Show(info, "Info");