c#郵件帶附件發送成功後附件文件不能及時刪除

 
在做郵件發送因爲附件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
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章