none
SaveAsFile results in {"Cannot save the attachment.":null} exception. RRS feed

  • Question

  • I'm trying to save an attachment to the drafts folder using this:

    Outlook.MAPIFolder folderDrafts = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts);

    Outlook.Attachment attachment =(Outlook.Attachment) mailItem.Attachments[1];
    string path = System.IO.Path.Combine(folderDrafts.FolderPath, attachment.FileName);
    attachment.SaveAsFile(path);

    but every time I get {"Cannot save the attachment.":null} exception, I know it gets the attachment correctly and I can read the attachment.FileName fine, but nothing I do will allow me to save, am I missing something obvious? ( I also tried just saving it with @"C:\temp" for the folderpath and anywhere on the c drive results in saying I cannot save because I don't have appropriate permissions, but that seems like a separate issue.)

    Monday, October 19, 2015 9:11 AM

Answers

  • Hello,

    You can't save the attached file to the Drafts folder in Outlook. You need to specify a local file name.

    >  I also tried just saving it with @"C:\temp" for the folderpath and anywhere on the c drive results in saying I cannot save because I don't have appropriate permissions, but that seems like a separate issue.)

    Yes, the system drive requires admin privileges for writing. You can try to save the attached files to a User's profile folders such as Documents or Application Data folder. Or just choose any other drive.

    Also I'd recommend releasing underlying COM objects. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article in MSDN.

    • Marked as answer by noinfoanywhere Wednesday, October 21, 2015 8:25 AM
    Monday, October 19, 2015 11:16 AM

All replies

  • Hello,

    You can't save the attached file to the Drafts folder in Outlook. You need to specify a local file name.

    >  I also tried just saving it with @"C:\temp" for the folderpath and anywhere on the c drive results in saying I cannot save because I don't have appropriate permissions, but that seems like a separate issue.)

    Yes, the system drive requires admin privileges for writing. You can try to save the attached files to a User's profile folders such as Documents or Application Data folder. Or just choose any other drive.

    Also I'd recommend releasing underlying COM objects. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article in MSDN.

    • Marked as answer by noinfoanywhere Wednesday, October 21, 2015 8:25 AM
    Monday, October 19, 2015 11:16 AM
  • Hi, thanks for the info, I used you advice to try and create a work around with by saving the attachment to the Documents folder and then using:

    Outlook.Application application = new Outlook.Application();
    Outlook.MailItem newMailItem = application.CreateItemFromTemplate(path,folderDrafts) as Outlook.MailItem;

    newMailItem.Save();

    while this succeeds in getting the attachment to the drafts folder it's not a solution since it fills up the hard drive with a bunch of garbage attachments. However if I am understanding this correctly there is no way to delete the files from the harddrive once I'm finished?

    • Edited by noinfoanywhere Wednesday, October 21, 2015 8:37 AM removed some whining
    Monday, October 19, 2015 7:44 PM
  • Hi noinfoanywhere,

    However if I am understanding this correctly there is no way to delete the files from the harddrive once I'm finished?

    Did you mean you want to delete attachments in the specific folder in your harddrive? If so, I suggest you use File.Delete.

    You could refer the link below for more information.
    # File.Delete Method (String)
    https://msdn.microsoft.com/en-us/library/system.io.file.delete%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 21, 2015 8:08 AM
  • Bingo, what Edward suggested is how I went about it. So if anyone else is trying to get .msg attachments into the drafts folder what I did was I created a temporary folder, SaveAsFile() all the attachments into that, then open them using CreateItemFromTemplate() and cast them as MailItems that can then be saved into the drafts folder. After that's all done I iterate through the temp folder and System.IO.File.Delete(file) all the files. So it's a little clumsy but it works, Thanks all!
    Wednesday, October 21, 2015 8:35 AM