Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting "process cannot access file" error when deleting files after sending email
    text
    copied!<p>I am getting error as mentioned below:</p> <p><strong>The process cannot access file "E:\TempPDFs\Sample.pdf" because it is being used by another process</strong></p> <p>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</p> <pre><code>FileInfo DeleteFileInfo = new FileInfo(directoryPath + "\\" + filename + ".pdf"); if (DeleteFileInfo.Exists) File.Delete(directoryPath + "\\" + filename + ".pdf"); </code></pre> <p>here directorypath is E:\TempPDFs, filename is Sample</p> <p><strong>UPDATED:</strong> </p> <pre><code>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 &amp;&amp; !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 &amp;&amp; fromAddress.Trim().Length &gt; 0) { //if (fromAddress != null &amp;&amp; fromName.trim().length &gt; 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 &gt; 0 &amp;&amp; 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); } } } } } </code></pre> <p>let me know if any query</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload