Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "process cannot access file" error when deleting files after sending email

I am getting error as mentioned below:

The process cannot access file "E:\TempPDFs\Sample.pdf" because it is being used by another process

I happen to send the pdf from email and after email is sent i need to delete the Sample.pdf file. The code that i have written doesn't work

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

here directorypath is E:\TempPDFs, filename is Sample

UPDATED:

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);
                    }
                }
            }
        }
    }

let me know if any query

Thanks!

like image 383
xorpower Avatar asked Feb 07 '11 13:02

xorpower


People also ask

How do you fix the process Cannot access the file?

Process cannot access file because it is being used by another process error message. To resolve this error: Press Ctrl + Alt + Delete, then click Task Manager. Ensure you're on the Processes tab.

How do you force delete the process Cannot access the file because it is being used by another process?

There is no way to delete a file that's currently being used by another process. You have to close whatever program has that file open first, before you can delete it. If you don't already know which program that is, you can figure it out using Handle or Process Explorer.

Why won't my files let me delete a file?

It's most likely because another program is currently trying to use the file. This can occur even if you don't see any programs running. When a file is open by another app or process, Windows 11/10 puts the file into a locked state, and you can't delete, modify, or move it to another location.


1 Answers

Can we see a code that is responsible for sending a PDF file via e-mail? Your problem might be caused by not released memory stream. If you're using an Attachment class then you should do like the following:

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
}

The using statement will ensure that the Dispose method is called when the object gets out of the scope.

UPDATE

After calling the Send method make a call to Dispose of your mail message object:

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

Hope this will help you.

like image 154
volpav Avatar answered Sep 19 '22 22:09

volpav