?
Solved

C#application - outlook - save mail attachment -- "access denied"?

Posted on 2004-09-07
3
Medium Priority
?
716 Views
Last Modified: 2007-12-19
Hi
     
  I am creating a C# windows application.  This application is supposed to get all the mails from an Outlook inbox and save the attachments to a specified local folder.
 
  So far it works fine for other parts except when it comes to save the mail attachments.   It will throw me an exception "System.Runtime.InteropServices.COMException ... Additional information: Cannot save the attachment.  Access is denied".  

   I have checked to make sure the folder exists, and this folder grants write permission to everyone.  I also wrote a line in my code to create a subdirectory under that folder, and it worked.  So I suspect it might be a problem with my Outlook library.  I am using Outlook 9 library.  

    I am completely new to C#/.Net.  If someone can help me on this , I would really appreciate :)

 Here is my code:

using System.IO;
using System.Reflection;
using Outlook;

.....

string dir  = @"D:\test\load";
if (Directory.Exists(dir)) {
                        Directory.CreateDirectory(dir+"\\test");
 /** This is just for testing write permission to "D:\test\load", and it works **/
}

Outlook.Application olApp = new Outlook.ApplicationClass();
Outlook.NameSpace olNS = olApp.GetNamespace("MAPI");
olNS.Logon(null,null,false,true);
Outlook.MAPIFolder oInboxFolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
MAPIFolder archFolder = oInboxFolder.Folders.Item("test");
Outlook.Items olMailItems = oInboxFolder.Items;
for (int i=olMailItems.Count;i>0; i--) {
      Object objItem = olMailItems.Item(i);
      if (objItem is MailItem) {
            MailItem thisMail = (MailItem) objItem;
            Outlook.Attachments attachs = thisMail.Attachments;
            for (int j=1; j<=attachs.Count; j++) {
                  Attachment thisAttach = attachs.Item(j);
                  thisAttach.SaveAsFile(dir);
            }
            Object moveObj = thisMail.Move(archFolder);
            MailItem  co = (MailItem) moveObj;
}      }

olNS.Logoff();
//olApp.Quit();
                  
...
            



0
Comment
Question by:c2chuc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
FaithNoMore earned 600 total points
ID: 12028176
Might you be getting the 'Access Denied' error from a security feature in Office?  Ive noticed that each Service Pack for Office greatly increases the security.  Most specifically, email security generally means stopping outside programs (usually virus, but can be a valid app) from accessing some of Outlook features/Emails/Contacts.

If this is an issue with Office security, try making some post on the msdn newsgroups (I love EE, but Microsoft has an excelent support community, especially with its MVP program)
0
 

Author Comment

by:c2chuc
ID: 12028324
Thanks... I will try this direction and see if this is the problem.... I will get back to you when I find out ...

thanks again.
0
 

Author Comment

by:c2chuc
ID: 12044784
Thanks... I found the problem... it's because the I forgot to save the file with its name...  thisAttach.SaveAsFile(dir) should be  thisAttach.SaveAsFile(dir+thisAttach.FileName).    Thanks for your reply though... I will reward you 100 points.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question