Attachment.SaveAsFile Method (Outlook) - Error: Cannot Save the Attachment. Cannot Find This File. Verify the Path and File Name are Correct

StackOverflow https://stackoverflow.com/questions/23377153

Вопрос

I am working with an Outlook AddIn which has the functionality to save an email and its attachments to a network drive.

For one of the users of the application, when saving 10 pdfs of about 1MB in a row, the application crashes with the following error message:

Cannot Save the Attachment. Cannot Find This File. Verify the Path and File Name are Correct.

The user did three tests and the issue happened twice on the same pdf and once on another.

Here is the snippet of code in question:

For myCount As Integer = 1 To inMailItem.Attachments.Count
    If inMailItem.Attachments(myCount).Type <> Outlook.OlAttachmentType.olOLE Then

        Dim thisFileName = IO.Path.GetFileName(inMailItem.Attachments(myCount).FileName)
        Dim thisExt = IO.Path.GetExtension(thisFileName)

        Dim charsAvailable = 46 - thisExt.Length ' [filename][random].ext <= 50 chars
        Dim tmpFileName = IO.Path.GetFileNameWithoutExtension(thisFileName.Substring(0, Math.Min(thisFileName.Length, charsAvailable))) + GetRandomChars(4) + thisExt
        fileName = EscapeSqlInput(tmpFileName)
        attachmentFilePath = myDirectoryName & "\" & fileName
        fileInfo = New IO.FileInfo(attachmentFilePath)
        isValidFileType = "1"
        isEmailBody = "0"
        sortOrder += 1
        inMailItem.Attachments(myCount).SaveAsFile(attachmentFilePath)
        attachmentFileSize = fileInfo.Length

        sb = New StringBuilder()
        sb.Append("INSERT INTO EmailDocuments (EmailFileID, DirectoryPath, FileName, IsValidFileType, AttachmentFileSize, IsEmailBody, SortOrder) ")
        sb.AppendFormat("VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')",
                        emailFileId,
                        directoryPath,
                        fileName,
                        isValidFileType,
                        attachmentFileSize,
                        isEmailBody,
                        sortOrder)
        SqlHelper.SqlExecuteNonQuery(sb.ToString())
    End If
Next

On the network drive I can see a .tmp file containing the exact same number of bytes as the original file. If I switch the .tmp extension to .pdf I can open the file using adobe reader. I see another .pdf file on the same drive, this one containing 0 bytes.

I am assuming the empty pdf file and the tmp files are related to the SaveAsFile method process.

Three users tested the issue. Two in San Diego, CA and one in New York, NY. The person in New York had the failure and the two people in San Diego didn't encounter the issue.

Would somebody have an idea of what could be the cause of this issue?

Это было полезно?

Решение

It sure sounds like you are running out of RPC channels. Avoid using multiple dot notation (especially in a loop) and release all object as soon as you are done with them

Attachments attachments = inMailItem.Attachments;
for (int myCount = 1; myCount <= attachments.Count; myCount++)
{
  Attachment attach = attachments.Item(myCount);
  attach.SaveAsFile(attachmentFilePath);
  Marshal.ReleaseComObject(attach);
}
Marshal.ReleaseComObject(attachments);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top