背景
在做邮件发送因为附件file控件是动态产出的,在后台只能以httpfilecollection方式循环获取,
因为IE和FireFox兼容问题,IE下File.FileName获取的是文件的全路径而在FireFox下就只能获取到文件名,
导致Attachment attchment = new Attachment(File.FileName);火狐下会出错,
因为File.SaveAs(File.FileName) 方法不存在FireFox和IE不兼容的问题,
笨方法,先把附件通过File.SaveAs(File.FileName) 方法上传到服务器上,
问题
邮件发送成功后再删除此文件,会一直报错异常“文件不能删除,另一个进程正在使用”;
原因及处理方式
以为File文件流的问题,真实原因是附件上传成功后需要释放的原因。
代码如下: string attchemUrlstrs = ""; //设置出错时提示的语句数组 string[] MsgStr = new string[3]; MsgStr[0] = "上传文件大小超过限制."; MsgStr[1] = "上传文件扩展名是不允许的扩展名."; MsgStr[2] = "上传文件失败\\n请重试."; int MaxSize = 2048000; string SavePath = "./../../Upload/Attachments/"; MailMessage msg = new MailMessage(); msg.From = new MailAddress(address);# #region 循环添加附件 HttpFileCollection fileList = HttpContext.Current.Request.Files; try { int attSizeCount = 0; ///上载文件列表中的每一个文件 for(int i = 0; i < fileList.Count; i++) { ///获取当前上载的文件 HttpPostedFile File = fileList[i]; //获得文件名 string FileName = System.IO.Path.GetFileName(File.FileName); if(FileName != "") {# region 上传附件到服务器( 这样做是为了ie和firefox不兼容, ie得到的是文件的全路径, 而火狐是文件名) string fileExtension = System.IO.Path.GetExtension(FileName).ToLower(); string imgReName = "" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-ffff", DateTimeFormatInfo.InvariantInfo) + "" + fileExtension; //文件夹名 string imgFolderName = DateTime.Now.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo); try { if(!System.IO.Directory.Exists(@Server.MapPath("" + SavePath + "" + imgFolderName + ""))) { //生成文件完整目录 System.IO.Directory.CreateDirectory(@Server.MapPath("" + SavePath + "" + imgFolderName + "")); } File.SaveAs(@Server.MapPath("" + SavePath + "" + imgFolderName + "/") + imgReName); } catch { Response.Write("<script type=\"text/javascript\">alert(\"附件上传总大小不能超过200k\");</script>"); return; } attchemUrlstrs += @Server.MapPath(SavePath + imgFolderName + "/" + imgReName) + ";"; //记录附件地址 Attachment attchment = new Attachment(@Server.MapPath(SavePath + imgFolderName + "/" + imgReName)); //得到附件的绝对地址 msg.Attachments.Add(attchment); attSizeCount += File.ContentLength;# endregion } } if(attSizeCount > MaxSize) { Response.Write("<script type=\"text/javascript\">alert(\"" + MsgStr[0] + ",附件上传总大小不能超过200k\");</script>"); return; } } catch(Exception ex) { ///显示上载文件的操作失败消息 Response.Write("<script type=\"text/javascript\">alert(\"" + ex.ToString() + ",附件上传总大小不能超过200k\");</script>"); return; }
#endregion //邮件发送地址 string to = this.txtTo.Text; if(to != "" && to != null) { string[] ccAddress = to.Split(','); foreach(string emailTo in ccAddress) { if(emailTo.Trim() != "") { msg.To.Add(emailTo); } } } msg.Subject = this.txtSubject.Text; //邮件标题 msg.SubjectEncoding = System.Text.Encoding.UTF8; //邮件编码 msg.Body = this.content1.Text; //邮件内容 msg.BodyEncoding = System.Text.Encoding.UTF8; msg.IsBodyHtml = true; //是否是html SmtpClient client = new SmtpClient(); if(type == "0") { address = address.Substring(0, address.Length - (address.Length - address.LastIndexOf('@'))); client.Credentials = new System.Net.NetworkCredential(address, emailpass); } if(type == "1") { client.Credentials = new System.Net.NetworkCredential(address, emailpass); } client.Host = server; //发送邮件服务器smtp:25,接收邮件服务器pop:110 client.EnableSsl = false; client.Port = 25; try { client.Send(msg); //发送邮件 Response.Write("<script language='javascript'>alert('邮件发送成功')</script>"); } catch(System.Net.Mail.SmtpException ex) { Response.Write("<script language='javascript'>alert('邮件发送失败,原因是:" + ex.ToString() + ")</script>"); } finally { foreach(Attachment item in msg.Attachments) { item.Dispose(); //一定要释放该对象,否则无法删除附件 }
#region 不论邮件是否发送成功, 删除服务器上临时附件 string[] attchemUrllist = attchemUrlstrs.Split(';'); foreach(string attchemUrl in attchemUrllist) { if(attchemUrl != "") { if(System.IO.File.Exists(attchemUrl)) { try { System.IO.File.Delete(attchemUrl); } catch(Exception ex) { ex.ToString(); } } } }
#endregion }
标签:string,无法访问,System,进程,File,附件,邮件,msg From: https://www.cnblogs.com/turnip/p/17659854.html