Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can't delete file after sending it attached to a mail message

Reply
Thread Tools

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

 
 
Seguros Catatumbo
Guest
Posts: n/a
 
      09-12-2007
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("(E-Mail Removed)");
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.

 
Reply With Quote
 
 
 
 
Lit
Guest
Posts: n/a
 
      09-12-2007
As a test can you delete the file before you send it?
why not null or destroy correo.
Lit


"Seguros Catatumbo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) s.com...
> 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("(E-Mail Removed)");
> 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.
>



 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      09-12-2007
On Sep 12, 6:44 pm, Seguros Catatumbo <(E-Mail Removed)>
wrote:
> 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("(E-Mail Removed)");
> 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.


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

 
Reply With Quote
 
Seguros Catatumbo
Guest
Posts: n/a
 
      09-12-2007

> 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


 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      09-12-2007
On Sep 12, 8:40 pm, Seguros Catatumbo <(E-Mail Removed)>
wrote:
> > 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


Glad it works for you

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Yahoo Mail uploading attached file crashes router joewo Computer Support 1 04-27-2008 10:13 PM
Windows XP freezes when USB mouse/keyboard are attached but not when a serial set is attached John Dalberg Computer Support 4 06-17-2007 09:27 AM
Message not displaying GIF images of attached HTML file Knack Firefox 1 11-01-2006 01:34 AM
pls help me when i sent mail, it vil sending twice instead of once ,am using java.mail,am sending my code.... shailajabtech@gmail.com Java 0 09-28-2006 06:38 AM
Sending 20MB attached file in one go? Sigi Rindler Computer Support 4 10-13-2005 01:07 AM



Advertisments