2010-05-12 47 views
0

我有一个asp.net网站,我已经做了.Dispose()这里是我的代码如下;该进程无法访问该文件,因为它正在被另一个进程使用

尝试 {

 MailMessage newMail = new MailMessage(MailFrom, MailTo, 

MailSubject,的MailMsg);

 if (MailAttachment != "") 
     { 
      Attachment data = new Attachment(MailAttachment, 

MediaTypeNames.Application.Octet); newMail.Attachments.Add(data); } newMail.BodyEncoding = System.Text.Encoding.UTF8; newMail.IsBodyHtml = true;

 SmtpClient client = new SmtpClient("192.168.2.205"); 
     client.Credentials = CredentialCache.DefaultNetworkCredentials; 
     client.Send(newMail); 

     newMail.Attachments.Dispose(); 
     newMail.Dispose(); 

     DeleteAttachment(MailAttachment); 

     lblSuccess.Text = "Başvurunuz alınmıştır teşekkürler."; 
     lblSuccess.Visible = true; 
     ClearForm(); 
    } 
    catch (Exception ex) 
    { 
     lblSuccess.Text = ex.Message; 
     //lblSuccess.Text = "Bir sorun oluştu bir daha deneyiniz."; 
     lblSuccess.Visible = true; 
    } 

但我得到同样的错误,它的运行良好,在我的本地主机,但在服务器我越来越此错误。我该如何解决它?

+0

也许它被另一个进程使用? 尝试提供更多信息。 – 2010-05-12 08:13:55

+0

也许更大的代码片段将有助于 – Midhat 2010-05-12 08:14:34

+0

我编辑并给出了一个更大的代码片段,希望它会有帮助 – Xenon 2010-05-12 08:19:31

回答

3

调用处理附件对象。

呼叫在SmtpClient上处置,不会在附件上调用它。

+0

它没有奏效。所有同样的作品在本地,但不是在服务器? – Xenon 2010-05-13 08:29:08

+0

你打电话给data.Dispose()吗?或只是在附件集合?尝试个人附件。如果这不起作用,你可以做的另一件事是将文件读入一个字节数组,并从字节数组创建附件。 – 2010-05-13 12:13:21

+0

btw,这里是一个codesnippet来帮助: byte [] data = File.ReadAllBytes(“c:\\ temp \\ sample.jpg”); MemoryStream ms = new MemoryStream(data); 附件a =新附件(ms,“sample.jpg”); m.Attachments.Add(a); m.Attachments.Add(a); File.Delete(“c:\\ temp \\ sample.jpg”); – 2010-05-13 12:25:08

相关问题