您的位置:首页 > 编程语言 > C#

c#邮件带附件发送成功后附件文件不能及时删除

2015-11-09 12:21 399 查看

在做邮件发送因为附件file控件是动态产出的,在后台只能以httpfilecollection方式循环获取,问题又产出了,因为ie和firefox兼容问题,

ie 下File.FileName获取的是文件的全路径而在firefox下就只能获取到文件名,导致

Attachment attchment = new Attachment(File.FileName);火狐下会出错, 因为File.SaveAs(File.FileName)方法不存在火狐和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/attchm/";

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

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: