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

Discussion in 'ASP .Net' started by Seguros Catatumbo, Sep 12, 2007.

  1. 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("");
    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.
     
    Seguros Catatumbo, Sep 12, 2007
    #1
    1. Advertising

  2. Seguros Catatumbo

    Lit Guest

    As a test can you delete the file before you send it?
    why not null or destroy correo.
    Lit


    "Seguros Catatumbo" <> wrote in message
    news:...
    > 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("");
    > 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, Sep 12, 2007
    #2
    1. Advertising

  3. On Sep 12, 6:44 pm, Seguros Catatumbo <>
    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("");
    > 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();
     
    Alexey Smirnov, Sep 12, 2007
    #3

  4. > 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
     
    Seguros Catatumbo, Sep 12, 2007
    #4
  5. On Sep 12, 8:40 pm, Seguros Catatumbo <>
    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
     
    Alexey Smirnov, Sep 12, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Deen
    Replies:
    2
    Views:
    522
    Gregory Toomey
    Jul 4, 2004
  2. Replies:
    0
    Views:
    1,442
  3. Replies:
    4
    Views:
    324
  4. Luke
    Replies:
    2
    Views:
    4,786
    Nigel Wade
    Mar 15, 2007
  5. Deen

    sending docs attached with Perl

    Deen, Jul 3, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    108
    Gregory Toomey
    Jul 4, 2004
Loading...

Share This Page