after email deleting attachment file, error "The process cannot access the file because it is being used by another process."
I am doing an email form. Email has attachments and after attaching files email is sent. Next there is requirement to delete file from server. When I tried to get the file it gave me the subject error. I am even calling GC.Collect() before deleting the file but error is still there. My code for deleting file is:
private void DeleteFiles(DataTable dt)
{
GC.Collect();
String[] sAttachments = new String[dt.Rows.Count];
try
{
sAttachments = new String[dt.Rows.Count];
for (Int32 J = 0; J < dt.Rows.Count; J++)
{
sAttachments[J] = dt.Rows[J]["AttachmentExt"].ToString().Trim();
string workDir =开发者_StackOverflow Server.MapPath(".") + @"\upload\";
if (File.Exists(workDir + sAttachments[J]))
File.Delete(workDir + sAttachments[J]);
}
}
catch (Exception ex)
{
}
For attaching file to email my code is:
oMess.Subject = sSubject;
string workDir = System.Web.HttpContext.Current.Server.MapPath(".") + @"\upload\";
if (Attachments != null)
{
for (Int32 I = 0; I < Attachments.Length; I++)
{
oatt = new Attachment(workDir+ sAttachments[I]);
oMess.Attachments.Add(oatt);
}
}
oMess.IsBodyHtml = IsHtml;
oMess.Body = sBody;
SendMessageGmail(oMess);
Edit: My mail sending code is:
private void SendMessageGmail(MailMessage message)
{
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
NetworkCredential loginInfo = new NetworkCredential("myid", "mypassword");
client.Credentials = loginInfo;
client.Port = 587;
client.Send(message);
}
Plz guide and help. thanks
Use this one. It worked for me
client.Send(oMess);
oMess.Attachments.Dispose();
I tried this one but it didn`t work for me
client.Send(oMess);
oMess.Dispose();
You should try to do
oatt.Dispose();
After execute "SendMessageGmail", free the attachment of MailMessage.Attachments by iterating.
SendMessageGmail(oMess);
if(oMess.Attachments != null) {
for(Int32 I = oMess.Attachments.Count - 1; I >= 0;I--) {
oMess.Attachments[i].Dispose();
}
oMess.Attachments.Clear();
oMess.Attachments.Dispose();
}
oMess.Dispose();
oMess = null;
oMess.Dispose()
This worked for me.
use the below code to fill the imagebox this will solve the problem of deleting the file.
pictureBox1.Load(filePath);
This worked for me:
FileStream file = new FileStream(attachment, FileMode.Open, FileAccess.Read);
MemoryStream ms = new MemoryStream();
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
ms.Write(bytes, 0, (int)file.Length);
objEmail.Attachments.Add(new Attachment(ms, new FileInfo(attachment).Name));
file.Close();
ms.Close();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
System.IO.File.Delete(attachment);
private void SendMessageGmail(MailMessage message)
{
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
NetworkCredential loginInfo = new NetworkCredential("myid", "mypassword");
client.Credentials = loginInfo;
client.Port = 587;
client.Send(message);
oatt.Dispose();
}
精彩评论