Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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