Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an application with a document management element, part of this element is an Outlook Addin which allows the saving of emails into a server location and updates my applications database.

When the user selects to save the email they are presented with a windows form where they enter extra detail before the email is saved. Intermittently they are receiving an "Operation Failed" error when the code attempts to use the MailItem.SaveAs() method, once the error occurs any further attempt to save emails will throw the same exception.
If the user restarts Outlook and attempts to save the same email again it will, mostly, succeed.

User are experiencing this error on a variety of Operating Systems and versions of Office (2010 and above), nothing unusual about usernames or profile settings.

Any guidance would be very gratefully received.

What I have tried:

The online investigations I have undertaken indicate that this tends to be a problem with file paths or filenames containing invalid characters, based on this I have altered the original code to ensure that the filenames can only contain alpha and numeric characters. The files are saved to the users local temp directory before being moved to the server.

The method used to save the email can be seen below.

The fileName parameter is Guid.NewGuid().ToString().Replace("-","")+".msg", the includeAttachments parameter is always set to True. The Outlook.MailItem is passed into the class constructor and assigned to the mailItem variable.

public void Save(string fileName, bool includeAttachments)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                throw new ArgumentNullException(fileName);

            if (this.mailItem == null)
                return;

            try
            {
                if (includeAttachments)
                {
                    this.mailItem.SaveAs(fileName, 3); <--- This line throws the "Operation Failed" exception
                    return;
                }

                var mailItemCopy = new MailItem(this.mailItem.Copy());

                mailItemCopy.RemoveAttachments();
                mailItemCopy.Item.SaveAs(fileName, 3);
                mailItemCopy.Item.Close(Outlook.OlInspectorClose.olDiscard);
                mailItemCopy.Item.Move(mailItemCopy.Item.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + "(FileName:" + fileName + "; IncludeAttachments:" + includeAttachments.ToString());
            }
        }
Posted
Comments
Richard MacCutchan 3-May-18 10:07am    
Check that filename contains a valid path.
Paul Devereux UK 3-May-18 10:47am    
Hi Richard, paths are generated using Path.Combine(Path.GetTempPath(), "ILBWorkingDocs") and in all of the cases I have seen the folder exists and none of the characters are invalid. My initial thought was the filename was invalid but as I mentioned I have ensured these only contain alpha and numeric characters
Richard MacCutchan 3-May-18 10:53am    
Just because they only contain alpha and numeric characters does not guarantee that they are valid paths. I have path names that include +, #, space characters etc.

Also, I suspect that in some cases "<somepath>\ILBWorkingDocs" may not be valid as a filename.
Paul Devereux UK 3-May-18 11:05am    
Latest instance of the error reports C:\Users\gemma\AppData\Local\Temp\ILBWorkingDocs\3f6d9988510f4d80be28bf30a59060a7.msg as the file path/name that the app is attempting to save. I have validated with the user that this folder exists (although the app is coded in such as way as to create the folder if not), I cannot see where the problem is.
Richard MacCutchan 3-May-18 11:11am    
Without more information it is impossible to guess what is happening under the covers. Take a look inside that folder to see if that reveals any clues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900