Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Can't delete file after sending it attached to a mail message

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Seguros Catatumbo - 12 Sep 2007 17:44 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.