'In Outlook pdf file named has no source. i am getting error please try removing this file again how can i solve it

I'm writing an Outlook add-in. In this plugin, I am uploading the attachments with the help of SDK. Sometimes it performs the upload process without any problems, but sometimes the code gives an error. I couldn't understand why the problem is. how can i solve this problem.

    {"ClassName":"System.IO.FileLoadException","Message":"Cannot save the attachment.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":"   at Microsoft.Office.Interop.Outlook.Attachment.SaveAsFile(String Path)\r\n   at  OutlookAddIn.BusinessLayer.ThisAddin.ThisAddinMethod.LinkCollection(MailItem MailItemObj) in C:\\Users\\MYPC\\Desktop\\OfficeAddin\\OfficeAddins\\OutlookAddIn\\BusinessLayer\\ThisAddin\\ThisAddinMethod.cs:line 171","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":"8\nSaveAsFile\nOutlookAddIn, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null\nMicrosoft.Office.Interop.Outlook.Attachment\nVoid SaveAsFile(System.String)","HResult":-2147024864,"Source":"Microsoft Outlook","WatsonBuckets":null,"FileLoad_FileName":null,"FileLoad_FusionLog":null}

enter image description here

    foreach (Attachment attach in MailItemObj.Attachments)
            {
                i = i + 1;
                string get = attach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
                if (!html.Contains("cid:" + attach.DisplayName) && (string.IsNullOrEmpty(get) || !html.Contains("cid:" + get)))
                {
                    var name = attach.DisplayName;
                    try
                    {
                        var index = attachmentInfoList.FirstOrDefault(f=>f.Id==attach.Index);

                        string filePath = Path.Combine(tempPath, attach.DisplayName);
                        string mainDirectoryPath = Path.GetDirectoryName(filePath);
                        string extension = Path.GetExtension(filePath);
                        string fileName = Path.GetFileNameWithoutExtension(filePath);
                        var number = oldFileList.Count(y => y == name);
                        string customFilePath = string.Empty;
                        string newFileName = string.Empty;
                        string fullFileName = string.Empty;
                        if (index!=null)
                        {
                            if (index.NewFileName!=null)
                            {

                         
                            
                            customFilePath = Path.Combine(mainDirectoryPath, index.NewFileName);
                            fullFileName = Path.GetFileName(customFilePath);
                            
                             newFileName = Path.GetDirectoryName(customFilePath);
                             fullFileName = Path.GetFileName(customFilePath);
                            attach.SaveAsFile(customFilePath);
                            linkFileCollection.Add(new SDK.V2.OutlookDTO.LinkFile(ExtensionTask.GetStreamFromUrl(customFilePath), fullFileName) { Path = customFilePath });
                            }

                        }
                        else
                        {
                           
                            customFilePath = Path.Combine(mainDirectoryPath, fullFileName);
                            fullFileName = Path.GetFileName(customFilePath);
                      
                             newFileName = Path.GetDirectoryName(customFilePath);
                            //string fullFileName = Path.GetFileName(customFilePath);
                            attach.SaveAsFile(customFilePath);
                            linkFileCollection.Add(new SDK.V2.OutlookDTO.LinkFile(ExtensionTask.GetStreamFromUrl(customFilePath), fullFileName) { Path = customFilePath });

                        }



                    }
                    catch (System.Exception ex)
                    {
                        BusinessLayer.Logger.LogWriter.WriteLog(JsonConvert.SerializeObject(ex));
                        throw new System.InvalidOperationException("FileAttach: " + name + " " + lang.FileAttachError);
                    }
                }

            }

I have an error log that crashes at the top



Solution 1:[1]

It 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. Use the for loop instead:

Attachments attachments = inMailItem.Attachments;
for (int myCount = 1; myCount <= attachments.Count; myCount++)
{
  Attachment attach = attachments.Item(myCount);
  attach.SaveAsFile(attachmentFilePath);

  // release the attachment COM object after
  Marshal.ReleaseComObject(attach);
  attach = null;
}
Marshal.ReleaseComObject(attachments);

You can see such kind of problems if you are using Exchange store in the online mode. Exchange server tracks the number of RPC channels used by each client on the per-object level.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene Astafiev