Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

10 Experts available now in Live!

Get 1:1 Help Now