Solved

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

Posted on 2004-09-07
3
705 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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
FaithNoMore earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now