c#邮件带附件发送成功后附件文件不能及时删除
2012-02-23 10:19
435 查看
在做邮件发送因为附件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
}
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
}
相关文章推荐
- c#邮件带附件发送成功后附件文件不能及时删除
- 如何阻止通过Outlook用户发送带Winmail.dat文件附件的邮件
- C#发送邮件小程序(支持群发,附件)
- C# 解决 邮件发送Excel附件后,excel处于锁定状态
- C#发送邮件(附件)类
- linux下c代码如何把指定文件作为附件,通过evolution发送邮件
- 学习记录 用C#.net 加 asp.net发送带附件邮件
- C#利用System.Net发送邮件(带 抄送、密送、附件、html格式的邮件)
- C#发送带附件的邮件
- c#.net 下的邮件发送(含附件)
- C#实现.Net对邮件进行DKIM签名和验证,支持附件,发送邮件签名后直接投递到对方服务器(无需己方邮件服务器)
- c#net中发送带附件的邮件:
- 发送带附件的邮件(包括文件上传)
- C#发送带附件邮件
- C# 带附件邮件发送(支持多附件)
- C#发送邮件小程序(支持群发,附件)
- 一个python的邮件发送脚本,自动,定时,可以附件发送,抄送,附有说明文件
- 【监控文件夹并将增加和删除的文件列表发送邮件完美脚本】-未来星开发开发团队
- C#发送带附件邮件
- C#操作EML邮件文件实例(含HTML格式化邮件正文和附件)