Hi guys, i am having trouble deleting a file after sending an email.
The file is in use.
Here's the code:
String texto = "Test";
System.Net.Mail.SmtpClient smtp = new
System.Net.Mail.SmtpClient();
System.Net.Mail.MailMessage correo = new
System.Net.Mail.MailMessage();
correo.To.Add("test@test.com");
correo.Body = texto;
correo.IsBodyHtml = false;
string pdf="file.pdf";
correo.Attachments.Add(new
System.Net.Mail.Attachment(Server.MapPath(pdf)));
try
{
smtp.Send(correo);
}
catch (Exception)
{
}
finally{
File.Delete(pdf);
}
I have searched on the web and everyone seems to be doing it the same
way. I dont know if this is relevant, but i am creating that file from
itextsharp, a port of itext to c#, which reads a pdf form and saves it
to a new file, which is the one being attached. The pdf file (called a
"stamper") is being closed before the redirect to the email page
happens. And the email is sent succesfully with the attached file if i
don't delete it.
Lit - 12 Sep 2007 17:51 GMT
As a test can you delete the file before you send it?
why not null or destroy correo.
Lit
> Hi guys, i am having trouble deleting a file after sending an email.
> The file is in use.
[quoted text clipped - 30 lines]
> happens. And the email is sent succesfully with the attached file if i
> don't delete it.
Alexey Smirnov - 12 Sep 2007 19:11 GMT
On Sep 12, 6:44 pm, Seguros Catatumbo <seguroscatatu...@gmail.com>
wrote:
> Hi guys, i am having trouble deleting a file after sending an email.
> The file is in use.
[quoted text clipped - 30 lines]
> happens. And the email is sent succesfully with the attached file if i
> don't delete it.
You need to dispose all Attachment objects, or you will leave open
file handles behind. Calling Dispose on the MailMessage will trigger
dispose calls in any attachments it contains.
smtp.Send(correo);
correo.Dispose();
Seguros Catatumbo - 12 Sep 2007 19:40 GMT
> You need to dispose all Attachment objects, or you will leave open
> file handles behind. Calling Dispose on the MailMessage will trigger
> dispose calls in any attachments it contains.
>
> smtp.Send(correo);
> correo.Dispose();
I was just going to reply that i found out about correo.Dispose() and
that it worked :)
Thanks
Alexey Smirnov - 12 Sep 2007 19:42 GMT
On Sep 12, 8:40 pm, Seguros Catatumbo <seguroscatatu...@gmail.com>
wrote:
> > You need to dispose all Attachment objects, or you will leave open
> > file handles behind. Calling Dispose on the MailMessage will trigger
[quoted text clipped - 7 lines]
>
> Thanks
Glad it works for you