Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试添加附件时System.Mail.Net无法访问关闭的文件异常_C#_Asp.net Mvc_System.net.mail - Fatal编程技术网

C# 尝试添加附件时System.Mail.Net无法访问关闭的文件异常

C# 尝试添加附件时System.Mail.Net无法访问关闭的文件异常,c#,asp.net-mvc,system.net.mail,C#,Asp.net Mvc,System.net.mail,嗨,伙计们,我正试图在电子邮件中添加附件,但我遇到了一个奇怪的异常,不清楚我做错了什么。 我从文件输入表单中获取附件 <div class="form-group"> <br/> Select any files to attach to email <div class="input-group"> <label class="input-group-btn btn btn-default btn-file" id="attachFiles"&

嗨,伙计们,我正试图在电子邮件中添加附件,但我遇到了一个奇怪的异常,不清楚我做错了什么。 我从文件输入表单中获取附件

<div class="form-group">
<br/>
Select any files to attach to email
<div class="input-group">
    <label class="input-group-btn btn btn-default btn-file" id="attachFiles">
        Upload <input type="button" id="attach" name="Upload" style="display: none" value="Upload"/>
    </label>
    <label class="input-group-btn btn btn-default btn-file">
        Browse <input type="file" id="attachInput" multiple="false" style="display: none"/>
    </label>
    <input type="text" disabled="disabled" id="attachText" class="form-control input-group-addon"/>
</div>
<br/>
<ul id="issueFormAttachName" class="list-group list-inline"></ul>
在此之后,我会打电话给我的管理员,使用此文件发送电子邮件

string usersToReceived = "";
            EmailMessage email = new EmailMessage
            {
                InnerHtmlBody = emailDto.EmailBody,
                Subject = $"Support enquiry ticket number #{issueId}",
                EmailHeader = "Thank you for contacting support"
            };
            emailDto.Users.ForEach(u => usersToReceived += u + ";");


            Dictionary<string, Stream> streams = new Dictionary<string, Stream>();

            //Check if attachments exist
            if (HomeController.EmailAttachments?.FileCollectionBase.Count > 0)
            {

                foreach (string streamName in HomeController.EmailAttachments.FileCollectionBase)
                {
                   streams.Add(streamName, HomeController.EmailAttachments.FileCollectionBase[streamName].InputStream);    
                }
                foreach (string file in HomeController.EmailAttachments.FileCollectionBase)
                {
                    //make sure stream is not disposed untill send is done
                    //Streams dont need to be explicitly disposed 
                    email.AddAttachment(streams[file], file, HomeController.EmailAttachments.FileCollectionBase[file]?.ContentType);
                }
            }

            email.Send(usersToReceived);

    }
字符串usersToReceived=“”;
EmailMessage电子邮件=新的EmailMessage
{
InnerHtmlBody=emailDto.EmailBody,
主题=$“支持查询票号#{issueId}”,
EmailHeader=“感谢您联系技术支持”
};
emailDto.Users.ForEach(u=>usersToReceived+=u+“;”);
字典流=新字典();
//检查附件是否存在
如果(HomeController.EmailAttachments?.FileCollectionBase.Count>0)
{
foreach(HomeController.EmailAttachments.FileCollectionBase中的字符串streamName)
{
streams.Add(streamName,HomeController.EmailAttachments.FileCollectionBase[streamName].InputStream);
}
foreach(HomeController.EmailAttachments.FileCollectionBase中的字符串文件)
{
//确保在完成发送之前不释放流
//流不需要显式处理
email.AddAttachment(流[文件]、文件、HomeController.EmailAttachments.FileCollectionBase[文件]?.ContentType);
}
}
email.Send(usersToReceived);
}
addattachment方法只调用本机附件构造函数
(新附件(\ms,fileName,mediaType)

现在,有时当我清除缓存并尝试使用上述代码时,这会起作用,但大多数情况下,这会引发异常,错误为无法访问关闭的文件。有人知道这里发生了什么吗?我确实尝试在最后使用清除缓存方法,但这不起作用。

这是一个技巧:

控制器中的每个动作都由反射调用,因此它们是不同的

当您开始
请求.Files
存储在静态变量中-它仅在管道未关闭时可用

接下来,您调用另一个操作,并且您的请求变量不同,因此它们不再存储
文件
属性


为了避免这种情况,您需要一些东西来存储请求之间的数据:数据库、静态变量或类似的东西。

您的附件会随着MVC管道而消亡。如果您将Request.Files内容重写为另一个ICollection变量,然后将其存储,这将非常有用。谢谢,我将给goHi另一个问题,我什么时候需要在Request.Files时或以后或调用send?之前重写它们,因为我看不到我的应用程序将MVC pipelineInside SaveAttachment()方法保留在何处。控制器中的每个操作都是通过反射调用的,因此它们是不同的。@avalothwide编辑原始问题,而不是在注释中询问。
string usersToReceived = "";
            EmailMessage email = new EmailMessage
            {
                InnerHtmlBody = emailDto.EmailBody,
                Subject = $"Support enquiry ticket number #{issueId}",
                EmailHeader = "Thank you for contacting support"
            };
            emailDto.Users.ForEach(u => usersToReceived += u + ";");


            Dictionary<string, Stream> streams = new Dictionary<string, Stream>();

            //Check if attachments exist
            if (HomeController.EmailAttachments?.FileCollectionBase.Count > 0)
            {

                foreach (string streamName in HomeController.EmailAttachments.FileCollectionBase)
                {
                   streams.Add(streamName, HomeController.EmailAttachments.FileCollectionBase[streamName].InputStream);    
                }
                foreach (string file in HomeController.EmailAttachments.FileCollectionBase)
                {
                    //make sure stream is not disposed untill send is done
                    //Streams dont need to be explicitly disposed 
                    email.AddAttachment(streams[file], file, HomeController.EmailAttachments.FileCollectionBase[file]?.ContentType);
                }
            }

            email.Send(usersToReceived);

    }