2011-02-07 27 views
6

Tôi nhận được lỗi như đề cập dưới đây:Bắt "quá trình không thể truy cập tập tin" báo lỗi khi xóa các tập tin sau khi gửi email

Quá trình này không thể truy cập tập tin "E: \ TempPDFs \ Sample.pdf" bởi vì nó đang được sử dụng bởi một quá trình khác

Tôi tình cờ gửi pdf từ email và sau khi gửi email, tôi cần phải xóa tệp Sample.pdf. Các mã mà tôi đã viết không hoạt động

FileInfo DeleteFileInfo = new FileInfo(directoryPath + "\\" + filename + ".pdf"); 
          if (DeleteFileInfo.Exists) 
           File.Delete(directoryPath + "\\" + filename + ".pdf"); 

đây directorypath là E: \ TempPDFs, filename là mẫu

CẬP NHẬT:

public static void SendMail(string fromAddress, string[] toAddress, string[] ccAddress, string[] bccAddress, string subject, string messageBody, bool isBodyHtml, ArrayList attachments, string host, string username, string pwd, string port) 
    { 

     { 
      try 
      { 
       if (isBodyHtml && !htmlTaxExpression.IsMatch(messageBody)) 
        isBodyHtml = false; 
       // Create the mail message 
       MailMessage objMailMsg; 
       objMailMsg = new MailMessage(); 
       if (toAddress != null) 
       { 
        foreach (string toAddr in toAddress) 
         objMailMsg.To.Add(new MailAddress(toAddr)); 
       } 
       if (ccAddress != null) 
       { 
        foreach (string ccAddr in ccAddress) 
         objMailMsg.CC.Add(new MailAddress(ccAddr)); 
       } 
       if (bccAddress != null) 
       { 
        foreach (string bccAddr in bccAddress) 
         objMailMsg.Bcc.Add(new MailAddress(bccAddr)); 
       } 

       if (fromAddress != null && fromAddress.Trim().Length > 0) 
       { 
        //if (fromAddress != null && fromName.trim().length > 0) 
        // objMailMsg.From = new MailAddress(fromAddress, fromName); 
        //else 
        objMailMsg.From = new MailAddress(fromAddress); 
       } 

       objMailMsg.BodyEncoding = Encoding.UTF8; 
       objMailMsg.Subject = subject; 
       objMailMsg.Body = messageBody; 
       objMailMsg.IsBodyHtml = isBodyHtml; 

       if (attachments != null) 
       { 
        foreach (string fileName in attachments) 
        { 
         if (fileName.Trim().Length > 0 && File.Exists(fileName)) 
          objMailMsg.Attachments.Add(new Attachment(fileName)); 
        } 
       } 

       //prepare to send mail via SMTP transport 
       SmtpClient objSMTPClient = new SmtpClient(); 

       if (objSMTPClient.Credentials != null) 
       { 

       } 
       else 
       { 
        objSMTPClient.UseDefaultCredentials = false; 
        NetworkCredential SMTPUserInfo = new NetworkCredential(username, pwd); 
        objSMTPClient.Host = host; 
        objSMTPClient.Port = Int16.Parse(port); 
        //objSMTPClient.UseDefaultCredentials = false; 
        objSMTPClient.Credentials = SMTPUserInfo; 
        //objSMTPClient.EnableSsl = true; 
        //objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network; 
       } 
       //objSMTPClient.Host = stmpservername; 
       //objSMTPClient.Credentials 
       //System.Net.Configuration.MailSettingsSectionGroup mMailsettings = null; 
       //string mailHost = mMailsettings.Smtp.Network.Host; 
       try 
       { 
        objSMTPClient.Send(objMailMsg); 
       } 
       catch (SmtpException smtpEx) 
       { 
        if (smtpEx.Message.Contains("secure connection")) 
        { 
         objSMTPClient.EnableSsl = true; 
         objSMTPClient.Send(objMailMsg); 
        } 
       } 
      } 
     } 
    } 

cho tôi biết nếu bất kỳ truy vấn

Cảm ơn!

+0

FYI, The Sample.pdf không mở cửa khi tôi đang nhận được lỗi này. –

+0

Dịch vụ SMTP của bạn có mở tập tin để đọc (gửi thư đồng thời với xóa của bạn) không? – AlG

+0

Bạn có thể cung cấp mã gửi thư không? Bạn có đang tạo PDF động không? – Anuraj

Trả lời

11

Chúng tôi có thể xem mã chịu trách nhiệm gửi tệp PDF qua e-mail không? Vấn đề của bạn có thể là do luồng bộ nhớ không được phát hành. Nếu bạn đang sử dụng một lớp Attachment thì bạn nên làm như sau:

using (Attachment data = new Attachment("document.pdf", MediaTypeNames.Application.Octet)) 
{ 
    // 1. Adding attachment to the e-mail message 
    // 2. Sending out the e-mail message 
} 

Tuyên bố using sẽ đảm bảo rằng phương pháp Dispose được gọi khi đối tượng được ra khỏi phạm vi.

CẬP NHẬT

Sau khi gọi phương thức Send thực hiện cuộc gọi đến Dispose của đối tượng tin nhắn qua thư của bạn:

objSMTPClient.Send(objMailMsg); 
objMailMsg.Dispose(); 

Hy vọng điều này sẽ giúp bạn.

+0

Bạn có thể thấy mã nhưng như tôi đã nói, nó vượt quá giới hạn tối đa của bất kỳ nhận xét nào –

+1

@Romil: Không đăng nó làm nhận xét. Thay vào đó, hãy chỉnh sửa câu hỏi của bạn. – volpav

+0

Cần thiết được thực hiện. –

0

hoặc chưa tốt hơn nếu đối tượng thực hiện IDisposable chỉ cần viết như thế này

((IDisposable)objSMTPClient).Dispose(); 
Các vấn đề liên quan