Solved

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

Posted on 2004-09-07
3
711 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 200 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
.NET: How to pass typed serialize using JavaScriptSerializer ==> Deserialize in C# 6 52
itextsharp with c# 3 33
Selenium and Xpath 4 66
How does this modal work? 3 31
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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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