I created a C# console program to loop through a specific folder in Outlook to look for a specific email subject. When I run it on my computer (multi-core CPU), it didn't use up 100% of the CPU resources. But when I test it on the server (single-core CPU), it uses up 100% of the server's CPU resources.
What can I do to optimize the code?
static void Main(string[] args)
{
try
{
string targetFolderName = System.Configuration.ConfigurationSettings.AppSettings["targetFolderName"].ToString().Trim();
string archiveFolderName = System.Configuration.ConfigurationSettings.AppSettings["archiveFolderName"].ToString().Trim();
string companyCode = System.Configuration.ConfigurationSettings.AppSettings["companyCode"].ToString().Trim();
Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.NameSpace nameSpace = outlook.GetNamespace("MAPI");
Microsoft.Office.Interop.Outlook.MAPIFolder mapiFolderPurchase = (Microsoft.Office.Interop.Outlook.MAPIFolder)nameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Parent;
Microsoft.Office.Interop.Outlook.MAPIFolder mapiFolderArchive = (Microsoft.Office.Interop.Outlook.MAPIFolder)outlook.Session.Folders[archiveFolderName].Folders["Inbox"].Parent;
using (SqlConnection sqlConnection = new SqlConnection(ConfigurationSettings.AppSettings["drawingRequestConnectionString"].Trim()))
{
string commandText = "SELECT Regno,UserAccEmail,Rec_date,vendor,approver from SubconJobQ WHERE Status=2 and year(Rec_date)>=2015 ORDER BY Regno DESC";
SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection);
sqlConnection.Open();
using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
{
if (sqlDataReader.HasRows)
{
while (sqlDataReader.Read())
{
//DateTime recordDate = Convert.ToDateTime(sqlDataReader["Rec_date"].ToString().Trim());
//if (recordDate.Year >= 2015)
{
string[] vendors = sqlDataReader["vendor"].ToString().Trim().Split(';');
for (int i = 0; i < vendors.Length; i++)
{
if (!string.IsNullOrEmpty(vendors[i]))
{
string subject = "ASM RFQ: " + vendors[i].Trim() + ", " + sqlDataReader["Regno"].ToString().Trim();
//FIND FOLDER "Sent Email to Vendor"
MAPIFolder vendorFolder = mapiFolderPurchase.Folders["Sent Email to Vendor"];
bool emailFound = false;
//SEARCH EMAIL IN DEFAULT "Sent Email to Vendor" FOLDER
foreach (object item in vendorFolder.Items)
{
MailItem mailItem = item as MailItem;
if (mailItem != null)
{
if (mailItem.Subject != null)
{
if (string.Compare(subject, mailItem.Subject.Trim(), StringComparison.OrdinalIgnoreCase) == 0)
{
emailFound = true;
break;
}
}
}
}
//IF EMAIL NOT FOUND IN DEFAULT "Sent Email to Vendor" FOLDER
//SEARCH IN ARCHIVE "Sent Email to Vendor" FOLDER
if (!emailFound)
{
MAPIFolder archiveSentEmailToVendorFolder = mapiFolderArchive.Folders["Sent Email to Vendor"];
foreach (object item in archiveSentEmailToVendorFolder.Items)
{
MailItem mailItem = item as MailItem;
if (mailItem != null)
{
if (mailItem.Subject != null)
{
if (string.Compare(subject, mailItem.Subject.Trim(), StringComparison.OrdinalIgnoreCase) == 0)
{
emailFound = true;
break;
}
}
}
}
}
if (!emailFound)
{
ShowMessageInConsole(sqlDataReader["Regno"].ToString().Trim() + " not send to vendor: " + vendors[i].Trim() + " yet!");
//SendAlertEmailToDrawingRequestEmailPersonInCharge("Drawing Request Status Check: ", "");
}
}
}
}
}
}
else
{
logger.Warn("Not a single Drawing Request is approved!");
ShowMessageInConsole("Not a single Drawing Request is approved!");
SendAlertEmailToDrawingRequestEmailPersonInCharge("Drawing Request Status Check: " + "Not a single Drawing Request is approved!", "Not a single Drawing Request is approved!");
}
}
}
}
catch (System.Exception ex)
{
logger.Error("Main Exception", ex);
ShowErrorMessageInConsole(ex);
}
finally
{
PressAnyKeyToContinue();
}
}