-1

//This below line has some issue but i am unable to find the issue can anyone help me out or provide some alternate option to download the attachment //outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox)

Application outlookApplication = null;
NameSpace outlookNamespace = null;
MAPIFolder inboxFolder = null;
Items mailItems = null;

try
{
outlookApplication = new Application();
outlookNamespace = outlookApplication.GetNamespace("MAPI");
inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);// Code is not executing from this line seems like some issue in this line
mailItems = inboxFolder.Items;

foreach (object collectionItem in mailItems)
{
MailItem newEmail = collectionItem as MailItem;
if (newEmail == null) continue;

if (newEmail.Attachments.Count > 0 && newEmail.UnRead)
{
for (int i = 1; i <= newEmail.Attachments.Count; i++)
{
string filePath = Path.Combine(@"C:\\Download\\1", newEmail.Attachments[i].FileName);
FileInfo file = new FileInfo(newEmail.Attachments[i].FileName);
if (file.Extension == ".xlsx" || file.Extension == ".xls")
{
newEmail.Attachments[i].SaveAsFile(filePath);
newEmail.UnRead = false;
}

}
}

}
}
catch (System.Exception ex)
{

}
S Rahman
  • 1
  • 1
  • what goes wrong? – pm100 Feb 11 '22 at 21:34
  • who is the logged in user when running under IIS . Docs :"Returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Calendar folder for the user who is currently logged on." – pm100 Feb 11 '22 at 21:35
  • I have added log to check where is the issue in my code but log is not going after this code inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox) – S Rahman Feb 11 '22 at 21:38
  • so you are getting an exception, what is it – pm100 Feb 11 '22 at 21:39
  • No exception is throwing – S Rahman Feb 11 '22 at 21:41
  • yes you are. if the code is stopping mid function thats an exception. Do `try{ bad statement }catch(Exception e){ log e.tostring()}` – pm100 Feb 11 '22 at 21:43
  • That I tried . This code will work if i run this in visual studio but it wont work if i use in windows service/Task scheduler . – S Rahman Feb 11 '22 at 21:50
  • did you read my second comment. – pm100 Feb 11 '22 at 21:51
  • Please indent your code. Are you trying to test the patience of people who answer questions here? – Heretic Monkey Feb 11 '22 at 22:02
  • I am using me as logged user – S Rahman Feb 11 '22 at 22:03
  • 1
    Basically, you can't run Office apps in a service. – Heretic Monkey Feb 11 '22 at 22:05
  • @HereticMonkey I am unable to figure out the issue of my code So I have posted the question . – S Rahman Feb 11 '22 at 22:07
  • No one asked why you posted the question... I asked why you don't indent your code, since it seems the least one can do to help people read your code so that they can understand it enough to answer. But in any case, you can't run Office apps in a service (IIS, Windows service), so this will never work anyway. – Heretic Monkey Feb 11 '22 at 22:11
  • Any other alternate way to download outlook attachment or link for Microsoft exchange – S Rahman Feb 11 '22 at 22:12
  • 1
    Your choices are EWS or Graph API. – Eugene Astafiev Feb 11 '22 at 22:13
  • If you keep in mind that there are indeed key differences between in Visual Studio and on IIS, https://blog.lextudio.com/web-application-differences-in-visual-studio-and-iis-60fec7e311b3, then such issues are expected. – Lex Li Feb 12 '22 at 01:13

1 Answers1

1

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 need to access an Outlook store from a windows service you may consider using a low-level API on which Outlook is based on - Extended MAPI. Or just consider using any third-party wrappers around this API such as redemption.

In case of Exchange profiles consider using EWS or Graph API.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45