Problem with difference between System.Web.Mail and System.Net.Mail in using attachments

Discussion in 'ASP .Net' started by Brad, Apr 25, 2006.

  1. Brad

    Brad Guest

    I have a web .Net app which sends emails with attachments. After the email
    is sent I clean up aftermyself and delete the attachments from disk. In
    upgrading to .Net 2 I changed the email logic from using system.web.mail to
    system.net.mail (of course vs is telling me system.web.mail is obsolete).
    In .Net 1.1, the files deleted with no problem but in .Net 2 when I attempt
    to delete I receive

    The process cannot access the file ...' because it is being used by another
    process.

    If I change the code back to using system.web.mail I can delete the files.
    So it would appear that something about the mail object, using
    system.net.mail, keeps the file in use. I found the same is true if I
    attempt to overwrite the files using something like File.Copy(from,to,
    true). Why in .Net 2 would the disk files still be in use...or at least
    how can they be released?

    Below is snip of code, both the 1.1 and 2.0 version.

    ..Net 1.1 code
    ============================
    Dim mailObj As New System.Web.Mail.MailMessage()
    With mailObj
    .From = fromName
    .To = toName
    .Body = body.ToString
    .Priority = MailPriority.High
    .Subject = "Test"
    .BodyFormat = Mail.MailFormat.Html
    .Attachments.Add(New
    System.Web.Mail.MailAttachment(attachment1Name))
    .Attachments.Add(New
    System.Web.Mail.MailAttachment(attachment2Name))

    End With

    System.Web.Mail.SmtpMail.SmtpServer = server
    System.Web.Mail.SmtpMail.Send(mailObj)

    File.Delete(attachment1Name)
    File.Delete(attachment2Name)
    ========================================


    ..Net 2.0 code
    ========================================
    Dim mailObj As New MailMessage(fromName, toName)
    With mailObj
    .Body = body.ToString
    .Priority = MailPriority.High
    .Subject = "Test"
    .IsBodyHtml = True
    .Attachments.Add(New Attachment(attachment1Name))
    .Attachments.Add(New Attachment(attachment2Name))

    End With

    Dim mailserver As New SmtpClient(server)
    mailserver.Send(mailObj)

    File.Delete(attachment1Name)
    File.Delete(attachment2Name)




    - Brad
     
    Brad, Apr 25, 2006
    #1
    1. Advertising

  2. Brad

    MSDN Guest

    mailObj.Dispose() Releases it.

    SA


    "Brad" <> wrote in message
    news:...
    >I have a web .Net app which sends emails with attachments. After the email
    > is sent I clean up aftermyself and delete the attachments from disk. In
    > upgrading to .Net 2 I changed the email logic from using system.web.mail
    > to
    > system.net.mail (of course vs is telling me system.web.mail is obsolete).
    > In .Net 1.1, the files deleted with no problem but in .Net 2 when I
    > attempt
    > to delete I receive
    >
    > The process cannot access the file ...' because it is being used by
    > another
    > process.
    >
    > If I change the code back to using system.web.mail I can delete the files.
    > So it would appear that something about the mail object, using
    > system.net.mail, keeps the file in use. I found the same is true if I
    > attempt to overwrite the files using something like File.Copy(from,to,
    > true). Why in .Net 2 would the disk files still be in use...or at least
    > how can they be released?
    >
    > Below is snip of code, both the 1.1 and 2.0 version.
    >
    > .Net 1.1 code
    > ============================
    > Dim mailObj As New System.Web.Mail.MailMessage()
    > With mailObj
    > .From = fromName
    > .To = toName
    > .Body = body.ToString
    > .Priority = MailPriority.High
    > .Subject = "Test"
    > .BodyFormat = Mail.MailFormat.Html
    > .Attachments.Add(New
    > System.Web.Mail.MailAttachment(attachment1Name))
    > .Attachments.Add(New
    > System.Web.Mail.MailAttachment(attachment2Name))
    >
    > End With
    >
    > System.Web.Mail.SmtpMail.SmtpServer = server
    > System.Web.Mail.SmtpMail.Send(mailObj)
    >
    > File.Delete(attachment1Name)
    > File.Delete(attachment2Name)
    > ========================================
    >
    >
    > .Net 2.0 code
    > ========================================
    > Dim mailObj As New MailMessage(fromName, toName)
    > With mailObj
    > .Body = body.ToString
    > .Priority = MailPriority.High
    > .Subject = "Test"
    > .IsBodyHtml = True
    > .Attachments.Add(New Attachment(attachment1Name))
    > .Attachments.Add(New Attachment(attachment2Name))
    >
    > End With
    >
    > Dim mailserver As New SmtpClient(server)
    > mailserver.Send(mailObj)
    >
    > File.Delete(attachment1Name)
    > File.Delete(attachment2Name)
    >
    >
    >
    >
    > - Brad
    >
    >
     
    MSDN, Apr 25, 2006
    #2
    1. Advertising

  3. Brad

    Brad Guest

    It most certainly does, thank you very much.


    "MSDN" <> wrote in message
    news:%...
    mailObj.Dispose() Releases it.

    SA


    "Brad" <> wrote in message
    news:...
    >I have a web .Net app which sends emails with attachments. After the email
    > is sent I clean up aftermyself and delete the attachments from disk. In
    > upgrading to .Net 2 I changed the email logic from using system.web.mail
    > to
    > system.net.mail (of course vs is telling me system.web.mail is obsolete).
    > In .Net 1.1, the files deleted with no problem but in .Net 2 when I
    > attempt
    > to delete I receive
    >
    > The process cannot access the file ...' because it is being used by
    > another
    > process.
    >
    > If I change the code back to using system.web.mail I can delete the files.
    > So it would appear that something about the mail object, using
    > system.net.mail, keeps the file in use. I found the same is true if I
    > attempt to overwrite the files using something like File.Copy(from,to,
    > true). Why in .Net 2 would the disk files still be in use...or at least
    > how can they be released?
    >
    > Below is snip of code, both the 1.1 and 2.0 version.
    >
    > .Net 1.1 code
    > ============================
    > Dim mailObj As New System.Web.Mail.MailMessage()
    > With mailObj
    > .From = fromName
    > .To = toName
    > .Body = body.ToString
    > .Priority = MailPriority.High
    > .Subject = "Test"
    > .BodyFormat = Mail.MailFormat.Html
    > .Attachments.Add(New
    > System.Web.Mail.MailAttachment(attachment1Name))
    > .Attachments.Add(New
    > System.Web.Mail.MailAttachment(attachment2Name))
    >
    > End With
    >
    > System.Web.Mail.SmtpMail.SmtpServer = server
    > System.Web.Mail.SmtpMail.Send(mailObj)
    >
    > File.Delete(attachment1Name)
    > File.Delete(attachment2Name)
    > ========================================
    >
    >
    > .Net 2.0 code
    > ========================================
    > Dim mailObj As New MailMessage(fromName, toName)
    > With mailObj
    > .Body = body.ToString
    > .Priority = MailPriority.High
    > .Subject = "Test"
    > .IsBodyHtml = True
    > .Attachments.Add(New Attachment(attachment1Name))
    > .Attachments.Add(New Attachment(attachment2Name))
    >
    > End With
    >
    > Dim mailserver As New SmtpClient(server)
    > mailserver.Send(mailObj)
    >
    > File.Delete(attachment1Name)
    > File.Delete(attachment2Name)
    >
    >
    >
    >
    > - Brad
    >
    >
     
    Brad, Apr 25, 2006
    #3
    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. Ajit
    Replies:
    0
    Views:
    401
  2. Ajit
    Replies:
    0
    Views:
    322
  3. jakk
    Replies:
    4
    Views:
    12,236
  4. tulasikumar
    Replies:
    2
    Views:
    932
    Mark Rae
    Mar 6, 2007
  5. codewarrior
    Replies:
    0
    Views:
    535
    codewarrior
    May 4, 2009
Loading...

Share This Page