I need to write a console application that essentially will:
- Access a group/shared mailbox's inbox.
- Select all emails older than n minutes.
- Move the selected emails to another folder "AgedEmails".
So far the program I wrote works well connecting to my email account, even not passing credentials.
The challenge is to access the shared emailbox not mine. The program will run in a server on a frequency set in Windows task scheduler. I have read many postings where the problem is the same I have, but could not find a solution that works.
I have tried the nameSpace.Logon method but it always connects to my employee's email account.
These are the many ways I tried to login to the shared email account (none work): outlookNameSpace.Logon("[email protected]", "", true, true); outlookNameSpace.Logon("[email protected]", "theRealPassword");, and this is how I try to get a handle into the inbox: inboxFolder = outlookNameSpace.Folders["[email protected]"].Folders["Inbox"];
I am looking for some one to put me in the right direction to achieve the goal. This application will run unattended.
Thanks in advance for your support. Here is the source code:
static async Task MoveEmailsAsync()
{
StreamWriter logFile = new StreamWriter(path, append: true);
Application outLookApplication = null;
NameSpace outlookNameSpace = null;
MAPIFolder inboxFolder = null;
MAPIFolder sourceFolder = null;
MAPIFolder testFolder = null;
Items mailItems = null;
string sSourceFolder = ConfigurationManager.AppSettings["SourceFolder"];
string destinationFolder = ConfigurationManager.AppSettings["DestinationFolder"];
try
{
int minutesAged = Convert.ToInt16(ConfigurationManager.AppSettings["MinutesAged"]);
DateTime age = DateTime.Now;
outLookApplication = new Application();
outlookNameSpace = outLookApplication.GetNamespace("MAPI");
inboxFolder = outlookNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
sourceFolder = inboxFolder.Folders[sSourceFolder.ToString()];
testFolder = inboxFolder.Folders[destinationFolder.ToString()];
mailItems = sourceFolder.Items;
string from = null;
string subject = null;
int counter = mailItems.Count;
int i = 0;
for (int k = counter; k >= 1; k--)
{
try
{
i ;
if (true) //this condition will be removed
{
from = null;
subject = null;
Microsoft.Office.Interop.Outlook.MailItem mailitem = (Microsoft.Office.Interop.Outlook.MailItem)mailItems[k];
TimeSpan ts = DateTime.Now - mailitem.ReceivedTime;
if (ts.TotalMinutes > minutesAged)
{
if (!((mailitem.SenderEmailAddress == null) || (mailitem.SenderEmailAddress == "")))
{
from = mailitem.SenderEmailAddress.ToString();
}
if (!((mailitem.Subject == null) || (mailitem.Subject == "")))
{
subject = mailitem.Subject;
}
await logFile.WriteLineAsync(i ". From: " from " - Subject: " subject);
mailitem.Move(testFolder);
}
}
}
catch (Exception e)
{
await logFile.WriteLineAsync("Exception caught: " e.ToString());
}
}
await logFile.WriteLineAsync(DateTime.Now.ToString() " - End of Job.");
}
catch (Exception e)
{
await logFile.WriteLineAsync("Exception caught: " e.ToString());
}
logFile.Flush();
logFile.Close();
}
}
CodePudding user response:
Firstly, Namespace.Logon
takes the name of an existing profile (as shown in Control Panel | Mail | Show Profiles), not an address of a mailbox.
Secondly, to open a folder from another user's mailbox, replace the GetDefaultFolder
call with Namespace.CreateRecipient
/ Namespace.GetSharedDefaultFolder
CodePudding user response:
Answers to your questions don't make any sense due to the following statement:
The program will run in a server on a frequency set in Windows task scheduler.
That is the key statement in your post because MS states the following for such scenarios:
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.
Read more about that in the Considerations for server-side Automation of Office article.
If you deal with Exchange accounts you may consider using EWS instead, see Explore the EWS Managed API, EWS, and web services in Exchange for more information. In case of Office365 consider using the Graph API, see Use the Microsoft Graph API.