I have a console application using Outlook = Microsoft.Office.Interop.Outlook reference that iterates through mailitems in a set of shared inboxes and saves the message fields to a database, the message to a specific file path for reference in a workflow application, then moves the emails to a subfolder. The problem I am encountering is when the sender recalls a message (MessageClass = "IPM.Outlook.Recall"). The program stops because it is waiting on me to click "OK" in Outlook. How do I get my console app to programmatically execute the button "OK" click in Outlook, so the program can move on to the next email?
I have done extensive searches for 3 days for a solution to this problem with no luck. This is in Windows 10, Outlook 2016.
Here is my code:
foreach (Object itm in inbox.Items)
{
if (itm is Outlook.MailItem)
{
//Select Email messages from Inbox
Outlook.MailItem msg = (Outlook.MailItem)itm;
if (msg.MessageClass == "IPM.Outlook.Recall")
{
///Solution for handling recall?
}
else
{
//Extract fields from email
EmailMessageFields emailFields = new EmailMessageFields();
emailFields.Source = workflow;
emailFields.MessageFields(msg);
string checkSender = SenderCheck(msg, job);
if (checkSender == "")
{
bool saveSuccess = SaveEmailMessage(msg, boxName, emailFields.ConversationIx);
if (!saveSuccess)
{
msg.Move(loadError);
}
else
{
//Check if email already uploaded then flag as duplicate and move if true
bool dupeCheck = CheckDuplicates(emailFields.ConversationIx);
if (dupeCheck == true)
{
msg.Move(wsco);
}
else
{
//Insert extracted fields into Email table
bool results = InsertMessageFields(emailFields);
if (results)
{
//Move Email to subfolder
msg.Move(wsco);
}
else
{
//If unable to save, move to Load Errors folder
msg.Move(loadError);
}
}
}
}
else
{
if (checkSender == "Not Loaded")
{
msg.Move(notloaded);
}
else if (checkSender == "Faxes")
{
Outlook.MAPIFolder faxFolder = wsco.Folders["Faxes"];
msg.Move(faxFolder);
}
}
}
}
else if (itm is Outlook.AppointmentItem)
{
Outlook.AppointmentItem aitem = (Outlook.AppointmentItem)itm;
aitem.Move(notloaded);
}
else if (itm is Outlook.ReportItem)
{
//Check if email is mail item or return system message
Outlook.ReportItem ritem = (Outlook.ReportItem)itm;
ritem.Move(notloaded);
}
}