[英]Attachment.SaveAsFile Method (Outlook) - Error: Cannot Save the Attachment. Cannot Find This File. Verify the Path and File Name are Correct
I am working with an Outlook AddIn which has the functionality to save an email and its attachments to a network drive. 我正在使用Outlook AddIn,它具有将电子邮件及其附件保存到网络驱动器的功能。
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: 对于应用程序的用户之一,当连续保存10个pdf(约1MB)时,该应用程序崩溃并显示以下错误消息:
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. 用户进行了三项测试,同一pdf上发生了两次该问题,另一次发生了该问题。
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. 在网络驱动器上,我可以看到一个.tmp文件,其中包含与原始文件完全相同的字节数。 If I switch the .tmp extension to .pdf I can open the file using adobe reader.
如果将.tmp扩展名切换为.pdf,则可以使用Adobe Reader打开文件。 I see another .pdf file on the same drive, this one containing 0 bytes.
我在同一驱动器上看到另一个.pdf文件,其中包含0个字节。
I am assuming the empty pdf file and the tmp files are related to the SaveAsFile method process. 我假设空的pdf文件和tmp文件与SaveAsFile方法过程有关。
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. 听起来好像您快要用完RPC频道了。 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.