user3446252
user3446252

Reputation: 13

Outlook Redemption - Permissions issues when opening .msg file from local folder (System.UnauthorizedAccessException)

I have a folder of Journaling messages in .EML format.

I'm writing some code to save an .EML file as a .MSG file, then access the attached .MSG file within the saved .MSG file. When I try to access any of the fields with the attached .MSG file I get the following exception:

$exception{"Error in IMessage.OpenAttach: MAPI_E_NO_ACCESS"} System.UnauthorizedAccessException

I've checked the permission on the file created and they appear to ok.

The code I'm using:

RDOSession Session = new RDOSession();
        foreach (var file in System.IO.Directory.GetFiles(@"\\win2012r2-ns\UserProfiles\phil\Downloads\email Archive\Beth_1\"))
        {
            RDOMail mi = Session.CreateMessageFromMsgFile(@"C:\Temp\temp.msg", "IPM.Mail");
            mi.Import(file, 1024);
            mi.Save();
            Marshal.ReleaseComObject(mi);
            mi = Session.GetMessageFromMsgFile(@"C:\Temp\temp.msg");
            for (int i = mi.Attachments.Count; i >= 1; i--)
            {
                if (mi.Attachments[i].FileName.IndexOf(".msg") != -1)
                {
                    Create_Folders(mi.Attachments[i].EmbeddedMsg.ReceivedTime.Year.ToString(), mi.Attachments[i].EmbeddedMsg.ReceivedTime.Month.ToString(), mi.Attachments[i].EmbeddedMsg.ReceivedTime.Day.ToString());
                    mi.Attachments[i].SaveAsFile(@"\\win2012r2-ns\EmailArchive\" + mi.Attachments[i].EmbeddedMsg.ReceivedTime.Year.ToString() + @"\" + mi.Attachments[i].EmbeddedMsg.ReceivedTime.Month.ToString() + @"\" + mi.Attachments[i].EmbeddedMsg.ReceivedTime.Day.ToString() + @"\" + mi.EntryID.ToString() + ".msg");
                }
            }
            Marshal.ReleaseComObject(mi);
        }

Upvotes: 0

Views: 455

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66255

Messages opened on top of MSG files won’t let you open attachments and attachment table until the previous instances of that object are released. You are using multiple dot notation, resulting in implicit variables that you cannot explicitly release. Try to change the code to the following:

RDOAttachments attachments = mi.Attachments;
for (int i = attachments.Count; i >= 1; i--)
{ 
  RDOAttachment attach = mi.Attachments[i];
  if (attach.Type == OlAttachmentType.olEmbeddeditem)
  { 
    RDOMail embeddedMsg = attach.EmbeddedMsg;
    Create_FoldersembeddedMsg.ReceivedTime.Year.ToString(), embeddedMsg.ReceivedTime.Month.ToString(), embeddedMsg.ReceivedTime.Day.ToString());
    attach.SaveAsFile(@"\\win2012r2-ns\EmailArchive\" + embeddedMsg.ReceivedTime.Year.ToString() + @"\" + embeddedMsg.ReceivedTime.Month.ToString() + @"\" + embeddedMsg.ReceivedTime.Day.ToString() + @"\" + mi.EntryID.ToString() + ".msg");
    Marshal.ReleaseComObject(embeddedMsg);                
  }   
  Marshal.ReleaseComObject(attach);
}
Marshal.ReleaseComObject(attachments);

Upvotes: 2

Related Questions